package
Entity parser for XML, HTML, External entites with security and NCR control
npm install @nodable/entitiesGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
72.8 kB
Includes dependencies.
Fast, zero-dependency XML/HTML entity encoder and decoder for Node.js.
npm install @nodable/entitiesimport { EntityEncoder, EntityDecoder, ALL_ENTITIES } from '@nodable/entities';
// Encode: plain text → entity references
// v3 requires an explicit entity set when encoding named (non‑ASCII) characters
const enc = new EntityEncoder({ namedEntities: ALL_ENTITIES });
enc.encode('Hello © 2024 & <stuff>');
// → 'Hello © 2024 & <stuff>'
// Decode: entity references → plain text
const dec = new EntityDecoder({ namedEntities: ALL_ENTITIES });
dec.decode('Hello © 2024 & <stuff>');
// → 'Hello © 2024 & <stuff>'If you only need to encode XML‑unsafe characters (&, <, >, ", '), you can disable named‑entity encoding entirely and avoid importing any entity set:
const enc = new EntityEncoder({ encodeAllNamed: false });
enc.encode('© <stuff>'); // → '© <stuff>' (copyright stays literal)To encode only a specific subset of characters (e.g. common HTML entities), pass a custom map:
import { EntityEncoder, COMMON_HTML, CURRENCY } from '@nodable/entities';
const enc = new EntityEncoder({ namedEntities: { ...COMMON_HTML, ...CURRENCY } });
enc.encode('Price: 10 © 2024'); // only COMMON_HTML/CURRENCY entities are recognizedBreaking change:
In v2, new EntityEncoder() automatically used the full built‑in entity set to encode non‑ASCII characters (like © → ©). This caused poor tree‑shaking: every consumer paid the cost of the entire entity table, even if they never used it.
In v3, EntityEncoder no longer includes a default entity set.
If you pass no options and encodeAllNamed is true (the default), the constructor will throw an error:
const enc = new EntityEncoder(); // throws: "encodeAllNamed is true but no `namedEntities` was provided"| What you need | v2 code | v3 code |
|---|---|---|
| Full built‑in set (same as v2) | new EntityEncoder() | import { ALL_ENTITIES } from '@nodable/entities';new EntityEncoder({ namedEntities: ALL_ENTITIES }) |
| Only XML‑unsafe chars (no named entities) | new EntityEncoder({ encodeAllNamed: false }) | Same – no change |
| Custom subset | new EntityEncoder({ namedEntities: myMap }) | Same – but now your map is required for named‑entity encoding |
This change allows bundlers to tree‑shake unused entity categories – you only pay for the characters you actually encode.
@nodable/entities@3.0.0.new EntityEncoder() calls in your project.ALL_ENTITIES and pass it as namedEntities.{ encodeAllNamed: false }.| encode | decode | |
|---|---|---|
entities (npm) | 3.65 M req/s | 1.76 M req/s |
@nodable/entities | 3.33 M req/s | 5.19 M req/s |
MIT