UNPKG

2.02 kBJavaScriptView Raw
1/**
2* Sutton SignWriting Unicode 8 Module v1.1.2 (https://github.com/sutton-signwriting/unicode8)
3* Author: Steve Slevinski (https://SteveSlevinski.me)
4* symbol.mjs is released under the MIT License.
5*/
6
7/**
8 * Object of regular expressions for symbol strings
9 *
10 * { base, fill, rotation, full }
11 * @alias symbol.re
12 * @type {object}
13 */
14let re = {
15 'base': '(?:\uD836[\uDC00-\uDE8B])',
16 'fill': '(?:\uD836[\uDE9B-\uDE9F])',
17 'rotation': '(?:\uD836[\uDEA1-\uDEAF])'
18};
19re.full = `(${re.base})(${re.fill})?(${re.rotation})?`;
20
21/**
22 * Function to parse symbol string to object
23 * @function symbol.parse
24 * @param {string} symbolString - a symbol string
25 * @returns {object} elements of symbol string
26 * @example
27 * symbol.parse('𝠀')
28 *
29 * return {
30 * 'base': '𝠀',
31 * 'fill': undefined,
32 * 'rotation': undefined
33 * }
34 */
35
36const parse = symbolString => {
37 const regex = `^${re.full}`;
38 const m = (typeof symbolString === 'string' ? symbolString.match(new RegExp(regex)) : []) || [];
39 return {
40 'base': !m[1] ? undefined : m[1],
41 'fill': !m[2] ? undefined : m[2],
42 'rotation': !m[3] ? undefined : m[3]
43 };
44};
45
46/**
47 * Function to compose symbol string from object
48 * @function symbol.compose
49 * @param {object} symbolObject - an object of symbol parts
50 * @param {string} symbolObject.base - base character for symbol
51 * @param {string} symbolObject.fill - fill character for symbol
52 * @param {string} symbolObject.rotation - rotation character for symbol
53 * @returns {string} symbol string
54 * @example
55 * symbol.compose({
56 * 'base': '𝠀'
57 * })
58 *
59 * return '𝠀'
60 */
61
62const compose = symbolObject => {
63 if (typeof symbolObject !== 'object' || symbolObject === null) return undefined;
64 const sym = (symbolObject.base ? symbolObject.base : '') + (symbolObject.fill ? symbolObject.fill : '') + (symbolObject.rotation ? symbolObject.rotation : '');
65 return sym ? sym : undefined;
66};
67
68export { compose, parse, re };
69
70/* support ongoing development on https://patreon.com/signwriting */