string-character-is-astral-surrogate1.11.0

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 Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 detergent 6.1.1
Extracts, cleans and encodes text
📦 string-apostrophes 1.3.2
Comprehensive, HTML-entities-aware tool to typographically-correct the apostrophes and single/double quotes
📦 string-split-by-whitespace 1.7.0
Split string into array by chunks of whitespace
📦 string-trim-spaces-only 2.9.0
Like String.trim() but you can choose granularly what to trim
📦 string-find-malformed 1.2.1
Search for a malformed string. Think of Levenshtein distance but in search
📦 string-strip-html 7.0.3
Strips HTML tags from strings. No parser, accepts mixed sources
📦 string-collapse-white-space 8.0.5
Replace chunks of whitespace with a single spaces