@chr33s/pdf-unicode-trie 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +8 -0
- package/README.md +80 -0
- package/dist/builder.d.ts +12 -0
- package/dist/builder.js +835 -0
- package/dist/builder.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/swap.d.ts +1 -0
- package/dist/swap.js +19 -0
- package/dist/swap.js.map +1 -0
- package/dist/unicode-trie.d.ts +26 -0
- package/dist/unicode-trie.js +110 -0
- package/dist/unicode-trie.js.map +1 -0
- package/package.json +37 -0
- package/src/builder.ts +1012 -0
- package/src/index.ts +4 -0
- package/src/swap.ts +21 -0
- package/src/unicode-trie.ts +148 -0
- package/test/builder.test.ts +151 -0
- package/test/swap.test.ts +36 -0
- package/test/trie.test.ts +125 -0
- package/test/unicode-trie.test.ts +258 -0
- package/tsconfig.json +9 -0
- package/tsconfig.typecheck.json +14 -0
- package/vitest.config.ts +8 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2018
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# @chr33s/pdf-unicode-trie
|
|
2
|
+
|
|
3
|
+
> A data structure for fast Unicode character metadata lookup, ported from ICU.
|
|
4
|
+
|
|
5
|
+
`@chr33s/pdf-unicode-trie` lives in the [`chr33s/pdf`](https://github.com/chr33s/pdf) monorepo and provides native ES modules with TypeScript declarations.
|
|
6
|
+
|
|
7
|
+
## Background
|
|
8
|
+
|
|
9
|
+
When implementing many Unicode algorithms such as text segmentation,
|
|
10
|
+
normalization, bidi processing, etc., fast access to character metadata
|
|
11
|
+
is crucial to good performance. There over a million code points in the
|
|
12
|
+
Unicode standard, many of which produce the same result when looked up,
|
|
13
|
+
so an array or hash table is not appropriate - those data structures are
|
|
14
|
+
fast but would require a lot of memory. The data is generally
|
|
15
|
+
grouped in ranges, so you could do a binary search, but that is not
|
|
16
|
+
fast enough for some applications.
|
|
17
|
+
|
|
18
|
+
The [International Components for Unicode](http://site.icu-project.org) (ICU) project
|
|
19
|
+
came up with a data structure based on a [Trie](http://en.wikipedia.org/wiki/Trie) that provides fast access
|
|
20
|
+
to Unicode metadata. The range data is precompiled to a serialized
|
|
21
|
+
and flattened trie, which is then used at runtime to lookup the necessary
|
|
22
|
+
data. According to my own tests, this is generally at least 50% faster
|
|
23
|
+
than binary search, with not too much additional memory required.
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
npm install @chr33s/pdf-unicode-trie
|
|
28
|
+
|
|
29
|
+
## Building a Trie
|
|
30
|
+
|
|
31
|
+
Unicode Tries are generally precompiled from data in the Unicode database
|
|
32
|
+
for faster runtime performance. To build a Unicode Trie, use the
|
|
33
|
+
`UnicodeTrieBuilder` class.
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
import { writeFileSync } from "node:fs";
|
|
37
|
+
import { builder as UnicodeTrieBuilder } from "@chr33s/pdf-unicode-trie";
|
|
38
|
+
|
|
39
|
+
// create a trie
|
|
40
|
+
let builder = new UnicodeTrieBuilder();
|
|
41
|
+
|
|
42
|
+
// optional parameters for default value, and error value
|
|
43
|
+
// if not provided, both are set to 0
|
|
44
|
+
builder = new UnicodeTrieBuilder(10, 999);
|
|
45
|
+
|
|
46
|
+
// set individual values and ranges
|
|
47
|
+
builder.set(0x4567, 99);
|
|
48
|
+
builder.setRange(0x40, 0xe7, 0x1234);
|
|
49
|
+
|
|
50
|
+
// you can lookup a value if you like
|
|
51
|
+
builder.get(0x4567); // => 99
|
|
52
|
+
|
|
53
|
+
// get a compiled trie (returns a UnicodeTrie object)
|
|
54
|
+
const trie = builder.freeze();
|
|
55
|
+
|
|
56
|
+
// write compressed trie to a binary file
|
|
57
|
+
writeFileSync("data.trie", builder.toBuffer());
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Using a precompiled Trie
|
|
61
|
+
|
|
62
|
+
Once you've built a precompiled trie, you can load it into the
|
|
63
|
+
`UnicodeTrie` class, which is a readonly representation of the
|
|
64
|
+
trie. From there, you can lookup values.
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
import { readFileSync } from "node:fs";
|
|
68
|
+
import UnicodeTrie from "@chr33s/pdf-unicode-trie";
|
|
69
|
+
|
|
70
|
+
// load serialized trie from binary file
|
|
71
|
+
const data = readFileSync("data.trie");
|
|
72
|
+
const trie = new UnicodeTrie(data);
|
|
73
|
+
|
|
74
|
+
// lookup a value
|
|
75
|
+
trie.get(0x4567); // => 99
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import UnicodeTrie from "./index.js";
|
|
3
|
+
declare class UnicodeTrieBuilder {
|
|
4
|
+
#private;
|
|
5
|
+
constructor(initialValue?: number | null, errorValue?: number | null);
|
|
6
|
+
set(codePoint: number, value: number): this;
|
|
7
|
+
setRange(start: number, end: number, value: number, overwrite?: boolean | null): this;
|
|
8
|
+
get(c: number, fromLSCP?: boolean | null): number;
|
|
9
|
+
freeze(): UnicodeTrie;
|
|
10
|
+
toBuffer(): Promise<Buffer>;
|
|
11
|
+
}
|
|
12
|
+
export default UnicodeTrieBuilder;
|