Comparing version 4.0.0 to 4.1.0
export declare const getCodePoint: (str: string, index: number) => number; | ||
export declare function encodeHTMLTrieRe(regExp: RegExp, str: string): string; | ||
export interface TrieNode { | ||
value?: string; | ||
next?: Map<number, TrieNode>; | ||
} | ||
export declare function getTrie(map: Record<string, string>): Map<number, TrieNode>; | ||
//# sourceMappingURL=encode-trie.d.ts.map |
@@ -6,4 +6,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getTrie = exports.encodeHTMLTrieRe = exports.getCodePoint = void 0; | ||
var entities_encode_json_1 = __importDefault(require("./maps/entities-encode.json")); | ||
exports.encodeHTMLTrieRe = exports.getCodePoint = void 0; | ||
var encode_html_1 = __importDefault(require("./generated/encode-html")); | ||
function isHighSurrugate(c) { | ||
@@ -26,3 +26,2 @@ return (c & 64512 /* Mask */) === 55296 /* High */; | ||
}; | ||
var htmlTrie = getTrie(entities_encode_json_1.default); | ||
function encodeHTMLTrieRe(regExp, str) { | ||
@@ -36,14 +35,22 @@ var _a; | ||
var char = str.charCodeAt(i); | ||
var next = htmlTrie.get(char); | ||
if (next) { | ||
if (next.next != null && i + 1 < str.length) { | ||
var value = (_a = next.next.get(str.charCodeAt(i + 1))) === null || _a === void 0 ? void 0 : _a.value; | ||
if (value != null) { | ||
ret += str.substring(lastIdx, i) + value; | ||
regExp.lastIndex += 1; | ||
lastIdx = i + 2; | ||
continue; | ||
var next = encode_html_1.default.get(char); | ||
if (next != null) { | ||
if (typeof next !== "string") { | ||
// We are in a branch. Try to match the next char. | ||
if (i + 1 < str.length) { | ||
var value = typeof next.n === "number" | ||
? next.n === str.charCodeAt(i + 1) | ||
? next.o | ||
: null | ||
: next.n.get(str.charCodeAt(i + 1)); | ||
if (value) { | ||
ret += str.substring(lastIdx, i) + value; | ||
lastIdx = regExp.lastIndex += 1; | ||
continue; | ||
} | ||
} | ||
// If we have a character without a value, use a numeric entitiy. | ||
next = (_a = next.v) !== null && _a !== void 0 ? _a : "&#x".concat(char.toString(16), ";"); | ||
} | ||
ret += str.substring(lastIdx, i) + next.value; | ||
ret += str.substring(lastIdx, i) + next; | ||
lastIdx = i + 1; | ||
@@ -60,23 +67,2 @@ } | ||
exports.encodeHTMLTrieRe = encodeHTMLTrieRe; | ||
function getTrie(map) { | ||
var _a, _b, _c, _d; | ||
var trie = new Map(); | ||
for (var _i = 0, _e = Object.keys(map); _i < _e.length; _i++) { | ||
var decoded = _e[_i]; | ||
var entity = map[decoded]; | ||
// Resolve the key | ||
var lastMap = trie; | ||
for (var i = 0; i < decoded.length - 1; i++) { | ||
var char = decoded.charCodeAt(i); | ||
var next = (_a = lastMap.get(char)) !== null && _a !== void 0 ? _a : {}; | ||
lastMap.set(char, next); | ||
lastMap = (_b = next.next) !== null && _b !== void 0 ? _b : (next.next = new Map()); | ||
} | ||
var val = (_c = lastMap.get(decoded.charCodeAt(decoded.length - 1))) !== null && _c !== void 0 ? _c : {}; | ||
(_d = val.value) !== null && _d !== void 0 ? _d : (val.value = "&".concat(entity, ";")); | ||
lastMap.set(decoded.charCodeAt(decoded.length - 1), val); | ||
} | ||
return trie; | ||
} | ||
exports.getTrie = getTrie; | ||
//# sourceMappingURL=encode-trie.js.map |
export declare const getCodePoint: (str: string, index: number) => number; | ||
export declare function encodeHTMLTrieRe(regExp: RegExp, str: string): string; | ||
export interface TrieNode { | ||
value?: string; | ||
next?: Map<number, TrieNode>; | ||
} | ||
export declare function getTrie(map: Record<string, string>): Map<number, TrieNode>; | ||
//# sourceMappingURL=encode-trie.d.ts.map |
@@ -1,2 +0,2 @@ | ||
import htmlMap from "../maps/entities-encode.json" assert {type:"json"}; | ||
import htmlTrie from "./generated/encode-html"; | ||
function isHighSurrugate(c) { | ||
@@ -17,3 +17,2 @@ return (c & 64512 /* Mask */) === 55296 /* High */; | ||
: c.charCodeAt(index); | ||
const htmlTrie = getTrie(htmlMap); | ||
export function encodeHTMLTrieRe(regExp, str) { | ||
@@ -27,14 +26,22 @@ var _a; | ||
const char = str.charCodeAt(i); | ||
const next = htmlTrie.get(char); | ||
if (next) { | ||
if (next.next != null && i + 1 < str.length) { | ||
const value = (_a = next.next.get(str.charCodeAt(i + 1))) === null || _a === void 0 ? void 0 : _a.value; | ||
if (value != null) { | ||
ret += str.substring(lastIdx, i) + value; | ||
regExp.lastIndex += 1; | ||
lastIdx = i + 2; | ||
continue; | ||
let next = htmlTrie.get(char); | ||
if (next != null) { | ||
if (typeof next !== "string") { | ||
// We are in a branch. Try to match the next char. | ||
if (i + 1 < str.length) { | ||
const value = typeof next.n === "number" | ||
? next.n === str.charCodeAt(i + 1) | ||
? next.o | ||
: null | ||
: next.n.get(str.charCodeAt(i + 1)); | ||
if (value) { | ||
ret += str.substring(lastIdx, i) + value; | ||
lastIdx = regExp.lastIndex += 1; | ||
continue; | ||
} | ||
} | ||
// If we have a character without a value, use a numeric entitiy. | ||
next = (_a = next.v) !== null && _a !== void 0 ? _a : `&#x${char.toString(16)};`; | ||
} | ||
ret += str.substring(lastIdx, i) + next.value; | ||
ret += str.substring(lastIdx, i) + next; | ||
lastIdx = i + 1; | ||
@@ -50,21 +57,2 @@ } | ||
} | ||
export function getTrie(map) { | ||
var _a, _b, _c, _d; | ||
const trie = new Map(); | ||
for (const decoded of Object.keys(map)) { | ||
const entity = map[decoded]; | ||
// Resolve the key | ||
let lastMap = trie; | ||
for (let i = 0; i < decoded.length - 1; i++) { | ||
const char = decoded.charCodeAt(i); | ||
const next = (_a = lastMap.get(char)) !== null && _a !== void 0 ? _a : {}; | ||
lastMap.set(char, next); | ||
lastMap = (_b = next.next) !== null && _b !== void 0 ? _b : (next.next = new Map()); | ||
} | ||
const val = (_c = lastMap.get(decoded.charCodeAt(decoded.length - 1))) !== null && _c !== void 0 ? _c : {}; | ||
(_d = val.value) !== null && _d !== void 0 ? _d : (val.value = `&${entity};`); | ||
lastMap.set(decoded.charCodeAt(decoded.length - 1), val); | ||
} | ||
return trie; | ||
} | ||
//# sourceMappingURL=encode-trie.js.map |
{ | ||
"name": "entities", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "Encode & decode XML and HTML entities with ease & speed", | ||
@@ -63,8 +63,7 @@ "author": "Felix Boehm <me@feedic.com>", | ||
"build": "npm run build:cjs && npm run build:esm", | ||
"build:cjs": "tsc && cp -r src/maps lib", | ||
"build:esm": "tsc --module esnext --target es2019 --outDir lib/esm && npm rum build:esm:fixup && echo '{\"type\":\"module\"}' > lib/esm/package.json", | ||
"build:esm:fixup": "sed -i.b '1s|\".*json\"|\"../maps/entities-encode.json\" assert {type:\"json\"}|' lib/esm/encode-trie.js && rm lib/esm/encode-trie.js.b", | ||
"build:cjs": "tsc", | ||
"build:esm": "tsc --module esnext --target es2019 --outDir lib/esm && echo '{\"type\":\"module\"}' > lib/esm/package.json", | ||
"build:docs": "typedoc --hideGenerator src/index.ts", | ||
"build:trie": "ts-node scripts/write-decode-map.ts", | ||
"build:encode-map": "jq -c 'to_entries | reverse | map( {(.value) : .key } ) | sort | add' maps/entities.json > src/maps/entities-encode.json", | ||
"build:encode-trie": "ts-node scripts/write-encode-map.ts", | ||
"prepare": "npm run build" | ||
@@ -71,0 +70,0 @@ }, |
@@ -13,3 +13,3 @@ # entities [![NPM version](http://img.shields.io/npm/v/entities.svg)](https://npmjs.org/package/entities) [![Downloads](https://img.shields.io/npm/dm/entities.svg)](https://npmjs.org/package/entities) [![Build Status](http://img.shields.io/travis/fb55/entities.svg)](http://travis-ci.org/fb55/entities) [![Coverage](http://img.shields.io/coveralls/fb55/entities.svg)](https://coveralls.io/r/fb55/entities) | ||
- ⚡️ Fast: `entities` is the fastes library for decoding HTML entities (as of | ||
August 2021); see [performance](#performance). | ||
April 2022); see [performance](#performance). | ||
- 🎛 Configurable: Get an output tailored for your needs. You are fine with | ||
@@ -67,9 +67,8 @@ UTF8? That'll safe you some bytes. Prefer to only have ASCII characters? We | ||
Strict decoding is for decoding entities in attributes, as well as in legacy | ||
environments. When strict decoding, entities not terminated with a semicolon | ||
will be ignored. | ||
When strict decoding, entities not terminated with a semicolon will be ignored. | ||
This is helpful for decoding entities in legacy environments. | ||
> Why should I use `entities` instead of alternative modules? | ||
As of August 2021, `entities` is a bit faster than other modules. Still, this is | ||
As of April 2022, `entities` is a bit faster than other modules. Still, this is | ||
not a very differentiated space and other modules can catch up. | ||
@@ -84,6 +83,6 @@ | ||
This depends on your bundler, but yes, it should! Eg. recent versions of Webpack | ||
are able to tree-shake commonjs projects, and having the `sideEffects` flag in | ||
the `package.json` set to `false` means that your bundles should be much | ||
smaller. | ||
Yes! `entities` ships as both a CommonJS and a ES module. Note that for best | ||
results, you should not use the `encode` and `decode` functions, as they wrap | ||
around a number of other functions, all of which will remain in the bundle. | ||
Instead, use the functions that you need directly. | ||
@@ -90,0 +89,0 @@ --- |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
724834
68
2122
0
123