rocicorp / compare-utf8

Compares JS strings using UTF-8 bitwise semantics

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

License:Apache License 2.0


Languages

Language:JavaScript 100.0%