UNPKG

2.63 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.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 = global || self, factory((global.ssw = global.ssw || {}, global.ssw.unicode8 = global.ssw.unicode8 || {}, global.ssw.unicode8.symbol = {})));
11}(this, (function (exports) { 'use strict';
12
13 /**
14 * Object of regular expressions for symbol strings
15 *
16 * { base, fill, rotation, full }
17 * @alias symbol.re
18 * @type {object}
19 */
20 let re = {
21 'base': '(?:\uD836[\uDC00-\uDE8B])',
22 'fill': '(?:\uD836[\uDE9B-\uDE9F])',
23 'rotation': '(?:\uD836[\uDEA1-\uDEAF])'
24 };
25 re.full = `(${re.base})(${re.fill})?(${re.rotation})?`;
26
27 /**
28 * Function to parse symbol string to object
29 * @function symbol.parse
30 * @param {string} symbolString - a symbol string
31 * @returns {object} elements of symbol string
32 * @example
33 * symbol.parse('𝠀')
34 *
35 * return {
36 * 'base': '𝠀',
37 * 'fill': undefined,
38 * 'rotation': undefined
39 * }
40 */
41
42 const parse = symbolString => {
43 const regex = `^${re.full}`;
44 const m = (typeof symbolString === 'string' ? symbolString.match(new RegExp(regex)) : []) || [];
45 return {
46 'base': !m[1] ? undefined : m[1],
47 'fill': !m[2] ? undefined : m[2],
48 'rotation': !m[3] ? undefined : m[3]
49 };
50 };
51
52 /**
53 * Function to compose symbol string from object
54 * @function symbol.compose
55 * @param {object} symbolObject - an object of symbol parts
56 * @param {string} symbolObject.base - base character for symbol
57 * @param {string} symbolObject.fill - fill character for symbol
58 * @param {string} symbolObject.rotation - rotation character for symbol
59 * @returns {string} symbol string
60 * @example
61 * symbol.compose({
62 * 'base': '𝠀'
63 * })
64 *
65 * return '𝠀'
66 */
67
68 const compose = symbolObject => {
69 if (typeof symbolObject !== 'object' || symbolObject === null) return undefined;
70 const sym = (symbolObject.base ? symbolObject.base : '') + (symbolObject.fill ? symbolObject.fill : '') + (symbolObject.rotation ? symbolObject.rotation : '');
71 return sym ? sym : undefined;
72 };
73
74 exports.compose = compose;
75 exports.parse = parse;
76 exports.re = re;
77
78 Object.defineProperty(exports, '__esModule', { value: true });
79
80})));
81
82/* support ongoing development on https://patreon.com/signwriting */