speedy-entities
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

speedy-entities 🏎💨build

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&lt');
// ⮕ 'ab&lt'

decodeHTML('&ltfoo&AElig');
// ⮕ '<foo\u00c6'

decodeHTML('&NotNestedGreaterGreater;&CounterClockwiseContourIntegral;');
// ⮕ '\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('&#X61;&#x62;&#x63;');
// ⮕ '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('&😘❤️');
// ⮕ '&amp;&#x1f618;&#x2764;&#xfe0f;'

escapeXML('&😘❤️');
// ⮕ '&amp;😘❤'

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.

Decode

Decode HTML performance chart

Encode

Encode XML performance chart

Package Sidebar

Install

npm i speedy-entities

Weekly Downloads

302

Version

3.0.1

License

MIT

Unpacked Size

113 kB

Total Files

6

Last publish

Collaborators

  • smikhalevski