UNPKG

13 kBJavaScriptView Raw
1/**
2* Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core)
3* Author: Steve Slevinski (https://SteveSlevinski.me)
4* convert.js is released under the MIT License.
5*/
6
7(function (global, factory) {
8 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
9 typeof define === 'function' && define.amd ? define(['exports'], factory) :
10 (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.ssw = global.ssw || {}, global.ssw.convert = {})));
11})(this, (function (exports) { 'use strict';
12
13 /**
14 * Object of regular expressions for FSW strings
15 *
16 * @alias fsw.re
17 * @property {string} symbol - regular expressions for a symbol
18 * @property {string} coord - regular expressions for a coordinate
19 * @property {string} sort - regular expressions for the sorting marker
20 * @property {string} box - regular expression for a signbox marker
21 * @property {string} prefix - regular expression for a sorting marker followed by one or more symbols
22 * @property {string} spatial - regular expression for a symbol followed by a coordinate
23 * @property {string} signbox - regular expression for a signbox marker, max coordinate and zero or more spatial symbols
24 * @property {string} sign - regular expression for an optional prefix followed by a signbox
25 * @property {string} sortable - regular expression for a mandatory prefix followed by a signbox
26 */
27 let re$1 = {
28 'symbol': 'S[123][0-9a-f]{2}[0-5][0-9a-f]',
29 'coord': '[0-9]{3}x[0-9]{3}',
30 'sort': 'A',
31 'box': '[BLMR]'
32 };
33 re$1.prefix = `(?:${re$1.sort}(?:${re$1.symbol})+)`;
34 re$1.spatial = `${re$1.symbol}${re$1.coord}`;
35 re$1.signbox = `${re$1.box}${re$1.coord}(?:${re$1.spatial})*`;
36 re$1.sign = `${re$1.prefix}?${re$1.signbox}`;
37 re$1.sortable = `${re$1.prefix}${re$1.signbox}`;
38
39 /**
40 * Object of regular expressions for SWU strings in UTF-16
41 *
42 * @alias swu.re
43 * @property {string} symbol - regular expressions for a symbol
44 * @property {string} coord - regular expressions for a coordinate
45 * @property {string} sort - regular expressions for the sorting marker
46 * @property {string} box - regular expression for a signbox marker
47 * @property {string} prefix - regular expression for a sorting marker followed by one or more symbols
48 * @property {string} spatial - regular expression for a symbol followed by a coordinate
49 * @property {string} signbox - regular expression for a signbox marker, max coordinate and zero or more spatial symbols
50 * @property {string} sign - regular expression for an optional prefix followed by a signbox
51 * @property {string} sortable - regular expression for a mandatory prefix followed by a signbox
52 */
53 let re = {
54 'symbol': '(?:(?:\uD8C0[\uDC01-\uDFFF])|(?:[\uD8C1-\uD8FC][\uDC00-\uDFFF])|(?:\uD8FD[\uDC00-\uDC80]))',
55 'coord': '(?:\uD836[\uDC0C-\uDDFF]){2}',
56 'sort': '\uD836\uDC00',
57 'box': '\uD836[\uDC01-\uDC04]'
58 };
59 re.prefix = `(?:${re.sort}(?:${re.symbol})+)`;
60 re.spatial = `${re.symbol}${re.coord}`;
61 re.signbox = `${re.box}${re.coord}(?:${re.spatial})*`;
62 re.sign = `${re.prefix}?${re.signbox}`;
63 re.sortable = `${re.prefix}${re.signbox}`;
64
65 /** The convert module contains functions to convert between Formal SignWriitng in ASCII (FSW) and SignWriting in Unicode (SWU) characters, along with other types of data.
66 * [Characters set definitions](https://tools.ietf.org/id/draft-slevinski-formal-signwriting-09.html#name-characters)
67 * @module convert
68 */
69 /**
70 * Function to convert an SWU structural marker to FSW equivalent
71 * @function convert.swu2mark
72 * @param {string} swuMark - character for SWU structural marker
73 * @returns {string} FSW structural marker
74 * @example
75 * convert.swu2mark('𝠀')
76 *
77 * return 'A'
78 */
79
80 const swu2mark = swuMark => {
81 return {
82 '𝠀': 'A',
83 '𝠁': 'B',
84 '𝠂': 'L',
85 '𝠃': 'M',
86 '𝠄': 'R'
87 }[swuMark];
88 };
89 /**
90 * Function to convert an FSW structural marker to SWU equivalent
91 * @function convert.mark2swu
92 * @param {string} fswMark - character for FSW structural marker
93 * @returns {string} SWU structural marker
94 * @example
95 * convert.mark2swu('A')
96 *
97 * return '𝠀'
98 */
99
100
101 const mark2swu = fswMark => {
102 return {
103 'A': '𝠀',
104 'B': '𝠁',
105 'L': '𝠂',
106 'M': '𝠃',
107 'R': '𝠄'
108 }[fswMark];
109 };
110 /**
111 * Function to convert an SWU number character to an integer
112 * @function convert.swu2num
113 * @param {string} swuNum - SWU number character
114 * @returns {number} Integer value for number
115 * @example
116 * convert.swu2num('ðĪ†')
117 *
118 * return 500
119 */
120
121
122 const swu2num = swuNum => parseInt(swuNum.codePointAt(0)) - 0x1D80C + 250;
123 /**
124 * Function to convert a number to an SWU number character
125 * @function convert.num2swu
126 * @param {number} num - Integer value for number
127 * @returns {string} SWU number character
128 * @example
129 * convert.num2swu(500)
130 *
131 * return 'ðĪ†'
132 */
133
134
135 const num2swu = num => String.fromCodePoint(0x1D80C + parseInt(num) - 250);
136 /**
137 * Function to convert two SWU number characters to an array of x,y integers
138 * @function convert.swu2coord
139 * @param {string} swuCoord - Two SWU number character
140 * @returns {number[]} Array of x,y integers
141 * @example
142 * convert.swu2coord('ðĪ†ðĪ†')
143 *
144 * return [500, 500]
145 */
146
147
148 const swu2coord = swuCoord => [swu2num(swuCoord.slice(0, 2)), swu2num(swuCoord.slice(2, 4))];
149 /**
150 * Function to convert an array of x,y integers to two SWU number characters
151 * @function convert.coord2swu
152 * @param {number[]} coord - Array of x,y integers
153 * @returns {string} Two SWU number character
154 * @example
155 * convert.coord2swu([500, 500])
156 *
157 * return 'ðĪ†ðĪ†'
158 */
159
160
161 const coord2swu = coord => coord.map(num => num2swu(num)).join('');
162 /**
163 * Function to convert an FSW coordinate string to an array of x,y integers
164 * @function convert.fsw2coord
165 * @param {string} fswCoord - An FSW coordinate string
166 * @returns {number[]} Array of x,y integers
167 * @example
168 * convert.fsw2coord('500x500')
169 *
170 * return [500, 500]
171 */
172
173
174 const fsw2coord = fswCoord => fswCoord.split('x').map(num => parseInt(num));
175 /**
176 * Function to convert an array of x,y integers to an FSW coordinate string
177 * @function convert.coord2fsw
178 * @param {number[]} coord - Array of x,y integers
179 * @returns {string} An FSW coordinate string
180 * @example
181 * convert.coord2fsw([500, 500])
182 *
183 * return '500x500'
184 */
185
186
187 const coord2fsw = coord => coord.join('x');
188 /**
189 * Function to convert an SWU symbol character to a code point on plane 4
190 * @function convert.swu2code
191 * @param {string} swuSym - SWU symbol character
192 * @returns {number} Code point on plane 4
193 * @example
194 * convert.swu2code('ņ€€')
195 *
196 * return 0x40001
197 */
198
199
200 const swu2code = swuSym => parseInt(swuSym.codePointAt(0));
201 /**
202 * Function to convert a code point on plane 4 to an SWU symbol character
203 * @function convert.code2swu
204 * @param {number} code - Code point on plane 4
205 * @returns {string} SWU symbol character
206 * @example
207 * convert.code2swu(0x40001)
208 *
209 * return 'ņ€€'
210 */
211
212
213 const code2swu = code => String.fromCodePoint(code);
214 /**
215 * Function to convert an SWU symbol character to a 16-bit ID
216 * @function convert.swu2id
217 * @param {string} swuSym - SWU symbol character
218 * @returns {number} 16-bit ID
219 * @example
220 * convert.swu2id('ņ€€')
221 *
222 * return 1
223 */
224
225
226 const swu2id = swuSym => swu2code(swuSym) - 0x40000;
227 /**
228 * Function to convert a 16-bit ID to an SWU symbol character
229 * @function convert.id2swu
230 * @param {number} id - 16-bit ID
231 * @returns {string} SWU symbol character
232 * @example
233 * convert.id2swu(1)
234 *
235 * return 'ņ€€'
236 */
237
238
239 const id2swu = id => code2swu(id + 0x40000);
240 /**
241 * Function to convert an FSW symbol key to a 16-bit ID
242 * @function convert.key2id
243 * @param {string} key - FSW symbol key
244 * @returns {number} 16-bit ID
245 * @example
246 * convert.key2id('S10000')
247 *
248 * return 1
249 */
250
251
252 const key2id = key => 1 + (parseInt(key.slice(1, 4), 16) - 256) * 96 + parseInt(key.slice(4, 5), 16) * 16 + parseInt(key.slice(5, 6), 16);
253 /**
254 * Function to convert a 16-bit ID to an FSW symbol key
255 * @function convert.id2key
256 * @param {number} id - 16-bit ID
257 * @returns {string} FSW symbol key
258 * @example
259 * convert.id2key(1)
260 *
261 * return 'S10000'
262 */
263
264
265 const id2key = id => {
266 const symcode = id - 1;
267 const base = parseInt(symcode / 96);
268 const fill = parseInt((symcode - base * 96) / 16);
269 const rotation = parseInt(symcode - base * 96 - fill * 16);
270 return 'S' + (base + 0x100).toString(16) + fill.toString(16) + rotation.toString(16);
271 };
272 /**
273 * Function to convert an SWU symbol character to an FSW symbol key
274 * @function convert.swu2key
275 * @param {string} swuSym - SWU symbol character
276 * @returns {string} FSW symbol key
277 * @example
278 * convert.swu2key('ņ€€')
279 *
280 * return 'S10000'
281 */
282
283
284 const swu2key = swuSym => {
285 const symcode = swu2code(swuSym) - 0x40001;
286 const base = parseInt(symcode / 96);
287 const fill = parseInt((symcode - base * 96) / 16);
288 const rotation = parseInt(symcode - base * 96 - fill * 16);
289 return 'S' + (base + 0x100).toString(16) + fill.toString(16) + rotation.toString(16);
290 };
291 /**
292 * Function to convert an FSW symbol key to an SWU symbol character
293 * @function convert.key2swu
294 * @param {string} key - FSW symbol key
295 * @returns {string} SWU symbol character
296 * @example
297 * convert.key2swu('S10000')
298 *
299 * return 'ņ€€'
300 */
301
302
303 const key2swu = key => code2swu(0x40001 + (parseInt(key.slice(1, 4), 16) - 256) * 96 + parseInt(key.slice(4, 5), 16) * 16 + parseInt(key.slice(5, 6), 16));
304 /**
305 * Function to convert SWU text to FSW text
306 * @function convert.swu2fsw
307 * @param {string} swuText - SWU text
308 * @returns {string} FSW text
309 * @example
310 * convert.swu2fsw('𝠀ņ€€’ņ€€šņ‹šĨņ‹›Đ𝠃ðĪŸðĪĐņ‹›ĐðĢĩðĪņ€€’ðĪ‡ðĢĪņ‹šĨðĪðĪ†ņ€€šðĢŪðĢ­')
311 *
312 * return 'AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475'
313 */
314
315
316 const swu2fsw = swuText => {
317 if (!swuText) return '';
318 let fsw = swuText.replace(/𝠀/g, "A").replace(/𝠁/g, "B").replace(/𝠂/g, "L").replace(/𝠃/g, "M").replace(/𝠄/g, "R");
319 const syms = fsw.match(new RegExp(re.symbol, 'g'));
320
321 if (syms) {
322 syms.forEach(function (sym) {
323 fsw = fsw.replace(sym, swu2key(sym));
324 });
325 }
326
327 const coords = fsw.match(new RegExp(re.coord, 'g'));
328
329 if (coords) {
330 coords.forEach(function (coord) {
331 fsw = fsw.replace(coord, swu2coord(coord).join('x'));
332 });
333 }
334
335 return fsw;
336 };
337 /**
338 * Function to convert FSW text to SWU text
339 * @function convert.fsw2swu
340 * @param {string} fswText - FSW text
341 * @returns {string} SWU text
342 * @example
343 * convert.fsw2swu('AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S2e704510x500S10019476x475')
344 *
345 * return '𝠀ņ€€’ņ€€šņ‹šĨņ‹›Đ𝠃ðĪŸðĪĐņ‹›ĐðĢĩðĪņ€€’ðĪ‡ðĢĪņ‹šĨðĪðĪ†ņ€€šðĢŪðĢ­'
346 */
347
348
349 const fsw2swu = fswText => {
350 if (!fswText) return '';
351 const prefixes = fswText.match(new RegExp(re$1.prefix, 'g'));
352
353 if (prefixes) {
354 prefixes.forEach(function (prefix) {
355 fswText = fswText.replace(prefix, '𝠀' + prefix.slice(1).match(/.{6}/g).map(key => key2swu(key)).join(''));
356 });
357 }
358
359 const boxes = fswText.match(new RegExp(re$1.box + re$1.coord, 'g'));
360
361 if (boxes) {
362 boxes.forEach(function (boxes) {
363 fswText = fswText.replace(boxes, mark2swu(boxes.slice(0, 1)) + coord2swu(fsw2coord(boxes.slice(1, 8))));
364 });
365 }
366
367 const spatials = fswText.match(new RegExp(re$1.spatial, 'g'));
368
369 if (spatials) {
370 spatials.forEach(function (spatial) {
371 fswText = fswText.replace(spatial, key2swu(spatial.slice(0, 6)) + coord2swu(fsw2coord(spatial.slice(6, 13))));
372 });
373 }
374
375 return fswText;
376 };
377
378 exports.code2swu = code2swu;
379 exports.coord2fsw = coord2fsw;
380 exports.coord2swu = coord2swu;
381 exports.fsw2coord = fsw2coord;
382 exports.fsw2swu = fsw2swu;
383 exports.id2key = id2key;
384 exports.id2swu = id2swu;
385 exports.key2id = key2id;
386 exports.key2swu = key2swu;
387 exports.mark2swu = mark2swu;
388 exports.num2swu = num2swu;
389 exports.swu2code = swu2code;
390 exports.swu2coord = swu2coord;
391 exports.swu2fsw = swu2fsw;
392 exports.swu2id = swu2id;
393 exports.swu2key = swu2key;
394 exports.swu2mark = swu2mark;
395 exports.swu2num = swu2num;
396
397 Object.defineProperty(exports, '__esModule', { value: true });
398
399}));
400
401/* support ongoing development on https://patreon.com/signwriting */