UNPKG

1.23 kBJavaScriptView Raw
1
2const { db } = require('../db/db');
3const { swu, convert } = require('@sutton-signwriting/core');
4
5const blank = null;
6
7/**
8 * Function that normalizes a symbol with a minimum coordinate for a center of 500,500
9 * @function swu.symbolNormalize
10 * @param {string} swuSym - an SWU symbol with optional coordinate and style string
11 * @returns {string} normalized symbol
12 * @example
13 * // using promise.then
14 * swu.symbolNormalize('񆇡-C').then( norm => {
15 * console.log(norm)
16 * })
17 * @example
18 * // using async/await
19 * const norm = await swu.symbolNormalize('񆇡-C')
20 */
21
22const symbolNormalize = async (swuSym) => {
23 const blank = '';
24 const parsed = swu.parse.symbol(swuSym);
25 if (!parsed.symbol) return blank;
26
27 const res = await db.query('select width,height from symbol where id=?', [convert.swu2id(parsed.symbol)]);
28 const sym = res[0]
29 if (!sym) return blank;
30
31 return `${parsed.symbol}${convert.coord2swu([500 - parseInt( (sym.width+1) / 2), 500 - parseInt( (sym.height+1) / 2)])}${parsed.style || ''}`;
32}
33
34if (require.main === module) {
35 symbolNormalize(process.argv[2]).then( res => {
36 console.log(res);
37 })
38} else {
39 module.exports = { symbolNormalize }
40}