§ Quick Take

import { strict as assert } from "assert";
import {
  allNamedEntities,
  allNamedEntitiesSetOnly,
  allNamedEntitiesSetOnlyCaseInsensitive,
  entStartsWith,
  entEndsWith,
  entStartsWithCaseInsensitive,
  entEndsWithCaseInsensitive,
  brokenNamedEntities,
  decode,
  minLength,
  maxLength,
  uncertain,
} from "all-named-html-entities";

assert.equal(
  Object.keys(allNamedEntities).length,
  2125
);
assert.equal(entStartsWith.A.E[0], "AElig");

§ Idea

This package exports a plain object with 11 keys:

Key's nameKey's value's typePurpose
allNamedEntitiesplain objectall named HTML entities, key is entity's name, value is raw decoded entity. 2125 in total.
entStartsWithplain objectall named HTML entities, grouped by first character, then by second
entEndsWithplain objectall named HTML entities, grouped by last character, then by second-to-last
entStartsWithCaseInsensitiveplain objectall named HTML entities, grouped by first character, then by second, both case-insensitive
entEndsWithCaseInsensitiveplain objectall named HTML entities, grouped by last character, then by second-to-last, both case insensitive
decodefunctiondecodes named HTML entities (&...; format)
minLengthintegerlength of the shortest of all named HTML entities (currently 2)
maxLengthintegerlength of the longest of all named HTML entities (currently 31)
uncertainplain objectall named HTML entities which could be interpreted as words if entity was malformed (missing ampersand for example)

§ API - entStartsWith

entStartsWith looks like this:

{
"A": {
    "E": [
        "AElig"
    ],
    "M": [
        "AMP"
    ],
    "a": [
        "Aacute"
    ],
    ...

The point of entStartsWith is that we don't have to iterate through up to 2,127 entities to match. Instead, we match by first and second letter and match against that list, which varies but is on average tens of strings long.

Let's tap it.

For example, imagine, we have to check, is there a named HTML entity to the right of string index 2 in string 123Aacute456:

const { entStartsWith } = require("all-named-html-entities");

// is there a named HTML entity to the right of index 2?
const input = "123Aacute456";

// first we slice the string from third index onwards, we get "Aacute456"
const workingSlice = input.slice(3);

// this is very verbose and exaggerated code but it's for illustrative purposes

// in real life it would be shorter than all this

// define default answer, false:
let result = false;

if (
workingSlice &&
entStartsWith.hasOwnProperty(workingSlice[0]) &&
entStartsWith[workingSlice[0]].hasOwnProperty(workingSlice[1]) &&
entStartsWith[workingSlice[0]][workingSlice[1]].some((entity) =>
workingSlice.startsWith(entity)
)
) {
result = true;
}
console.log(`result: ${result}`);
const { all } = require("all-named-html-entities");
console.log(Array.isArray(all));

§ API - decode

const { decode } = require("all-named-html-entities");
console.log(decode("ℵ"));
// => ℵ

If the given input is not a string, or is an empty string or does not start with ampersand or does not end with semicolon, error is thrown.

Else, check is performed and if it's not found among known entities, a null is returned.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 html-entities-not-email-friendly 0.2.9
All HTML entities which are not email template friendly
📦 detergent 5.11.7
Extracts, cleans and encodes text