UNPKG

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