https://github.com/rocicorp/compare-utf8
Compares JS strings using UTF-8 bitwise semantics
https://github.com/rocicorp/compare-utf8
Last synced: over 1 year ago
JSON representation
Compares JS strings using UTF-8 bitwise semantics
- Host: GitHub
- URL: https://github.com/rocicorp/compare-utf8
- Owner: rocicorp
- License: apache-2.0
- Created: 2022-05-30T10:34:22.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-12-09T22:39:42.000Z (over 1 year ago)
- Last Synced: 2025-03-24T08:47:50.715Z (over 1 year ago)
- Language: JavaScript
- Size: 29.3 KB
- Stars: 7
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# compare-utf8
Compares JS strings using UTF-8 bitwise semantics
## Why?
Strings in JavaScript are UTF-16 encoded[^1].
[^1]: JS does not enforce that the bytes composing a string are _valid_ UTF-16.
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
```js
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
```