UNPKG

4.56 kBJavaScriptView Raw
1
2import * as style from '../style';
3import { swu2coord } from '../convert';
4import { re } from './swu-re';
5
6const 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 * Function to parse an swu text
84 * @function swu.parse.text
85 * @param {string} swuText - an swu text
86 * @returns {array} swu signs and punctuations
87 * @example
88 * swu.parse.text('𝠀ńČĄńˆ©§đ ƒđ€˜đ€ŁńČĄđŁłđŁ©ńˆ©§đ€‰đŁ» 𝠀ńƒŠąńƒŠ«ń‹›•ń†‡Ąđ ƒđ€˜đ€§ńƒŠ«đŁ»đ€•ńƒŠąđŁŽđŁŒń†‡Ąđ€Žđ€‚ń‹›•đ€†đŁŠ ńŒđŁąđ€‚')
89 *
90 * return [
91 * '𝠀ńČĄńˆ©§đ ƒđ€˜đ€ŁńČĄđŁłđŁ©ńˆ©§đ€‰đŁ»',
92 * '𝠀ńƒŠąńƒŠ«ń‹›•ń†‡Ąđ ƒđ€˜đ€§ńƒŠ«đŁ»đ€•ńƒŠąđŁŽđŁŒń†‡Ąđ€Žđ€‚ń‹›•đ€†đŁŠ',
93 * 'ńŒđŁąđ€‚'
94 * ]
95 */
96 text: (swuText) => {
97 if (typeof swuText !== 'string') return [];
98 const regex = `(${re.sign}(${style.re.full})?|${re.spatial}(${style.re.full})?)`;
99 const matches = swuText.match(new RegExp(regex,'g'))
100 return matches?[...matches]:[]
101 }
102}
103
104/**
105 * Function to encode SWU characters using the UTF-16 escape format.
106 * @function swu.encode
107 * @param {string} swu - SWU characters
108 * @returns {string} UTF-16 escape format
109 * @example
110 * swu.encode('ń€€đ€†đ€†')
111 *
112 * return '\\uD8C0\\uDC01\\uD836\\uDD06\\uD836\\uDD06'
113 */
114const encode = (text) => text.replace(/[\u007F-\uFFFF]/g, function (chr) {
115 return "\\u" + ("0000" + chr.charCodeAt(0).toString(16)).substr(-4).toUpperCase();
116});
117
118/**
119 * Function to decode UTF-16 escape format to SWU characters.
120 * @function swu.decode
121 * @param {string} encoded - UTF-16 escape format
122 * @returns {string} SWU characters
123 * @example
124 * swu.decode('\\uD8C0\\uDC01\\uD836\\uDD06\\uD836\\uDD06')
125 *
126 * return 'ń€€đ€†đ€†'
127 */
128const decode = (encoded) => encoded.replace(/\\u([0-9A-F]{4})/g, function (match, chr) {
129 return String.fromCharCode(parseInt(chr, 16));
130});
131
132/**
133 * Function to decompose an SWU character into UTF-16 surrogate pairs.
134 * @function swu.pair
135 * @param {string} swuChar - an SWU character
136 * @returns {string[]} an array of UTF-16 surrogate pairs
137 * @example
138 * swu.pair('񀀁')
139 *
140 * return ['D8C0', 'DC01']
141 */
142const pair = (swuChar) => [swuChar.charCodeAt(0).toString(16).toUpperCase(), swuChar.charCodeAt(1).toString(16).toUpperCase()];
143
144export { parse, encode, decode, pair }