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