UNPKG

@sutton-signwriting/unicode8

Version:

a javascript package for processing SignWriting in Unicode 8 (uni8) characters

84 lines (75 loc) 2.71 kB
/** * Sutton SignWriting Unicode 8 Module v1.2.0 (https://github.com/sutton-signwriting/unicode8) * Author: Steve Slevinski (https://SteveSlevinski.me) * Sponsor: https://patreon.com/signwriting * Donate: https://donate.sutton-signwriting.io * * symbol.js is released under the MIT License. */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.ssw = global.ssw || {}, global.ssw.unicode8 = global.ssw.unicode8 || {}, global.ssw.unicode8.symbol = {}))); })(this, (function (exports) { 'use strict'; /** * Object of regular expressions for symbol strings * * { base, fill, rotation, full } * @alias symbol.re * @type {object} */ let re = { 'base': '(?:\uD836[\uDC00-\uDE8B])', 'fill': '(?:\uD836[\uDE9B-\uDE9F])', 'rotation': '(?:\uD836[\uDEA1-\uDEAF])' }; re.full = `(${re.base})(${re.fill})?(${re.rotation})?`; /** * Function to parse symbol string to object * @function symbol.parse * @param {string} symbolString - a symbol string * @returns {object} elements of symbol string * @example * symbol.parse('𝠀') * * return { * 'base': '𝠀', * 'fill': undefined, * 'rotation': undefined * } */ const parse = symbolString => { const regex = `^${re.full}`; const m = (typeof symbolString === 'string' ? symbolString.match(new RegExp(regex)) : []) || []; return { 'base': !m[1] ? undefined : m[1], 'fill': !m[2] ? undefined : m[2], 'rotation': !m[3] ? undefined : m[3] }; }; /** * Function to compose symbol string from object * @function symbol.compose * @param {object} symbolObject - an object of symbol parts * @param {string} symbolObject.base - base character for symbol * @param {string} symbolObject.fill - fill character for symbol * @param {string} symbolObject.rotation - rotation character for symbol * @returns {string} symbol string * @example * symbol.compose({ * 'base': '𝠀' * }) * * return '𝠀' */ const compose = symbolObject => { if (typeof symbolObject !== 'object' || symbolObject === null) return undefined; const sym = (symbolObject.base ? symbolObject.base : '') + (symbolObject.fill ? symbolObject.fill : '') + (symbolObject.rotation ? symbolObject.rotation : ''); return sym ? sym : undefined; }; exports.compose = compose; exports.parse = parse; exports.re = re; Object.defineProperty(exports, '__esModule', { value: true }); })); /* the end */