Neurological Phenomenon Multiplexer
Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

all-named-html-entities

1.3.1 • Public • Published

all-named-html-entities

List of all named HTML entities

Repository is on GitLab Coverage no dependencies Downloads/Month Test in browser Code style: prettier MIT License

Table of Contents

Install

npm i all-named-html-entities

Consume via a require():

const {
  allNamedEntities,
  entStartsWith,
  entEndsWith,
  entStartsWithCaseInsensitive,
  entEndsWithCaseInsensitive,
  brokenNamedEntities,
  decode,
  minLength,
  maxLength,
  uncertain,
} = require("all-named-html-entities");

or as an ES Module:

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

or for web pages, as a production-ready minified script file (so-called "UMD build"), straight from CDN:

<script src="https://cdn.jsdelivr.net/npm/all-named-html-entities/dist/all-named-html-entities.umd.js"></script>
// in which case you get a global variable "allNamedHtmlEntities" which you consume like this:
const {
  allNamedEntities,
  entStartsWith,
  entEndsWith,
  entStartsWithCaseInsensitive,
  entEndsWithCaseInsensitive,
  brokenNamedEntities,
  decode,
  minLength,
  maxLength,
  uncertain,
} = allNamedHtmlEntities;

This package has three builds in dist/ folder:

Type Key in package.json Path Size
Main export - CommonJS version, transpiled to ES5, contains require and module.exports main dist/all-named-html-entities.cjs.js 268 KB
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import/export. module dist/all-named-html-entities.esm.js 275 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/all-named-html-entities.umd.js 166 KB

⬆ back to top

Idea

This package exports a plain object with 11 keys:

Key's name Key's value's type Purpose
allNamedEntities plain object all named HTML entities, key is entity's name, value is raw decoded entity. 2125 in total.
entStartsWith plain object all named HTML entities, grouped by first character, then by second
entEndsWith plain object all named HTML entities, grouped by last character, then by second-to-last
entStartsWithCaseInsensitive plain object all named HTML entities, grouped by first character, then by second, both case-insensitive
entEndsWithCaseInsensitive plain object all named HTML entities, grouped by last character, then by second-to-last, both case insensitive
decode function decodes named HTML entities (&...; format)
minLength integer length of the shortest of all named HTML entities (currently 2)
maxLength integer length of the longest of all named HTML entities (currently 31)
uncertain plain object all named HTML entities which could be interpreted as words if entity was malformed (missing ampersand for example)

⬆ back to top

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 upto 2127 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));

⬆ back to top

API - decode

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

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.

⬆ back to top

Contributing

  • If you see an error, raise an issue.
  • If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
  • If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
  • If you want to contribute some code, fork the monorepo via GitLab, then write code, then file a pull request on GitLab. We'll merge it in and release.

In monorepo, npm libraries are located in packages/ folder. Inside, the source code is located either in src/ folder (normal npm library) or in the root, cli.js (if it's a command-line application).

The npm script "dev", the "dev": "rollup -c --dev" builds the development version retaining all console.logs with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.logs.

⬆ back to top

Licence

MIT License

Copyright (c) 2015-2020 Roy Revelt and other contributors

Install

npm i all-named-html-entities

DownloadsWeekly Downloads

93

Version

1.3.1

License

MIT

Unpacked Size

1.09 MB

Total Files

8

Last publish

Collaborators

  • avatar