summaryrefslogtreecommitdiff
path: root/js/src
diff options
context:
space:
mode:
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);
+ }
+};