1 |
|
2 | import * as style from '../style';
|
3 | import { swu2coord } from '../convert';
|
4 | import { re } from './swu-re';
|
5 |
|
6 | const parse = {
|
7 | /**
|
8 | * Function to parse an swu symbol with optional coordinate and style string
|
9 | * @function swu.parse.symbol
|
10 | * @param {string} swuSym - an swu symbol
|
11 | * @returns {object} elements of swu symbol
|
12 | * @example
|
13 | * swu.parse.symbol('ņðĪðĪ-C')
|
14 | *
|
15 | * return {
|
16 | * 'symbol': 'ņ',
|
17 | * 'coord': [500, 500],
|
18 | * 'style': '-C'
|
19 | * }
|
20 | */
|
21 | symbol: (swuSym) => {
|
22 | const regex = `^(${re.symbol})(${re.coord})?(${style.re.full})?`;
|
23 | const symbol = (typeof swuSym === 'string') ? swuSym.match(new RegExp(regex)) : undefined;
|
24 | return {
|
25 | 'symbol': (symbol ? symbol[1] : undefined),
|
26 | 'coord': (symbol && symbol[2] ? swu2coord(symbol[2]) : undefined),
|
27 | 'style': (symbol ? symbol[3] : undefined)
|
28 | };
|
29 | },
|
30 | /**
|
31 | * Function to parse an swu sign with style string
|
32 | * @function swu.parse.sign
|
33 | * @param {string} swuSign - an swu sign
|
34 | * @returns {object} elements of swu sign
|
35 | * @example
|
36 | * swu.parse.sign('ð ņņņĨņĐð ðĪðĪĐņĐðĢĩðĪņðĪðĢĪņĨðĪðĪņðĢŪðĢ-C')
|
37 | *
|
38 | * return {
|
39 | * sequence: ['ņ','ņ','ņĨ','ņĐ''],
|
40 | * box: 'ð ',
|
41 | * max: [525, 535],
|
42 | * spatials: [
|
43 | * {
|
44 | * symbol: 'ņĐ',
|
45 | * coord: [483, 510]
|
46 | * },
|
47 | * {
|
48 | * symbol: 'ņ',
|
49 | * coord: [501, 466]
|
50 | * },
|
51 | * {
|
52 | * symbol: 'ņĨ',
|
53 | * coord: [510, 500]
|
54 | * },
|
55 | * {
|
56 | * symbol: 'ņ',
|
57 | * coord: [476, 475]
|
58 | * }
|
59 | * ],
|
60 | * style: '-C'
|
61 | * }
|
62 | */ sign: (swuSign) => {
|
63 | const regex = `^(${re.prefix})?(${re.signbox})(${style.re.full})?`;
|
64 | const sign = (typeof swuSign === 'string') ? swuSign.match(new RegExp(regex)) : undefined;
|
65 | if (sign) {
|
66 | return {
|
67 | 'sequence': (sign[1] ? sign[1].slice(2).match(/.{2}/g) : undefined),
|
68 | 'box': sign[2].slice(0, 2),
|
69 | 'max': swu2coord(sign[2].slice(2, 6)),
|
70 | 'spatials': sign[2].length < 7 ? undefined : sign[2].slice(6).match(/(.{6})/g).map(m => {
|
71 | return {
|
72 | symbol: m.slice(0, 2),
|
73 | coord: swu2coord(m.slice(2))
|
74 | }
|
75 | }),
|
76 | 'style': sign[3]
|
77 | };
|
78 | } else {
|
79 | return {};
|
80 | }
|
81 | }
|
82 | }
|
83 |
|
84 | /**
|
85 | * Function to encode SWU characters using the UTF-16 escape format.
|
86 | * @function swu.encode
|
87 | * @param {string} swu - SWU characters
|
88 | * @returns {string} UTF-16 escape format
|
89 | * @example
|
90 | * swu.encode('ņðĪðĪ')
|
91 | *
|
92 | * return '\\uD8C0\\uDC01\\uD836\\uDD06\\uD836\\uDD06'
|
93 | */
|
94 | const encode = (text) => text.replace(/[\u007F-\uFFFF]/g, function (chr) {
|
95 | return "\\u" + ("0000" + chr.charCodeAt(0).toString(16)).substr(-4).toUpperCase();
|
96 | });
|
97 |
|
98 | /**
|
99 | * Function to decode UTF-16 escape format to SWU characters.
|
100 | * @function swu.decode
|
101 | * @param {string} encoded - UTF-16 escape format
|
102 | * @returns {string} SWU characters
|
103 | * @example
|
104 | * swu.decode('\\uD8C0\\uDC01\\uD836\\uDD06\\uD836\\uDD06')
|
105 | *
|
106 | * return 'ņðĪðĪ'
|
107 | */
|
108 | const decode = (encoded) => encoded.replace(/\\u([0-9A-F]{4})/g, function (match, chr) {
|
109 | return String.fromCharCode(parseInt(chr, 16));
|
110 | });
|
111 |
|
112 | /**
|
113 | * Function to decompose an SWU character into UTF-16 surrogate pairs.
|
114 | * @function swu.pair
|
115 | * @param {string} swuChar - an SWU character
|
116 | * @returns {string[]} an array of UTF-16 surrogate pairs
|
117 | * @example
|
118 | * swu.pair('ņ')
|
119 | *
|
120 | * return ['D8C0', 'DC01']
|
121 | */
|
122 | const pair = (swuChar) => [swuChar.charCodeAt(0).toString(16).toUpperCase(), swuChar.charCodeAt(1).toString(16).toUpperCase()];
|
123 |
|
124 | export { parse, encode, decode, pair }
|