@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.75 kB
JavaScript
/* Sutton SignWriting Core Module v2.0.1 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */
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))),i=r=>r.match(new RegExp(`(${e.list}${e.coord})`,"g")).map((e=>{let r,i;return e.includes("x")?(r=n(e.slice(-7)),i=e.slice(0,-7)):i=e,i.includes("o")?{or:i.split("o").map((e=>e.includes("S")?e:e.slice(1).split("t"))),coord:r,coord:r}:i.includes("S")?{symbol:i,coord:r}:{range:i.slice(1).split("t"),coord:r}})),o=r=>{const n="string"==typeof r?r.match(new RegExp(`^${e.full}`)):void 0;return{query:!!n||void 0,prefix:n&&n[1]?(o=n[1],{required:!0,parts:"T"==o?void 0:o.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]?i(n[2]):void 0,variance:n&&n[3]?parseInt(n[3].slice(1)):void 0,style:!(!n||!n[4])||void 0};var o},t=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};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 l=e=>{const i=`^(${r.prefix})?(${r.signbox})(${s.full})?`,o="string"==typeof e?e.match(new RegExp(i)):void 0;return o?{sequence:o[1]?o[1].slice(1).match(/.{6}/g):void 0,box:o[2][0],max:n(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]}:{}},a=(e,r)=>{let n="";const i=l(e);if(i.box){const e=r.indexOf("A")>-1,o=r.indexOf("a")>-1,t=r.indexOf("S")>-1,s=r.indexOf("s")>-1,l=r.indexOf("L")>-1;return(e||o||t||s)&&((e||o)&&i.sequence&&(n+="A",n+=i.sequence.map((e=>e.slice(0,4)+(o?"uu":e.slice(4,6)))).join(""),n+="T"),(t||s)&&i.spatials&&(n+=i.spatials.map((e=>e.symbol.slice(0,4)+(s?"uu":e.symbol.slice(4,6))+(l?e.coord.join("x"):""))).join(""))),n?"Q"+n:void 0}},c=(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],t=e[1],s=e[2],l=r[0],a=r[1],c=r[2],u=i?"0123456789abcdef":"0123456789",$=u.indexOf(o),p=u.indexOf(l);if($>p)throw new Error("Start is greater than end");if(o===l){const e=function(e,r,n){const i=e[0],o=e[1],t=r[0],s=r[1],l=n?"0123456789abcdef":"0123456789",a=l.indexOf(i),c=l.indexOf(t);if(a<c){const e=[`${i}${f(o,l[l.length-1],n)}`],r=l.slice(a+1,c);return r.length>0&&e.push(f(r[0],r[r.length-1],n)+(n?"[0-9a-f]":"[0-9]")),e.push(`${t}${f(l[0],s,n)}`),e.join("|")}return`${i}${f(o,s,n)}`}(t+s,a+c,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],t=i.indexOf(e);if(r===i[0])return f(e,o,n)+(n?"[0-9a-f]":"[0-9]");{const s=f(r,o,n),l=t+1<i.length?i[t+1]:null;if(l){return e+s+"|"+f(l,o,n)+(n?"[0-9a-f]":"[0-9]")}return e+s}}(t,s,i);if(e.push(r.includes("|")?`${o}(?:${r})`:`${o}${r}`),$+1<p){const r=u.slice($+1,p),n=f(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],t=i.indexOf(e);if(t>0){return f(o,i[t-1],n)+(n?"[0-9a-f]":"[0-9]")+"|"+e+f(o,r,n)}return e+f(o,r,n)}(a,c,i);return e.push(n.includes("|")?`${l}(?:${n})`:`${l}${n}`),`(?:${e.join("|")})`}};function f(e,r,n){if(e===r)return e;if(!n)return`[${e}-${r}]`;const i="0123456789abcdef",o=i.indexOf(e),t=i.indexOf(r),s=i.slice(o,t+1);if(s.includes("9")&&s.includes("a")){const e=s.match(/[0-9]+/)[0],r=s.match(/[a-f]+/)[0];return`[${e[0]}-${e.slice(-1)}${r[0]}-${r.slice(-1)}]`}return`[${s[0]}-${s.slice(-1)}]`}const u=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},$=e=>{let r=e.slice(1,4),n=e.slice(5,8);return"S"+c(r,n,"hex")+"[0-5][0-9a-f]"},p=i=>{if(!(i=i.match(new RegExp(`^${e.full}`))[0]))return"";var o,t,l,a,f,p,g,d,x,m,b,h=20,y="(?:"+s.full+")?";if("Q"==i)return[r.prefix+"?"+r.signbox];if("Q-"==i)return[r.prefix+"?"+r.signbox+y];if("QT"==i)return[r.prefix+r.signbox];if("QT-"==i)return[r.prefix+r.signbox+y];var A=[],v=i.indexOf("T")+1;if(v){b="(?:A";var R=i.slice(0,v);if(i=i.replace(R,""),"QT"==R)b+=r.nullorsymbol+"+)";else if(o=R.match(new RegExp(e.list,"g"))){for(f=0;f<o.length;f+=1)if(a=[],t=o[f].match(new RegExp(e.item,"g"))){for(p=0;p<t.length;p+=1)(l=t[p].match(new RegExp(e.nullorsymbol)))?a.push(u(l[0])):a.push($(t[p]));1==a.length?b+=a[0]:b+="(?:"+a.join("|")+")"}b+=r.nullorsymbol+"*)"}}if((o=i.match(new RegExp(e.var,"g")))&&(h=1*o.toString().slice(1)),o=i.match(new RegExp(e.list+e.coord,"g")))for(f=0;f<o.length;f+=1){if(a=[],t=o[f].match(new RegExp("("+e.symbol+"|"+e.range+")","g"))){for(p=0;p<t.length;p+=1)(l=t[p].match(new RegExp(e.symbol)))?a.push(u(l[0])):a.push($(t[p]));g=1==a.length?a[0]:"(?:"+a.join("|")+")"}o[f].includes("x")?(x=(d=n(o[f].slice(-7)))[0],m=d[1],g+=c(x-h,x+h),g+="x",g+=c(m-h,m+h)):g+=r.coord,g=r.signbox+g+"(?:"+r.symbol+r.coord+")*",g=v?b+g:r.prefix+"?"+g,i.indexOf("-")>0&&(g+=y),A.push(g)}return A.length||(i.indexOf("-")>0&&(g+=y),A.push(b+r.signbox)),A},g=(e,r)=>{if(!r)return[];let n,i,o,t,s,l=p(e);if(!l)return[];for(s=0;s<l.length;s+=1)n=l[s],i=r.match(new RegExp(n,"g")),r=i?i.join(" "):"";return r?(o=r.split(" "),t=o.filter((function(e){return!(e in o)&&(o[e]=!0)}),{})):t=[],t},d=(e,r)=>{if(!r)return[];let n,i,o,t,s,l=p(e);if(!l)return[];for(s=0;s<l.length;s+=1)n=l[s],n="^"+n+".*",i=r.match(new RegExp(n,"mg")),r=i?i.join("\n"):"";return r?(o=r.split("\n"),t=o.filter((function(e){return!(e in o)&&(o[e]=!0)}),{})):t=[],t};export{t as compose,a as fsw2query,d as lines,o as parse,c as range,e as re,p as regex,g as results};