string-character-is-astral-surrogate1.10.64

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.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 detergent 5.11.10
Extracts, cleans and encodes text
📦 string-process-comma-separated 1.2.14
Extracts chunks from possibly comma or whatever-separated string
📦 string-remove-widows 1.6.17
Helps to prevent widow words in a text
📦 string-trim-spaces-only 2.8.23
Like String.trim() but you can choose granularly what to trim
📦 string-unfancy 3.9.65
Replace all n/m dashes, curly quotes with their simpler equivalents
📦 string-convert-indexes 2.0.2
Convert between native JS string character indexes and grapheme-count-based indexes
📦 string-collapse-white-space 6.0.0
Efficient collapsing of white space with optional outer- and/or line-trimming and HTML tag recognition