From d4cb5b0f45a95f37f92cac2bb4dc8a998c31d224 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Jan 2021 15:40:11 +0000 Subject: Include strings for scalar values --- js/src/index.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'js/src') diff --git a/js/src/index.ts b/js/src/index.ts index 13b1f3b..1563b59 100644 --- a/js/src/index.ts +++ b/js/src/index.ts @@ -1,15 +1,20 @@ import dingbats from "./dingbats"; -interface UnicodeScalarValue { +export interface UnicodeScalarValue { codePoint: number; + string: string; } const dingbatsByDec: {[key: string]: UnicodeScalarValue} = {}; const dingbatsByHex: {[key: string]: UnicodeScalarValue} = {}; +const fromCodePoint = String.fromCodePoint ? String.fromCodePoint : fromCodePointPolyfill; + for (const dingbat of dingbats) { + const codePoint = parseInt(dingbat["Unicode dec"], 10); const scalarValue = { - codePoint: parseInt(dingbat["Unicode dec"], 10), + codePoint: codePoint, + string: fromCodePoint(codePoint), }; dingbatsByDec[dingbat["Typeface name"] + "_" + dingbat["Dingbat dec"]] = scalarValue; @@ -27,3 +32,16 @@ export function dec(typeface: string, dec: string): UnicodeScalarValue | undefin export function hex(typeface: string, hex: string): UnicodeScalarValue | undefined { return dingbatsByHex[typeface + "_" + hex.toUpperCase()]; } + +function fromCodePointPolyfill(codePoint: number) { + if (codePoint <= 0xFFFF) { + // BMP + return String.fromCharCode(codePoint); + } else { + // Astral + // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae + const highSurrogate = Math.floor((codePoint - 0x10000) / 0x400) + 0xD800; + const lowSurrogate = (codePoint - 0x10000) % 0x400 + 0xDC00; + return String.fromCharCode(highSurrogate, lowSurrogate); + } +}; -- cgit v1.2.3