speedy-entities 1.1.3 → 3.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.txt +1 -1
- package/README.md +54 -81
- package/lib/index.d.ts +42 -4
- package/lib/index.js +233 -4
- package/lib/index.mjs +227 -0
- package/package.json +28 -29
- package/lib/createEntityDecoder.d.ts +0 -29
- package/lib/createEntityDecoder.js +0 -89
- package/lib/createEntityManager.d.ts +0 -42
- package/lib/createEntityManager.js +0 -24
- package/lib/data/illegal-code-points.json +0 -129
- package/lib/decodeHtml.d.ts +0 -6
- package/lib/decodeHtml.js +0 -11
- package/lib/decodeXml.d.ts +0 -9
- package/lib/decodeXml.js +0 -18
- package/lib/fromCodePoint.d.ts +0 -5
- package/lib/fromCodePoint.js +0 -33
- package/lib/full-cjs.js +0 -1
- package/lib/full.d.ts +0 -10
- package/lib/full.js +0 -17
- package/lib/gen/entities.json +0 -1
- package/lib/gen/legacy-entities.json +0 -1
- package/lib/gen/overrides.json +0 -1
- package/lib/index-cjs.js +0 -1
- package/lib/misc.d.ts +0 -2
- package/lib/misc.js +0 -4
- package/lib/unpackMap.d.ts +0 -4
- package/lib/unpackMap.js +0 -22
package/LICENSE.txt
CHANGED
package/README.md
CHANGED
|
@@ -1,103 +1,79 @@
|
|
|
1
|
-
# speedy-entities
|
|
1
|
+
# speedy-entities 🏎💨 [](https://github.com/smikhalevski/speedy-entities/actions/workflows/master.yml)
|
|
2
2
|
|
|
3
|
-
[The fastest](#performance) XML/HTML entity decoder
|
|
3
|
+
[The fastest](#performance) XML/HTML entity encoder/decoder in
|
|
4
|
+
[23 kB gzipped](https://bundlephobia.com/package/speedy-entities).
|
|
4
5
|
|
|
5
6
|
```shell
|
|
6
7
|
npm install --save-prod speedy-entities
|
|
7
8
|
```
|
|
8
9
|
|
|
9
|
-
#
|
|
10
|
+
# Decode
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
## Preconfigured decoders
|
|
14
|
-
|
|
15
|
-
There are two preconfigured decoders: `decodeXml` and `decodeHtml`.
|
|
12
|
+
There are two preconfigured decoders: `decodeXML` and `decodeHTML`.
|
|
16
13
|
|
|
17
14
|
```ts
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
decodeXml('ab<'); // → "ab<"
|
|
21
|
-
decodeHtml('<fooÆ'); // → "<foo\u00c6"
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
By default, `decodeHtml` supports only legacy HTML entities. This allowed to reduce the bundle size to just
|
|
25
|
-
[2.3 kB gzipped](https://bundlephobia.com/package/speedy-entities).
|
|
26
|
-
|
|
27
|
-
<details>
|
|
28
|
-
<summary>The list of legacy HTML entities</summary>
|
|
29
|
-
<p>
|
|
15
|
+
import { decodeXML, decodeHTML } from 'speedy-entities';
|
|
30
16
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
> `Eacute` `ecirc` `Ecirc` `egrave` `Egrave` `eth` `ETH` `euml` `Euml` `frac12` `frac14` `frac34` `gt` `GT` `iacute`
|
|
34
|
-
> `Iacute` `icirc` `Icirc` `iexcl` `igrave` `Igrave` `iquest` `iuml` `Iuml` `laquo` `lt` `LT` `macr` `micro` `middot`
|
|
35
|
-
> `nbsp` `not` `ntilde` `Ntilde` `oacute` `Oacute` `ocirc` `Ocirc` `ograve` `Ograve` `ordf` `ordm` `oslash` `Oslash`
|
|
36
|
-
> `otilde` `Otilde` `ouml` `Ouml` `para` `plusmn` `pound` `quot` `QUOT` `raquo` `reg` `REG` `sect` `shy` `sup1` `sup2`
|
|
37
|
-
> `sup3` `szlig` `thorn` `THORN` `times` `uacute` `Uacute` `ucirc` `Ucirc` `ugrave` `Ugrave` `uml` `uuml` `Uuml`
|
|
38
|
-
> `yacute` `Yacute` `yen` `yuml`
|
|
17
|
+
decodeXML('ab<');
|
|
18
|
+
// ⮕ 'ab<'
|
|
39
19
|
|
|
40
|
-
|
|
41
|
-
|
|
20
|
+
decodeHTML('<fooÆ');
|
|
21
|
+
// ⮕ '<foo\u00c6'
|
|
42
22
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
```ts
|
|
47
|
-
import {decodeXml, decodeHtml} from 'speedy-entities/lib/full';
|
|
48
|
-
|
|
49
|
-
decodeXml('ab<'); // → "ab<"
|
|
50
|
-
decodeHtml('⪢̸∳');
|
|
51
|
-
// → "\u2aa2\u0338\u2233"
|
|
23
|
+
decodeHTML('⪢̸∳');
|
|
24
|
+
// ⮕ '\u2aa2\u0338\u2233'
|
|
52
25
|
```
|
|
53
26
|
|
|
54
|
-
|
|
27
|
+
## Custom decoder
|
|
28
|
+
|
|
29
|
+
You can create a decoder that decodes custom named entities:
|
|
55
30
|
|
|
56
31
|
```ts
|
|
57
|
-
import {
|
|
32
|
+
import { arrayTrieEncode, trieCreate, trieSet } from '@smikhalevski/trie';
|
|
33
|
+
import { createEntityDecoder } from 'speedy-entities';
|
|
58
34
|
|
|
59
|
-
|
|
60
|
-
|
|
35
|
+
// Create a trie that would hold entities
|
|
36
|
+
const trie = trieCreate<string>();
|
|
61
37
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
38
|
+
// Register named entities
|
|
39
|
+
trieSet('foo;', 'okay');
|
|
40
|
+
trieSet('qux;', 'yeah');
|
|
65
41
|
|
|
66
|
-
|
|
42
|
+
// Encode a trie
|
|
43
|
+
const entitiesTrie = arrayTrieEncode(trie);
|
|
67
44
|
|
|
68
|
-
|
|
45
|
+
// Create a decoder
|
|
46
|
+
const decode = createEntityDecoder({
|
|
47
|
+
entitiesTrie,
|
|
48
|
+
numericReferenceSemicolonRequired: true,
|
|
49
|
+
});
|
|
69
50
|
|
|
70
|
-
|
|
71
|
-
|
|
51
|
+
// Decode entities
|
|
52
|
+
decode('&foo;');
|
|
53
|
+
// ⮕ 'okay'
|
|
72
54
|
|
|
73
|
-
|
|
74
|
-
|
|
55
|
+
decode('&foo');
|
|
56
|
+
// ⮕ '&foo'
|
|
75
57
|
|
|
76
|
-
//
|
|
77
|
-
|
|
58
|
+
// Decode numeric character references
|
|
59
|
+
decode('abc');
|
|
60
|
+
// ⮕ 'abc'
|
|
61
|
+
```
|
|
78
62
|
|
|
79
|
-
|
|
80
|
-
entityManager.set('bar', 'nope', true);
|
|
63
|
+
# Encode
|
|
81
64
|
|
|
82
|
-
|
|
83
|
-
entityManager.set({
|
|
84
|
-
foo: 'okay',
|
|
85
|
-
qux: 'yeah',
|
|
86
|
-
});
|
|
65
|
+
`encodeXML` encodes non-ASCII characters as named XML entities or as numeric references.
|
|
87
66
|
|
|
88
|
-
|
|
89
|
-
const decode = createEntityDecoder(entityManager);
|
|
67
|
+
`escapeXML` escapes only `"&'<>` characters.
|
|
90
68
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
decode('&foo;'); // → "okay"
|
|
69
|
+
```ts
|
|
70
|
+
import { encodeXML, escapeXML } from 'speedy-entities';
|
|
94
71
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
decode('&bar;'); // → "nope"
|
|
72
|
+
encodeXML('&😘❤️');
|
|
73
|
+
// ⮕ '&😘❤️'
|
|
98
74
|
|
|
99
|
-
|
|
100
|
-
|
|
75
|
+
escapeXML('&😘❤️');
|
|
76
|
+
// ⮕ '&😘❤'
|
|
101
77
|
```
|
|
102
78
|
|
|
103
79
|
# Performance
|
|
@@ -106,13 +82,10 @@ Clone this repo and use `npm ci && npm run perf` to run the performance testsuit
|
|
|
106
82
|
|
|
107
83
|
Results are in millions of operations per second. The higher number is better.
|
|
108
84
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
| `"&<>"` | 4.08 | 3.10 | 3.46 | 1.02 |
|
|
117
|
-
| `"⪢̸"` | 6.47 | 1.84 | 3.80 | 1.64 |
|
|
118
|
-
| `"&NotNestedGreaterGreater"` | 6.44 | 2.74 | 4.00 | 2.41 |
|
|
85
|
+
### Decode
|
|
86
|
+
|
|
87
|
+

|
|
88
|
+
|
|
89
|
+
### Encode
|
|
90
|
+
|
|
91
|
+

|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { ArrayTrie } from '@smikhalevski/trie';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The options recognized by {@linkcode createEntityDecoder}.
|
|
5
|
+
*/
|
|
6
|
+
interface EntityDecoderOptions {
|
|
7
|
+
/**
|
|
8
|
+
* The trie of [character entity references](https://www.w3.org/TR/html4/charset.html#h-5.3.2).
|
|
9
|
+
*/
|
|
10
|
+
entitiesTrie?: ArrayTrie<string>;
|
|
11
|
+
/**
|
|
12
|
+
* If `true` then [numeric character references](https://www.w3.org/TR/html4/charset.html#h-5.3.1) must be terminated
|
|
13
|
+
* with a semicolon to be decoded. Otherwise, numeric character references are recognized even if they aren't
|
|
14
|
+
* terminated with a semicolon.
|
|
15
|
+
*
|
|
16
|
+
* @default false
|
|
17
|
+
*/
|
|
18
|
+
numericReferenceSemicolonRequired?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates an entity decoder that rewrites numeric and named entities found in input to their respective values.
|
|
22
|
+
*
|
|
23
|
+
* @param options The decoder options.
|
|
24
|
+
* @returns A function that decodes entities in the string.
|
|
25
|
+
*/
|
|
26
|
+
declare function createEntityDecoder(options?: EntityDecoderOptions): (input: string) => string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Decodes all known HTML entities and numeric character references in the input.
|
|
30
|
+
*/
|
|
31
|
+
declare const decodeHTML: (input: string) => string;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Decodes XML entities and numeric character references in the input.
|
|
35
|
+
*/
|
|
36
|
+
declare const decodeXML: (input: string) => string;
|
|
37
|
+
|
|
38
|
+
declare function encodeXML(input: string): string;
|
|
39
|
+
|
|
40
|
+
declare function escapeXML(input: string): string;
|
|
41
|
+
|
|
42
|
+
export { EntityDecoderOptions, createEntityDecoder, decodeHTML, decodeXML, encodeXML, escapeXML };
|