The fastest XML/HTML entity encoder/decoder in 23 kB gzipped.
npm install --save-prod speedy-entities
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'
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'
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('&😘❤️');
// ⮕ '&😘❤'
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.
Generated using TypeDoc