Tells, is given character a part of astral character, specifically, a high and low surrogate
Table of Contents
npm i string-character-is-astral-surrogate
Consume via a
constisHighSurrogateisLowSurrogate} = ;
or as an ES Module:
or for web pages, as a production-ready minified script file (so-called "UMD build"), straight from CDN:
// in which case you get a global variable "stringCharacterIsAstralSurrogate" which you consume like this:const isHighSurrogate isLowSurrogate = stringCharacterIsAstralSurrogate;
This package has three builds in
|Main export - CommonJS version, transpiled to ES5, contains
|ES module build that Webpack/Rollup understands. Untranspiled ES6 code with
|UMD build for browsers, transpiled, minified, containing
When you traverse a string the most efficient way, index-by-index, using a
for loop, you might stumble upon an astral character's low and high surrogates. This library helps to identify them.
No other library seems to be able to do that. For example, astral-regex can tell you, does a string contain astral characters or does the given character comprise of two surrogates. But it won't help you identify them separately.
I need to be able to identify surrogates separately to be able to cover cases such as surrogates without second counterpart.
In itself, this library is very simple, two functions:
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.
constisHighSurrogateisLowSurrogate} = ;// 🧢 = \uD83E\uDDE2console;// => true// the first character, high surrogate of the cap is indeed a high surrogateconsole;// => false// the second character, low surrogate of the cap is NOT a high surrogateconsole;// => false// the first character, high surrogate of the cap is NOT a low surrogate// it's high surrogateconsole;// => true// the second character, low surrogate of the cap is indeed a low surrogate// PS.// undefined yields false, doesn't throwconsole;// => falseconsole;// => false
Two functions, same API: isHighSurrogate(str) isLowSurrogate(str)
Input: zero or more characters, where
charCodeAt(0) will be evaluated.
- If input is empty string or undefined,
- If input is anything other than the string or undefined, type error is thrown.
- If input consists of more characters, everything beyond
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.
- If you see an error, raise an issue.
- If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
- If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
- If you want to contribute some code, fork the monorepo via GitLab, then write code, then file a pull request on GitLab. We'll merge it in and release.
In monorepo, npm libraries are located in
packages/ folder. Inside, the source code is located either in
src/ folder (normal npm library) or in the root,
cli.js (if it's a command-line application).
The npm script "
"dev": "rollup -c --dev --silent" builds the development version retaining all
console.logs with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all
Copyright (c) 2015-2019 Roy Revelt and other contributors