string-character-is-astral-surrogate1.11.0
§ 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 . 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 , on Sourcehut.
§ Licence
Copyright © 2010–2020 Roy Revelt and other contributors
Related packages:
String.trim()
but you can choose granularly what to trim