@sutton-signwriting/unicode8
Version:
a javascript package for processing SignWriting in Unicode 8 (uni8) characters
186 lines (170 loc) • 6.09 kB
JavaScript
/**
* 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
*
* convert.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.convert = {})));
})(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]
};
};
/** The convert module contains functions to help process the various SignWriting Character sets.
* @module convert
*/
/**
* Function to convert a SignWriting in Unicode 8 (uni8) code point to a character
* @function convert.code2uni
* @param {integer} code - unicode code point
* @returns {string} SignWriting in Unicode 8 character
* @example
* convert.code2uni(0x1D800)
*
* return '𝠀'
*/
const code2uni = code => String.fromCharCode(0xD800 + (code - 0x10000 >> 10), 0xDC00 + (code - 0x10000 & 0x3FF));
/**
* Function to convert a SignWriting in Unicode 8 (uni8) character to a code point
* @function convert.uni2code
* @param {string} uni8 - SignWriting in Unicode 8 character
* @returns {integer} unicode code point
* @example
* convert.uni2code('𝠀')
*
* return 0x1D800
*/
const uni2code = uni8 => uni8.codePointAt(0);
/**
* Function to convert a SignWriting in Unicode 8 (uni8) character to hex values
* @function convert.uni2hex
* @param {string} uni8 - SignWriting in Unicode 8 character
* @returns {string} hex value of unicode character
* @example
* convert.uni2hex('𝠀')
*
* return "1D800"
*/
const uni2hex = uni8 => uni8.codePointAt(0).toString(16).toUpperCase();
/**
* Function to convert a SignWriting in Unicode 8 (uni8) symbol to Formal SignWriting in ASCII (FSW)
* @function convert.uni2fsw
* @param {string} uni8 - SignWriting in Unicode 8 character(s)
* @returns {string} an FSW symbol key
* @example
* convert.uni2fsw('𝠀')
*
* return 'S10000'
*/
const uni2fsw = uni8 => {
let sym = parse(uni8);
if (sym.base) {
sym.base = sym.base.codePointAt(0) - 0x1D700;
sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0;
sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0;
return "S" + sym.base.toString(16) + sym.fill.toString(16) + sym.rotation.toString(16);
} else {
return undefined;
}
};
/**
* Function to convert a SignWriting in Unicode 8 (uni8) symbol to SignWriting in Unicode (SWU)
* @function convert.uni2swu
* @param {string} uni8 - SignWriting in Unicode 8 character(s)
* @returns {string} an SWU symbol
* @example
* convert.uni2swu('𝠀')
*
* return ''
*/
const uni2swu = uni8 => {
let sym = parse(uni8);
if (sym.base) {
sym.base = sym.base.codePointAt(0) - 0x1D800;
sym.fill = sym.fill ? sym.fill.codePointAt(0) - 0x1DA9A : 0;
sym.rotation = sym.rotation ? sym.rotation.codePointAt(0) - 0x1DAA0 : 0;
return code2uni(0x40001 + sym.base * 96 + sym.fill * 16 + sym.rotation);
} else {
return undefined;
}
};
/**
* Function to convert a Formal SignWriting in ASCII (FSW) to SignWriting in Unicode 8 (uni8)
* @function convert.fsw2uni
* @param {string} fswSym - an FSW symbol key
* @returns {string} SignWriting in Unicode 8 character(s)
* @example
* convert.fsw2uni('S10000')
*
* return '𝠀'
*/
const fsw2uni = fswSym => {
let base = parseInt(fswSym.slice(1, 4), 16);
let fill = parseInt(fswSym.slice(4, 5), 16);
let rotation = parseInt(fswSym.slice(5, 6), 16);
return code2uni(base + 0x1D700) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : '');
};
/**
* Function to convert a SignWriting in Unicode (SWU) to SignWriting in Unicode 8 (uni8)
* @function convert.swu2uni
* @param {string} swuSym - an SWU symbol
* @returns {string} SignWriting in Unicode 8 character(s)
* @example
* convert.swu2uni('')
*
* return '𝠀'
*/
const swu2uni = swuSym => {
const symcode = swuSym.codePointAt(0) - 0x40001;
const base = parseInt(symcode / 96);
const fill = parseInt((symcode - base * 96) / 16);
const rotation = parseInt(symcode - base * 96 - fill * 16);
return code2uni(base + 0x1D800) + (fill ? code2uni(fill + 0x1DA9A) : '') + (rotation ? code2uni(rotation + 0x1DAA0) : '');
};
exports.code2uni = code2uni;
exports.fsw2uni = fsw2uni;
exports.swu2uni = swu2uni;
exports.uni2code = uni2code;
exports.uni2fsw = uni2fsw;
exports.uni2hex = uni2hex;
exports.uni2swu = uni2swu;
Object.defineProperty(exports, '__esModule', { value: true });
}));
/* the end */