# unicode-trie

> Unicode Trie data structure for fast character metadata lookup, ported from ICU

Latest version **2.0.0** (published 2019-11-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install unicode-trie
pnpm add unicode-trie
yarn add unicode-trie
bun add unicode-trie
```

## Health

**Score 38/100 (D)** — status: abandoned.

Positive: has types package; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-11-17 |
| First published | 2014-07-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/unicode-trie) |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 47.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 68 |
| Author | Devon Govett |
| Maintainers | devongovett |

## Links

- npm: https://www.npmjs.com/package/unicode-trie
- Repository: https://github.com/devongovett/unicode-trie
- Issues: https://github.com/devongovett/unicode-trie/issues
- npm.io page: https://npm.io/package/unicode-trie

## Dependencies (2)

- [pako](https://npm.io/package/pako.md) ^0.2.5
- [tiny-inflate](https://npm.io/package/tiny-inflate.md) ^1.0.0

## Recent versions

- 2.0.0 (latest) — 2019-11-17
- 1.0.0 — 2019-06-17
- 0.3.1 — 2015-11-11
- 0.3.0 — 2015-02-15
- 0.2.0 — 2014-12-16
- 0.1.2 — 2014-07-14
- 0.1.1 — 2014-07-14
- 0.1.0 — 2014-07-14

## README

# unicode-trie
A data structure for fast Unicode character metadata lookup, ported from ICU

## Background

When implementing many Unicode algorithms such as text segmentation,
normalization, bidi processing, etc., fast access to character metadata
is crucial to good performance.  There over a million code points in the
Unicode standard, many of which produce the same result when looked up,
so an array or hash table is not appropriate - those data structures are
fast but would require a lot of memory.  The data is generally
grouped in ranges, so you could do a binary search, but that is not
fast enough for some applications.

The [International Components for Unicode](http://site.icu-project.org) (ICU) project
came up with a data structure based on a [Trie](http://en.wikipedia.org/wiki/Trie) that provides fast access
to Unicode metadata.  The range data is precompiled to a serialized
and flattened trie, which is then used at runtime to lookup the necessary
data.  According to my own tests, this is generally at least 50% faster
than binary search, with not too much additional memory required.

## Installation

    npm install unicode-trie

## Building a Trie

Unicode Tries are generally precompiled from data in the Unicode database
for faster runtime performance.  To build a Unicode Trie, use the
`UnicodeTrieBuilder` class.

```js
const UnicodeTrieBuilder = require('unicode-trie/builder');
const fs = require('fs');

// create a trie
let t = new UnicodeTrieBuilder();

// optional parameters for default value, and error value
// if not provided, both are set to 0
t = new UnicodeTrieBuilder(10, 999);

// set individual values and ranges
t.set(0x4567, 99);
t.setRange(0x40, 0xe7, 0x1234);

// you can lookup a value if you like
t.get(0x4567); // => 99

// get a compiled trie (returns a UnicodeTrie object)
const trie = t.freeze();

// write compressed trie to a binary file
fs.writeFileSync('data.trie', t.toBuffer());
```

## Using a precompiled Trie

Once you've built a precompiled trie, you can load it into the
`UnicodeTrie` class, which is a readonly representation of the
trie.  From there, you can lookup values.

```js
const UnicodeTrie = require('unicode-trie');
const fs = require('fs');

// load serialized trie from binary file
const data = fs.readFileSync('data.trie');
const trie = new UnicodeTrie(data);

// lookup a value
trie.get(0x4567); // => 99
```

## License

MIT

---
_Source: https://npm.io/package/unicode-trie · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
