speedy-entities
v3.0.1
Published
The fastest XML/HTML entities decoder.
Downloads
3,146
Maintainers
Readme
speedy-entities 🏎💨
The fastest XML/HTML entity encoder/decoder in 20 kB gzipped.
npm install --save-prod speedy-entities
Decode
There are two preconfigured decoders: decodeXML
and decodeHTML
.
import { decodeXML, decodeHTML } from 'speedy-entities';
decodeXML('ab<');
// ⮕ 'ab<'
decodeHTML('<fooÆ');
// ⮕ '<foo\u00c6'
decodeHTML('⪢̸∳');
// ⮕ '\u2aa2\u0338\u2233'
Custom decoder
You can create a decoder that decodes custom named entities:
import { arrayTrieEncode, trieCreate, trieSet } from '@smikhalevski/trie';
import { createEntityDecoder } from 'speedy-entities';
// Create a trie that would hold entities
const trie = trieCreate<string>();
// Register named entities
trieSet('foo;', 'okay');
trieSet('qux;', 'yeah');
// Encode a trie
const entitiesTrie = arrayTrieEncode(trie);
// Create a decoder
const decode = createEntityDecoder({
entitiesTrie,
numericReferenceSemicolonRequired: true,
});
// Decode entities
decode('&foo;');
// ⮕ 'okay'
decode('&foo');
// ⮕ '&foo'
// Decode numeric character references
decode('abc');
// ⮕ 'abc'
Encode
encodeXML
encodes non-ASCII characters as named XML entities or as numeric references.
escapeXML
escapes only "&'<>
characters.
import { encodeXML, escapeXML } from 'speedy-entities';
encodeXML('&😘❤️');
// ⮕ '&😘❤️'
escapeXML('&😘❤️');
// ⮕ '&😘❤'
Performance
Clone this repo and use npm ci && npm run perf
to run the performance testsuite.
Results are in millions of operations per second. The higher number is better.