UNPKG

@sutton-signwriting/core

Version:

a javascript package for node and browsers that supports general processing of the Sutton SignWriting script

3 lines (2 loc) 7.83 kB
/* Sutton SignWriting Core Module v2.0.1 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});let e={null:"S00000",base:"[123][0-9a-f]{2}",coord:"(?:[0-9]{3}x[0-9]{3})?",var:"V[0-9]+"};e.symbol=`S${e.base}[0-5u][0-9a-fu]`,e.nullorsymbol=`(?:${e.null}|${e.symbol})`,e.range=`R${e.base}t${e.base}`,e.item=`(?:${e.null}|${e.symbol}|${e.range})`,e.list=`${e.item}(?:o${e.item})*`,e.prefix=`(?:A(?:${e.list})+)?T`,e.signbox=`(?:${e.list}${e.coord})*`,e.full=`Q(${e.prefix})?(${e.signbox})?(${e.var})?(-?)`;let r={null:"S00000",symbol:"S[123][0-9a-f]{2}[0-5][0-9a-f]",coord:"[0-9]{3}x[0-9]{3}",sort:"A",box:"[BLMR]"};r.nullorsymbol=`(?:${r.null}|${r.symbol})`,r.prefix=`(?:${r.sort}${r.nullorsymbol}+)`,r.spatial=`${r.symbol}${r.coord}`,r.signbox=`${r.box}${r.coord}(?:${r.spatial})*`,r.sign=`${r.prefix}?${r.signbox}`,r.sortable=`${r.prefix}${r.signbox}`;const n=e=>e.split("x").map((e=>parseInt(e))),o=r=>r.match(new RegExp(`(${e.list}${e.coord})`,"g")).map((e=>{let r,o;return e.includes("x")?(r=n(e.slice(-7)),o=e.slice(0,-7)):o=e,o.includes("o")?{or:o.split("o").map((e=>e.includes("S")?e:e.slice(1).split("t"))),coord:r,coord:r}:o.includes("S")?{symbol:o,coord:r}:{range:o.slice(1).split("t"),coord:r}}));let i={colorize:"C",colorhex:"(?:[0-9a-fA-F]{3}){1,2}",colorname:"[a-zA-Z]+",padding:"P[0-9]{2}",zoom:"Z(?:[0-9]+(?:\\.[0-9]+)?|x)",classbase:"-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}",id:"[a-zA-Z][_a-zA-Z0-9-]{0,100}"};i.colorbase=`(?:${i.colorhex}|${i.colorname})`,i.color=`_${i.colorbase}_`,i.colors=`_${i.colorbase}(?:,${i.colorbase})?_`,i.background=`G${i.color}`,i.detail=`D${i.colors}`,i.detailsym=`D[0-9]{2}${i.colors}`,i.classes=`${i.classbase}(?: ${i.classbase})*`,i.full=`-(${i.colorize})?(${i.padding})?(${i.background})?(${i.detail})?(${i.zoom})?(?:-((?:${i.detailsym})*))?(?:-(${i.classes})?!(?:(${i.id})!)?)?`;const s=e=>{const o=`^(${r.prefix})?(${r.signbox})(${i.full})?`,s="string"==typeof e?e.match(new RegExp(o)):void 0;return s?{sequence:s[1]?s[1].slice(1).match(/.{6}/g):void 0,box:s[2][0],max:n(s[2].slice(1,8)),spatials:s[2].length<9?void 0:s[2].slice(8).match(/(.{13})/g).map((e=>({symbol:e.slice(0,6),coord:[parseInt(e.slice(6,9)),parseInt(e.slice(10,13))]}))),style:s[3]}:{}},t=(e,r,n)=>{const o=n||!1;e="number"==typeof e?e.toString().padStart(3,"0"):("000"+e).slice(-3),r="number"==typeof r?r.toString().padStart(3,"0"):""+r;const i=e[0],s=e[1],t=e[2],a=r[0],c=r[1],f=r[2],u=o?"0123456789abcdef":"0123456789",p=u.indexOf(i),$=u.indexOf(a);if(p>$)throw new Error("Start is greater than end");if(i===a){const e=function(e,r,n){const o=e[0],i=e[1],s=r[0],t=r[1],a=n?"0123456789abcdef":"0123456789",c=a.indexOf(o),f=a.indexOf(s);if(c<f){const e=[`${o}${l(i,a[a.length-1],n)}`],r=a.slice(c+1,f);return r.length>0&&e.push(l(r[0],r[r.length-1],n)+(n?"[0-9a-f]":"[0-9]")),e.push(`${s}${l(a[0],t,n)}`),e.join("|")}return`${o}${l(i,t,n)}`}(s+t,c+f,o);return e.includes("|")?`${i}(?:${e})`:`${i}${e}`}{const e=[],r=function(e,r,n){const o=n?"0123456789abcdef":"0123456789",i=o[o.length-1],s=o.indexOf(e);if(r===o[0])return l(e,i,n)+(n?"[0-9a-f]":"[0-9]");{const t=l(r,i,n),a=s+1<o.length?o[s+1]:null;if(a){return e+t+"|"+l(a,i,n)+(n?"[0-9a-f]":"[0-9]")}return e+t}}(s,t,o);if(e.push(r.includes("|")?`${i}(?:${r})`:`${i}${r}`),p+1<$){const r=u.slice(p+1,$),n=l(r[0],r[r.length-1],o),i=o?"[0-9a-f]":"[0-9]";e.push(n+i+i)}const n=function(e,r,n){const o=n?"0123456789abcdef":"0123456789",i=o[0],s=o.indexOf(e);if(s>0){return l(i,o[s-1],n)+(n?"[0-9a-f]":"[0-9]")+"|"+e+l(i,r,n)}return e+l(i,r,n)}(c,f,o);return e.push(n.includes("|")?`${a}(?:${n})`:`${a}${n}`),`(?:${e.join("|")})`}};function l(e,r,n){if(e===r)return e;if(!n)return`[${e}-${r}]`;const o="0123456789abcdef",i=o.indexOf(e),s=o.indexOf(r),t=o.slice(i,s+1);if(t.includes("9")&&t.includes("a")){const e=t.match(/[0-9]+/)[0],r=t.match(/[a-f]+/)[0];return`[${e[0]}-${e.slice(-1)}${r[0]}-${r.slice(-1)}]`}return`[${t[0]}-${t.slice(-1)}]`}const a=e=>{let r=e.slice(0,4),n=e.slice(4,5);r+="u"==n?"[0-5]":n;let o=e.slice(5,6);return r+="u"==o?"[0-9a-f]":o,r},c=e=>{let r=e.slice(1,4),n=e.slice(5,8);return"S"+t(r,n,"hex")+"[0-5][0-9a-f]"},f=o=>{if(!(o=o.match(new RegExp(`^${e.full}`))[0]))return"";var s,l,f,u,p,$,g,x,d,m,b,h=20,y="(?:"+i.full+")?";if("Q"==o)return[r.prefix+"?"+r.signbox];if("Q-"==o)return[r.prefix+"?"+r.signbox+y];if("QT"==o)return[r.prefix+r.signbox];if("QT-"==o)return[r.prefix+r.signbox+y];var A=[],v=o.indexOf("T")+1;if(v){b="(?:A";var R=o.slice(0,v);if(o=o.replace(R,""),"QT"==R)b+=r.nullorsymbol+"+)";else if(s=R.match(new RegExp(e.list,"g"))){for(p=0;p<s.length;p+=1)if(u=[],l=s[p].match(new RegExp(e.item,"g"))){for($=0;$<l.length;$+=1)(f=l[$].match(new RegExp(e.nullorsymbol)))?u.push(a(f[0])):u.push(c(l[$]));1==u.length?b+=u[0]:b+="(?:"+u.join("|")+")"}b+=r.nullorsymbol+"*)"}}if((s=o.match(new RegExp(e.var,"g")))&&(h=1*s.toString().slice(1)),s=o.match(new RegExp(e.list+e.coord,"g")))for(p=0;p<s.length;p+=1){if(u=[],l=s[p].match(new RegExp("("+e.symbol+"|"+e.range+")","g"))){for($=0;$<l.length;$+=1)(f=l[$].match(new RegExp(e.symbol)))?u.push(a(f[0])):u.push(c(l[$]));g=1==u.length?u[0]:"(?:"+u.join("|")+")"}s[p].includes("x")?(d=(x=n(s[p].slice(-7)))[0],m=x[1],g+=t(d-h,d+h),g+="x",g+=t(m-h,m+h)):g+=r.coord,g=r.signbox+g+"(?:"+r.symbol+r.coord+")*",g=v?b+g:r.prefix+"?"+g,o.indexOf("-")>0&&(g+=y),A.push(g)}return A.length||(o.indexOf("-")>0&&(g+=y),A.push(b+r.signbox)),A};exports.compose=r=>{if(!r||!r.query)return;let n="Q";return r.prefix&&r.prefix.required&&(Array.isArray(r.prefix.parts)&&(n+="A",n+=r.prefix.parts.map((e=>"string"==typeof e?e:Array.isArray(e)&&2==e.length?`R${e[0]}t${e[1]}`:Array.isArray(e)&&e.length>2&&"or"==e[0]?(e.shift(),e.map((e=>"string"==typeof e?e:Array.isArray(e)&&2==e.length?`R${e[0]}t${e[1]}`:void 0)).join("o")):void 0)).join("")),n+="T"),Array.isArray(r.signbox)&&(n+=r.signbox.map((e=>{let r;return e.or?r=e.or.map((e=>"string"==typeof e?e:Array.isArray(e)&&2==e.length?`R${e[0]}t${e[1]}`:void 0)).join("o"):e.symbol?r=e.symbol:e.range&&Array.isArray(e.range)&&2==e.range.length&&(r=`R${e.range[0]}t${e.range[1]}`),r+(Array.isArray(e.coord)&&2==e.coord.length?e.coord.join("x"):"")})).join("")),r.variance&&(n+="V"+r.variance),n+=r.style?"-":"",n=n.match(new RegExp(`^${e.full}`))[0],n},exports.fsw2query=(e,r)=>{let n="";const o=s(e);if(o.box){const e=r.indexOf("A")>-1,i=r.indexOf("a")>-1,s=r.indexOf("S")>-1,t=r.indexOf("s")>-1,l=r.indexOf("L")>-1;return(e||i||s||t)&&((e||i)&&o.sequence&&(n+="A",n+=o.sequence.map((e=>e.slice(0,4)+(i?"uu":e.slice(4,6)))).join(""),n+="T"),(s||t)&&o.spatials&&(n+=o.spatials.map((e=>e.symbol.slice(0,4)+(t?"uu":e.symbol.slice(4,6))+(l?e.coord.join("x"):""))).join(""))),n?"Q"+n:void 0}},exports.lines=(e,r)=>{if(!r)return[];let n,o,i,s,t,l=f(e);if(!l)return[];for(t=0;t<l.length;t+=1)n=l[t],n="^"+n+".*",o=r.match(new RegExp(n,"mg")),r=o?o.join("\n"):"";return r?(i=r.split("\n"),s=i.filter((function(e){return!(e in i)&&(i[e]=!0)}),{})):s=[],s},exports.parse=r=>{const n="string"==typeof r?r.match(new RegExp(`^${e.full}`)):void 0;return{query:!!n||void 0,prefix:n&&n[1]?(i=n[1],{required:!0,parts:"T"==i?void 0:i.match(new RegExp(`${e.list}`,"g")).map((r=>r.includes("o")?["or"].concat(r.match(new RegExp(`(${e.item})`,"g")).map((e=>"S"==e[0]?e:e.slice(1).split("t")))):"S"==r[0]?r:r.slice(1).split("t")))}):void 0,signbox:n&&n[2]?o(n[2]):void 0,variance:n&&n[3]?parseInt(n[3].slice(1)):void 0,style:!(!n||!n[4])||void 0};var i},exports.range=t,exports.re=e,exports.regex=f,exports.results=(e,r)=>{if(!r)return[];let n,o,i,s,t,l=f(e);if(!l)return[];for(t=0;t<l.length;t+=1)n=l[t],o=r.match(new RegExp(n,"g")),r=o?o.join(" "):"";return r?(i=r.split(" "),s=i.filter((function(e){return!(e in i)&&(i[e]=!0)}),{})):s=[],s};