Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/smikhalevski/speedy-entities

๐ŸŽ๐Ÿ’จโ€‚The fastest XML/HTML entity encoder/decoder in 20 kB gzipped.
https://github.com/smikhalevski/speedy-entities

decoder entities html-entities

Last synced: 4 days ago
JSON representation

๐ŸŽ๐Ÿ’จโ€‚The fastest XML/HTML entity encoder/decoder in 20 kB gzipped.

Awesome Lists containing this project

README

        

# speedy-entitiesโ€‚๐ŸŽ๐Ÿ’จโ€‚[![build](https://github.com/smikhalevski/speedy-entities/actions/workflows/master.yml/badge.svg?branch=master&event=push)](https://github.com/smikhalevski/speedy-entities/actions/workflows/master.yml)

[The fastest](#performance) XML/HTML entity encoder/decoder in
[20 kB gzipped](https://bundlephobia.com/package/speedy-entities).

```shell
npm install --save-prod speedy-entities
```

# Decode

There are two preconfigured decoders: `decodeXML` and `decodeHTML`.

```ts
import { decodeXML, decodeHTML } from 'speedy-entities';

decodeXML('ab&lt');
// โฎ• 'ab&lt'

decodeHTML('&ltfoo&AElig');
// โฎ• '();

// 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.

```ts
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.

### Decode

![Decode HTML performance chart](./images/perf-decode-html.svg)

### Encode

![Encode XML performance chart](./images/perf-encode-xml.svg)