UNPKG

char-info

Version:

Unicode character information library.

97 lines 3.29 kB
"use strict"; /** @external */ /* tslint:disable:naming-convention */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.lookup = void 0; const node_interval_tree_1 = __importDefault(require("node-interval-tree")); const block_ranges_1 = __importDefault(require("./data/block.ranges")); const category_ranges_1 = __importDefault(require("./data/category.ranges")); const script_ranges_1 = __importDefault(require("./data/script.ranges")); function homogenizeRawStr(str) { return str.toLowerCase().replace(/_/g, ""); } const rangeRegex = /(\\\w[0-9a-fA-F]+|[\s\S])(?:-(\\\w[0-9a-fA-F]+|[\s\S]))?/g; function getCharCode(str) { if (str.length === 1) { return str.charCodeAt(0); } const hex = str.slice(2); return Number.parseInt(hex, 16); } function expandIntoRanges(compressedForm) { const matches = []; let x = null; while ((x = rangeRegex.exec(compressedForm))) { matches.push([x[1], x[2] || x[1]]); } const ranges = []; for (const match of matches) { const start = getCharCode(match[0]); const end = getCharCode(match[1]); ranges.push({ low: start, high: end }); } ranges.sort((a, b) => a.low - b.low); return ranges; } function expandRawRecord(raw) { let name; let alias = undefined; const fst = raw[0]; if (fst.constructor === Array) { name = fst[0]; alias = fst[1]; } else { name = fst; } return { name, alias, intervals: expandIntoRanges(raw[1]), get displayName() { return this.alias || this.name; } }; } function buildLookup() { const lookup = { allBlocks: new node_interval_tree_1.default(), allCategories: new node_interval_tree_1.default(), allScripts: new node_interval_tree_1.default(), blocks: new Map(), categories: new Map(), scripts: new Map(), longCategoryToCode: new Map() }; for (const rawBlock of block_ranges_1.default) { const block = expandRawRecord(rawBlock); lookup.blocks.set(homogenizeRawStr(block.name), block); for (const interval of block.intervals) { lookup.allBlocks.insert(interval.low, interval.high, block); } } for (const rawCategory of category_ranges_1.default) { const cat = expandRawRecord(rawCategory); const hName = homogenizeRawStr(cat.name); lookup.categories.set(hName, cat); lookup.longCategoryToCode.set(homogenizeRawStr(cat.alias), hName); for (const interval of cat.intervals) { lookup.allCategories.insert(interval.low, interval.high, cat); } } for (const rawScript of script_ranges_1.default) { const script = expandRawRecord(rawScript); lookup.scripts.set(homogenizeRawStr(script.name), script); for (const interval of script.intervals) { lookup.allScripts.insert(interval.low, interval.high, script); } } return lookup; } exports.lookup = buildLookup(); //# sourceMappingURL=unicode-lookup.js.map