summaryrefslogtreecommitdiff
path: root/js/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2021-01-16 15:40:11 +0000
committerMichael Williamson <mike@zwobble.org>2021-01-16 15:40:11 +0000
commitd4cb5b0f45a95f37f92cac2bb4dc8a998c31d224 (patch)
treec0c002024ff6504045861048a9f2fd5174fb8426 /js/src
parent5138195d335706a59ff19b60bb63ae28ca25b490 (diff)
Include strings for scalar values
Diffstat (limited to 'js/src')
-rw-r--r--js/src/index.ts22
1 files changed, 20 insertions, 2 deletions
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);
+ }
+};