1 | /**
|
2 | * Sutton SignWriting Unicode 8 Module v1.2.0 (https://github.com/sutton-signwriting/unicode8)
|
3 | * Author: Steve Slevinski (https://SteveSlevinski.me)
|
4 | * Sponsor: https://patreon.com/signwriting
|
5 | * Donate: https://donate.sutton-signwriting.io
|
6 | *
|
7 | * convert.mjs is released under the MIT License.
|
8 | */
|
9 |
|
10 | /**
|
11 | * Object of regular expressions for symbol strings
|
12 | *
|
13 | * { base, fill, rotation, full }
|
14 | * @alias symbol.re
|
15 | * @type {object}
|
16 | */
|
17 | let re = {
|
18 | 'base': '(?:\uD836[\uDC00-\uDE8B])',
|
19 | 'fill': '(?:\uD836[\uDE9B-\uDE9F])',
|
20 | 'rotation': '(?:\uD836[\uDEA1-\uDEAF])'
|
21 | };
|
22 | re.full = `(${re.base})(${re.fill})?(${re.rotation})?`;
|
23 |
|
24 | /**
|
25 | * Function to parse symbol string to object
|
26 | * @function symbol.parse
|
27 | * @param {string} symbolString - a symbol string
|
28 | * @returns {object} elements of symbol string
|
29 | * @example
|
30 | * symbol.parse('𝠀')
|
31 | *
|
32 | * return {
|
33 | * 'base': '𝠀',
|
34 | * 'fill': undefined,
|
35 | * 'rotation': undefined
|
36 | * }
|
37 | */
|
38 | const parse = symbolString => {
|
39 | const regex = `^${re.full}`;
|
40 | const m = (typeof symbolString === 'string' ? symbolString.match(new RegExp(regex)) : []) || [];
|
41 | return {
|
42 | 'base': !m[1] ? undefined : m[1],
|
43 | 'fill': !m[2] ? undefined : m[2],
|
44 | 'rotation': !m[3] ? undefined : m[3]
|
45 | };
|
46 | };
|
47 |
|
48 | /** The convert module contains functions to help process the various SignWriting Character sets.
|
49 | * @module convert
|
50 | */
|
51 |
|
52 | /**
|
53 | * Function to convert a SignWriting in Unicode 8 (uni8) code point to a character
|
54 | * @function convert.code2uni
|
55 | * @param {integer} code - unicode code point
|
56 | * @returns {string} SignWriting in Unicode 8 character
|
57 | * @example
|
58 | * convert.code2uni(0x1D800)
|
59 | *
|
60 | * return '𝠀'
|
61 | */
|
62 | const code2uni = code => String.fromCharCode(0xD800 + (code - 0x10000 >> 10), 0xDC00 + (code - 0x10000 & 0x3FF));
|
63 |
|
64 | /**
|
65 | * Function to convert a SignWriting in Unicode 8 (uni8) character to a code point
|
66 | * @function convert.uni2code
|
67 | * @param {string} uni8 - SignWriting in Unicode 8 character
|
68 | * @returns {integer} unicode code point
|
69 | * @example
|
70 | * convert.uni2code('𝠀')
|
71 | *
|
72 | * return 0x1D800
|
73 | */
|
74 | const uni2code = uni8 => uni8.codePointAt(0);
|
75 |
|
76 | /**
|
77 | * Function to convert a SignWriting in Unicode 8 (uni8) character to hex values
|
78 | * @function convert.uni2hex
|
79 | * @param {string} uni8 - SignWriting in Unicode 8 character
|
80 | * @returns {string} hex value of unicode character
|
81 | * @example
|
82 | * convert.uni2hex('𝠀')
|
83 | *
|
84 | * return "1D800"
|
85 | */
|
86 | const uni2hex = uni8 => uni8.codePointAt(0).toString(16).toUpperCase();
|
87 |
|
88 | /**
|
89 | * Function to convert a SignWriting in Unicode 8 (uni8) symbol to Formal SignWriting in ASCII (FSW)
|
90 | * @function convert.uni2fsw
|
91 | * @param {string} uni8 - SignWriting in Unicode 8 character(s)
|
92 | * @returns {string} an FSW symbol key
|
93 | * @example
|
94 | * convert.uni2fsw('𝠀')
|
95 | *
|
96 | * return 'S10000'
|
97 | */
|
98 | const uni2fsw = uni8 => {
|
99 | let sym = parse(uni8);
|
100 | if (sym.base) {
|
101 | sym.base = sym.base.codePointAt(0) - 0x1D700;
|
102 | sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0;
|
103 | sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0;
|
104 | return "S" + sym.base.toString(16) + sym.fill.toString(16) + sym.rotation.toString(16);
|
105 | } else {
|
106 | return undefined;
|
107 | }
|
108 | };
|
109 |
|
110 | /**
|
111 | * Function to convert a SignWriting in Unicode 8 (uni8) symbol to SignWriting in Unicode (SWU)
|
112 | * @function convert.uni2swu
|
113 | * @param {string} uni8 - SignWriting in Unicode 8 character(s)
|
114 | * @returns {string} an SWU symbol
|
115 | * @example
|
116 | * convert.uni2swu('𝠀')
|
117 | *
|
118 | * return ''
|
119 | */
|
120 | const uni2swu = uni8 => {
|
121 | let sym = parse(uni8);
|
122 | if (sym.base) {
|
123 | sym.base = sym.base.codePointAt(0) - 0x1D800;
|
124 | sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0;
|
125 | sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0;
|
126 | return code2uni(0x40001 + sym.base * 96 + sym.fill * 16 + sym.rotation);
|
127 | } else {
|
128 | return undefined;
|
129 | }
|
130 | };
|
131 |
|
132 | /**
|
133 | * Function to convert a Formal SignWriting in ASCII (FSW) to SignWriting in Unicode 8 (uni8)
|
134 | * @function convert.fsw2uni
|
135 | * @param {string} fswSym - an FSW symbol key
|
136 | * @returns {string} SignWriting in Unicode 8 character(s)
|
137 | * @example
|
138 | * convert.fsw2uni('S10000')
|
139 | *
|
140 | * return '𝠀'
|
141 | */
|
142 | const fsw2uni = fswSym => {
|
143 | let base = parseInt(fswSym.slice(1, 4), 16);
|
144 | let fill = parseInt(fswSym.slice(4, 5), 16);
|
145 | let rotation = parseInt(fswSym.slice(5, 6), 16);
|
146 | return code2uni(base + 0x1D700) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : '');
|
147 | };
|
148 |
|
149 | /**
|
150 | * Function to convert a SignWriting in Unicode (SWU) to SignWriting in Unicode 8 (uni8)
|
151 | * @function convert.swu2uni
|
152 | * @param {string} swuSym - an SWU symbol
|
153 | * @returns {string} SignWriting in Unicode 8 character(s)
|
154 | * @example
|
155 | * convert.swu2uni('')
|
156 | *
|
157 | * return '𝠀'
|
158 | */
|
159 | const swu2uni = swuSym => {
|
160 | const symcode = swuSym.codePointAt(0) - 0x40001;
|
161 | const base = parseInt(symcode / 96);
|
162 | const fill = parseInt((symcode - base * 96) / 16);
|
163 | const rotation = parseInt(symcode - base * 96 - fill * 16);
|
164 | return code2uni(base + 0x1D800) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : '');
|
165 | };
|
166 |
|
167 | export { code2uni, fsw2uni, swu2uni, uni2code, uni2fsw, uni2hex, uni2swu };
|
168 |
|
169 | /* the end */
|