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.21 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.swuquery={}))}(this,(function(e){"use strict";let r={null:"񀀀",base:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:(?:\ud836[\udc0c-\uddff]){2})?",var:"V[0-9]+"};r.symbol=`${r.base}f?r?`,r.nullorsymbol=`(?:${r.null}|${r.symbol})`,r.range=`R${r.base}${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 o={null:"񀀀",symbol:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:\ud836[\udc0c-\uddff]){2}",sort:"𝠀",box:"\ud836[\udc01-\udc04]"};o.nullorsymbol=`(?:${o.null}|${o.symbol})`,o.prefix=`(?:${o.sort}(?:${o.nullorsymbol})+)`,o.spatial=`${o.symbol}${o.coord}`,o.signbox=`${o.box}${o.coord}(?:${o.spatial})*`,o.sign=`${o.prefix}?${o.signbox}`,o.sortable=`${o.prefix}${o.signbox}`;const t=e=>parseInt(e.codePointAt(0))-120844+250,n=e=>String.fromCodePoint(120844+parseInt(e)-250),s=e=>[t(e.slice(0,2)),t(e.slice(2,4))],i=e=>e.map((e=>n(e))).join(""),l=e=>String.fromCodePoint(e),c=e=>{if("񀀀"===e)return"S00000";const r=(e=>parseInt(e.codePointAt(0)))(e)-262145,o=parseInt(r/96),t=parseInt((r-96*o)/16),n=parseInt(r-96*o-16*t);return"S"+(o+256).toString(16)+t.toString(16)+n.toString(16)},a=e=>l("S00000"===e?262144:262145+96*(parseInt(e.slice(1,4),16)-256)+16*parseInt(e.slice(4,5),16)+parseInt(e.slice(5,6),16)),u=e=>e.match(new RegExp(`(${r.list}${r.coord})`,"g")).map((e=>{let r,t;return r=e.match(new RegExp(`${o.coord}`)),r?(r=s(r[0]),t=e.slice(0,-4)):(r=void 0,t=e),t.includes("o")?{or:t.split("o").map((e=>e.includes("R")?[e.slice(1,3),e.slice(3,5)]:e)),coord:r,coord:r}:t.includes("R")?{range:[t.slice(1,3),t.slice(3,5)],coord:r}:{symbol:t,coord:r}}));let f={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}"};f.colorbase=`(?:${f.colorhex}|${f.colorname})`,f.color=`_${f.colorbase}_`,f.colors=`_${f.colorbase}(?:,${f.colorbase})?_`,f.background=`G${f.color}`,f.detail=`D${f.colors}`,f.detailsym=`D[0-9]{2}${f.colors}`,f.classes=`${f.classbase}(?: ${f.classbase})*`,f.full=`-(${f.colorize})?(${f.padding})?(${f.background})?(${f.detail})?(${f.zoom})?(?:-((?:${f.detailsym})*))?(?:-(${f.classes})?!(?:(${f.id})!)?)?`;const d=e=>{const r=`^(${o.prefix})?(${o.signbox})(${f.full})?`,t="string"==typeof e?e.match(new RegExp(r)):void 0;return t?{sequence:t[1]?t[1].slice(2).match(/.{2}/g):void 0,box:t[2].slice(0,2),max:s(t[2].slice(2,6)),spatials:t[2].length<7?void 0:t[2].slice(6).match(/(.{6})/g).map((e=>({symbol:e.slice(0,2),coord:s(e.slice(2))}))),style:t[3]}:{}},p=e=>[e.charCodeAt(0).toString(16).toUpperCase(),e.charCodeAt(1).toString(16).toUpperCase()],g=(e,r)=>{if(e>r)return"";let o,t="",n=[];if(e=p(e),r=p(r),2!=e.length&&2!=r.length)return"";if(e[0]==r[0])e[1]==r[1]?(t="\\u"+e[0]+"\\u"+e[1],n.push(t)):(t="\\u"+e[0]+"[\\u"+e[1]+"-\\u"+r[1]+"]",n.push(t));else{t="DFFF"==e[1]?"\\u"+e[0]+"\\uDFFF":"\\u"+e[0]+"[\\u"+e[1]+"-\\uDFFF]",n.push(t);let o=parseInt(r[0],16)-parseInt(e[0],16);2==o&&(t="\\u"+(parseInt(e[0],16)+1).toString(16).toUpperCase(),t+="[\\uDC00-\\uDFFF]",n.push(t)),o>2&&(t="[",t+="\\u"+(parseInt(e[0],16)+1).toString(16).toUpperCase(),t+="-\\u"+(parseInt(r[0],16)-1).toString(16).toUpperCase(),t+="][\\uDC00-\\uDFFF]",n.push(t)),t="DC00"==r[1]?"\\u"+r[0]+"\\uDC00":"\\u"+r[0]+"[\\uDC00-\\u"+r[1]+"]",n.push(t)}return o=n.length,1==o?t=n[0]:(t=n.join(")|("),t="(("+t+"))"),t.replace(/\\u([0-9A-F]{4})/g,(function(e,r){return String.fromCharCode(parseInt(r,16))}))},m=e=>{let o=e.match(new RegExp(r.symbol));if(o){let e,r,t=o[0].slice(0,2),n=c(t),s=n.slice(0,4);if("fr"==o[0].slice(-2))return e=a(s+"00"),r=a(s+"5f"),g(e,r);if("r"==o[0].slice(-1))return e=a(n.slice(0,5)+"0"),r=a(n.slice(0,5)+"f"),g(e,r);if("f"==o[0].slice(-1)){return"("+[0,1,2,3,4,5].map((function(e){return a(s+e+n.slice(-1))})).join("|")+")"}return t}return""},$=e=>{let r=c(e.slice(1,3)),o=c(e.slice(-2));return r=a(r.slice(0,4)+"00"),o=a(o.slice(0,4)+"5f"),g(r,o)},h=e=>{if(!(e=e.match(new RegExp(`^${r.full}`))[0]))return"";let t,i,l,c,a,u,d,p,h,x,y,b=20,A=o.box+o.coord+"(?:"+o.symbol+o.coord+")*",R="(?:"+o.sort+o.nullorsymbol+"+)",w="(?:"+f.full+")?";if("Q"==e)return[o.sign];if("Q-"==e)return[o.sign+"("+f.full+")?"];if("QT"==e)return[o.sortable];if("QT-"==e)return[o.sortable+"("+f.full+")?"];let v=[],E=e.indexOf("T")+1;if(E){y="(?:"+o.sort;let n=e.slice(0,E);if(e=e.replace(n,""),"QT"==n)y+=o.nullorsymbol+"+)";else if(t=n.match(new RegExp(r.list,"g")),t){for(a=0;a<t.length;a+=1)if(c=[],i=t[a].match(new RegExp(r.item,"g")),i){for(u=0;u<i.length;u+=1)l=i[u].match(new RegExp(r.range)),l?c.push($(i[u])):c.push(m(i[u]));1==c.length?y+=c[0]:y+="(?:"+c.join("|")+")"}y+=o.nullorsymbol+"*)"}}if(t=e.match(new RegExp("(?:V[0-9]+)","g")),t&&(b=1*t.toString().slice(1)),t=e.match(new RegExp(r.list+r.coord,"g")),t)for(a=0;a<t.length;a+=1){if(c=[],i=t[a].match(new RegExp("("+r.range+"|"+r.symbol+")","g")),i){for(u=0;u<i.length;u+=1)l=i[u].match(new RegExp(r.range)),l?c.push($(i[u])):c.push(m(i[u]));p=1==c.length?c[0]:"(?:"+c.join("|")+")"}d=t[a].match(new RegExp(`${o.coord}`)),d?(d=s(d[0]),h=d[0],x=d[1],p+=g(n(h-b),n(h+b)),p+=g(n(x-b),n(x+b))):p+=o.coord,p=A+p+"(?:"+o.symbol+o.coord+")*",p=E?y+p:R+"?"+p,e.indexOf("-")>0&&(p+=w),v.push(p)}return v.length||(e.indexOf("-")>0&&(p+=w),v.push(y+A)),v};e.compose=e=>{if(!e||!e.query)return;let o="Q";return e.prefix&&e.prefix.required&&(Array.isArray(e.prefix.parts)&&(o+="A",o+=e.prefix.parts.map((e=>"string"==typeof e?e:Array.isArray(e)&&2==e.length?`R${e[0]}${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]}${e[1]}`:void 0)).join("o")):void 0)).join("")),o+="T"),Array.isArray(e.signbox)&&(o+=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]}${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]}${e.range[1]}`),r+(Array.isArray(e.coord)&&2==e.coord.length?i(e.coord):"")})).join("")),e.variance&&(o+="V"+e.variance),o+=e.style?"-":"",o=o.match(new RegExp(`^${r.full}`))[0],o},e.lines=(e,r)=>{if(!r)return[];let o,t,n,s,i,l=h(e);if(!l)return[];for(i=0;i<l.length;i+=1)o=l[i],o="^"+o+".*",t=r.match(new RegExp(o,"mg")),r=t?t.join("\n"):"";return r?(n=r.split("\n"),s=n.filter((function(e){return!(e in n)&&(n[e]=!0)}),{})):s=[],s},e.parse=e=>{const o="string"==typeof e?e.match(new RegExp(`^${r.full}`)):void 0;return{query:!!o||void 0,prefix:o&&o[1]?(t=o[1],{required:!0,parts:"T"==t?void 0:t.match(new RegExp(`(${r.list})`,"g")).map((e=>e.includes("o")?["or"].concat(e.match(new RegExp(`(${r.item})`,"g")).map((e=>"R"!=e[0]?e:[e.slice(1,3),e.slice(3,5)]))):"R"!=e[0]?e:[e.slice(1,3),e.slice(3,5)]))}):void 0,signbox:o&&o[2]?u(o[2]):void 0,variance:o&&o[3]?parseInt(o[3].slice(1)):void 0,style:!(!o||!o[4])||void 0};var t},e.range=g,e.re=r,e.regex=h,e.results=(e,r)=>{if(!r)return[];let o,t,n,s,i,l=h(e);if(!l)return[];for(i=0;i<l.length;i+=1)o=l[i],t=r.match(new RegExp(o,"g")),r=t?t.join(" "):"";return r?(n=r.split(" "),s=n.filter((function(e){return!(e in n)&&(n[e]=!0)}),{})):s=[],s},e.swu2query=(e,r)=>{let o="";const t=d(e);if(t.box){const e=r.indexOf("A")>-1,n=r.indexOf("a")>-1,s=r.indexOf("S")>-1,l=r.indexOf("s")>-1,c=r.indexOf("L")>-1;return(e||n||s||l)&&((e||n)&&t.sequence&&(o+="A",o+=t.sequence.map((e=>e+(n?"fr":""))).join(""),o+="T"),(s||l)&&t.spatials&&(o+=t.spatials.map((e=>e.symbol+(l?"fr":"")+(c?i(e.coord):""))).join(""))),o?"Q"+o:void 0}},e.symbolRanges=m,Object.defineProperty(e,"__esModule",{value:!0})}));