Skip to content

Compares JS strings using UTF-8 bitwise semantics

License

Notifications You must be signed in to change notification settings

rocicorp/compare-utf8

Repository files navigation

compare-utf8

Compares JS strings using UTF-8 bitwise semantics

Why?

Strings in JavaScript are UTF-16 encoded1.

However, sometimes it is useful to compare strings using UTF-8 bitwise semantics. Especially if you are using strings in different languages or databases where you are limited to UTF-8.

Installation

npm add compare-utf8

Usage

import { compareUTF8 } from 'compare-utf8';

compareUTF8('a', 'b'); // < 0
compareUTF8('a', 'a'); // 0
compareUTF8('b', 'a'); // > 0
compareUTF('a👻', 'a💩'); // < 0

compareUTF8('\u005A', '\uFF3A'); // < 0
compareUTF8('\uFF3A', '\u{1D655}'); // < 0
compareUTF8('\u005A', '\u{1D655}'); // < 0

Footnotes

  1. JS does not enforce that the bytes composing a string are valid UTF-16.

About

Compares JS strings using UTF-8 bitwise semantics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published