string-character-is-astral-surrogate1.12.14

Tells, is given character a part of astral character, specifically, a high and low surrogate

§ Quick Take

import { strict as assert } from "assert";
import {
  isHighSurrogate,
  isLowSurrogate,
} from "string-character-is-astral-surrogate";

// 🧢 = \uD83E\uDDE2

assert.equal(isHighSurrogate("\uD83E"), true);
// the first character, high surrogate of the cap is indeed a high surrogate

assert.equal(isHighSurrogate("\uDDE2"), false);
// the second character, low surrogate of the cap is NOT a high surrogate

assert.equal(isLowSurrogate("\uD83E"), false);
// the first character, high surrogate of the cap is NOT a low surrogate
// it's a high surrogate

assert.equal(isLowSurrogate("\uDDE2"), true);
// the second character, low surrogate of the cap is indeed a low surrogate

§ Idea

This program identifies high and low surrogates, specifically.

The API comprises of two functions:

isHighSurrogate (char)

isLowSurrogate (char)

It reads the character at first index (the first Unicode code point) and evaluates its charcode. That's it. If there are more characters they are ignored.

In theory, high surrogate goes first, low surrogate goes second source opens in a new tab. This program enables us to detect surrogate-related errors, for example, malformed emoji or parts of emoji.

§ API

Two functions, same API:

isHighSurrogate(str)
isLowSurrogate(str)

Input: zero or more characters, where charCodeAt(0) will be evaluated. Output: Boolean

  • If input is empty string or undefined, false is returned.
  • If input is anything other than the string or undefined, type error is thrown.
  • If input consists of more characters, everything beyond .charCodeAt(0) is ignored.

We return false to make life easier when traversing the string. When you check "next" character, if it doesn't exist, as far as astral-ness is concerned, we're fine, so it yields false. Otherwise, you'd have to check the input before feeding into this library and that's is tedious. This is a low-level library and it doesn't have to be picky.

§ Changelog

See it in the monorepo opens in a new tab, on GitHub.

§ Contributing

To report bugs or request features or assistance, raise an issue on GitHub opens in a new tab.

Any code contributions welcome! All Pull Requests will be dealt promptly.

§ Licence

MIT opens in a new tab

Copyright © 2010–2021 Roy Revelt and other contributors

Related packages:

📦 detergent 7.0.14
Extracts, cleans and encodes text
📦 string-uglify 1.4.14
Shorten sets of strings deterministically, to be git-friendly
📦 string-remove-widows 2.0.14
Helps to prevent widow words in a text
📦 string-split-by-whitespace 2.0.14
Split string into array by chunks of whitespace
📦 string-apostrophes 1.4.14
Comprehensive, HTML-entities-aware tool to typographically-correct the apostrophes and single/double quotes
📦 string-extract-class-names 6.0.14
Extracts CSS class/id names from a string
📦 string-collapse-leading-whitespace 5.0.14
Collapse the leading and trailing whitespace of a string