compare-utf8
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

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
  1. JS does not enforce that the bytes composing a string are valid UTF-16.

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i compare-utf8

    Weekly Downloads

    349

    Version

    0.1.1

    License

    Apache-2.0

    Unpacked Size

    17 kB

    Total Files

    5

    Last publish

    Collaborators

    • arv