bmp

xduce.util. bmp

Source:

These functions are used by xduce to create iterators for strings in pre-ES2015 environments. String iterators in ES2015+ account for double-width characters in the Basic Multilingual Plane, returning those double-wide characters as one iterator value. Older environments do not do this; double-width characters would in that case be returned as two distinct characters, but for Xduce's use of these functions.

Note that the built-in charAt and length do not take double-width characters into account even in ES2015+ environments even though iterators do. These functions are still useful as utility functions in any environment.

Methods

(static) charAt(str, index) → {string}

Source:

Returns the character at a particular index in a string, taking double-width BMP characters into account.

This is a BMP version of the standard JavaScript string.charAt function. The index is adjusted to account for double-width characters in the input string, and if the resulting character is double-width, it will be returned as a two-character string. The second half of these double-width characters don't get assigned an index at all, so it works seemlessly between character widths.

Parameters:
Name Type Description
str string

The input string whose character at the given index is sought.

index number

The index in the input string of the character being sought.

Returns:

The character at the given index in the provided string. If this character is a BMP character, the full character will be returned as a two-character string.

Type
string

(static) length(str) → {number}

Source:

Calculates the length of a string, taking double-width BMP characters into account.

Since this function takes double-width characters into account and the build in string length property does not, it is entirely possible for this function to provide a different result than querying the same string's length property.

Parameters:
Name Type Description
str string

The string whose length is being determined.

Returns:

The number of characters in the string, counting double-width BMP characters as single characters.

Type
number