Nonflavored Prescription Medicine

npm

Need private packages and team management tools?Check out npm Orgs. »

string-character-is-astral-surrogate

1.10.48 • Public • Published

string-character-is-astral-surrogate

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

Minimum Node version required Repository is on GitLab Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

Table of Contents

Install

npm i string-character-is-astral-surrogate

Consume via a require():

const {
  isHighSurrogate,
  isLowSurrogate 
= require("string-character-is-astral-surrogate");

or as an ES Module:

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

or for web pages, as a production-ready minified script file (so-called "UMD build"), straight from CDN:

<script src="https://cdn.jsdelivr.net/npm/string-character-is-astral-surrogate/dist/string-character-is-astral-surrogate.umd.js"></script>
// in which case you get a global variable "stringCharacterIsAstralSurrogate" which you consume like this:
const { isHighSurrogate, isLowSurrogate } = stringCharacterIsAstralSurrogate;

This package has three builds in dist/ folder:

Type Key in package.json Path Size
Main export - CommonJS version, transpiled to ES5, contains require and module.exports main dist/string-character-is-astral-surrogate.cjs.js 2 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/string-character-is-astral-surrogate.esm.js 1 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/string-character-is-astral-surrogate.umd.js 1 KB

⬆ back to top

Idea

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:

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.

⬆ back to top

Usage

const {
  isHighSurrogate,
  isLowSurrogate 
= require("string-character-is-astral-surrogate");
// 🧢 = \uD83E\uDDE2
console.log(isHighSurrogate("\uD83E"));
// => true
// the first character, high surrogate of the cap is indeed a high surrogate
 
console.log(isHighSurrogate("\uDDE2"));
// => false
// the second character, low surrogate of the cap is NOT a high surrogate
 
console.log(isLowSurrogate("\uD83E"));
// => false
// the first character, high surrogate of the cap is NOT a low surrogate
// it's high surrogate
 
console.log(isLowSurrogate("\uDDE2"));
// => true
// the second character, low surrogate of the cap is indeed a low surrogate
 
// PS.
// undefined yields false, doesn't throw
console.log(isHighSurrogate(undefined));
// => false
 
console.log(isLowSurrogate(undefined));
// => false

⬆ back to top

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.

⬆ back to top

Contributing

  • 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", the "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 console.logs.

⬆ back to top

Licence

MIT License

Copyright (c) 2015-2019 Roy Revelt and other contributors

install

npm i string-character-is-astral-surrogate

Downloadsweekly downloads

901

version

1.10.48

license

MIT

homepage

gitlab.com

repository

Gitgitlab

last publish

collaborators

  • avatar
Report a vulnerability