UNPKG

7.23 kBJavaScriptView Raw
1/* Sutton SignWriting Core Module v1.2.0 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */
2let e={base:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:(?:\ud836[\udc0c-\uddff]){2})?",var:"V[0-9]+"};e.symbol=`${e.base}f?r?`,e.range=`R${e.base}${e.base}`,e.prefix=`(?:A(?:${e.symbol}|${e.range})+)?T`,e.signbox=`(?:${e.symbol}${e.coord}|${e.range}${e.coord})*`,e.full=`Q(${e.prefix})?(${e.signbox})?(${e.var})?(-?)`;let r={symbol:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:\ud836[\udc0c-\uddff]){2}",sort:"𝠀",box:"\ud836[\udc01-\udc04]"};r.prefix=`(?:${r.sort}(?:${r.symbol})+)`,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 t=e=>parseInt(e.codePointAt(0))-120844+250,o=e=>[t(e.slice(0,2)),t(e.slice(2,4))],n=e=>e.map(e=>(e=>String.fromCodePoint(120844+parseInt(e)-250))(e)).join(""),s=e=>{const r=(e=>parseInt(e.codePointAt(0)))(e)-262145,t=parseInt(r/96),o=parseInt((r-96*t)/16),n=parseInt(r-96*t-16*o);return"S"+(t+256).toString(16)+o.toString(16)+n.toString(16)},i=e=>(e=>String.fromCodePoint(e))(262145+96*(parseInt(e.slice(1,4),16)-256)+16*parseInt(e.slice(4,5),16)+parseInt(e.slice(5,6),16)),l=r=>({required:!0,parts:"T"==r?void 0:r.match(new RegExp(`(${e.symbol}|${e.range})`,"g")).map(e=>"R"!=e[0]?e:[e.slice(1,3),e.slice(3,5)])}),c=r=>r.match(new RegExp(`(${e.symbol}${e.coord}|${e.range}${e.coord})`,"g")).map(e=>{let r,t;return e.length>5?(r=o(e.slice(-4)),t=e.slice(0,-4)):t=e,t.includes("R")?{range:[t.slice(1,3),t.slice(3,5)],coord:r}:{symbol:t,coord:r}}),a=r=>{const t="string"==typeof r?r.match(new RegExp(`^${e.full}`)):void 0;return{query:!!t||void 0,prefix:t&&t[1]?l(t[1]):void 0,signbox:t&&t[2]?c(t[2]):void 0,variance:t&&t[3]?parseInt(t[3].slice(1)):void 0,style:!(!t||!t[4])||void 0}},u=r=>{if(!r||!r.query)return;let t="Q";return r.prefix&&r.prefix.required&&(Array.isArray(r.prefix.parts)&&(t+="A",t+=r.prefix.parts.map(e=>"string"==typeof e?e:Array.isArray(e)&&2==e.length?`R${e[0]}${e[1]}`:void 0).join("")),t+="T"),Array.isArray(r.signbox)&&(t+=r.signbox.map(e=>{let r;return 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?n(e.coord):"")}).join("")),t=(t+=r.style?"-":"").match(new RegExp(`^${e.full}`))[0]};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)",zoomsym:"Z[0-9]{2},[0-9]+(?:\\.[0-9]+)?(?:,[0-9]{3}x[0-9]{3})?",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.zoomsym})*))?(?:-(${f.classes})?!(?:(${f.id})!)?)?`;const d=e=>{const t=`^(${r.prefix})?(${r.signbox})(${f.full})?`,n="string"==typeof e?e.match(new RegExp(t)):void 0;return n?{sequence:n[1]?n[1].slice(2).match(/.{2}/g):void 0,box:n[2].slice(0,2),max:o(n[2].slice(2,6)),spatials:n[2].length<7?void 0:n[2].slice(6).match(/(.{6})/g).map(e=>({symbol:e.slice(0,2),coord:o(e.slice(2))})),style:n[3]}:{}},p=e=>[e.charCodeAt(0).toString(16).toUpperCase(),e.charCodeAt(1).toString(16).toUpperCase()],g=(e,r)=>{let t="";const o=d(e);if(o.box){const e=r.indexOf("A")>-1,s=r.indexOf("a")>-1,i=r.indexOf("S")>-1,l=r.indexOf("s")>-1,c=r.indexOf("L")>-1;return(e||s||i||l)&&((e||s)&&o.sequence&&(t+="A",t+=o.sequence.map(e=>e+(s?"fr":"")).join(""),t+="T"),(i||l)&&o.spatials&&(t+=o.spatials.map(e=>e.symbol+(l?"fr":"")+(c?n(e.coord):"")).join(""))),t?"Q"+t:void 0}},$=(e,r)=>{if(e>r)return"";let t,o="",n=[];if(e=p(e),r=p(r),2!=e.length&&2!=r.length)return"";if(e[0]==r[0])e[1]==r[1]?(o="\\u"+e[0]+"\\u"+e[1],n.push(o)):(o="\\u"+e[0]+"[\\u"+e[1]+"-\\u"+r[1]+"]",n.push(o));else{o="DFFF"==e[1]?"\\u"+e[0]+"\\uDFFF":"\\u"+e[0]+"[\\u"+e[1]+"-\\uDFFF]",n.push(o);let t=parseInt(r[0],16)-parseInt(e[0],16);2==t&&(o="\\u"+(parseInt(e[0],16)+1).toString(16).toUpperCase(),o+="[\\uDC00-\\uDFFF]",n.push(o)),t>2&&(o="[",o+="\\u"+(parseInt(e[0],16)+1).toString(16).toUpperCase(),o+="-\\u"+(parseInt(r[0],16)-1).toString(16).toUpperCase(),o+="][\\uDC00-\\uDFFF]",n.push(o)),o="DC00"==r[1]?"\\u"+r[0]+"\\uDC00":"\\u"+r[0]+"[\\uDC00-\\u"+r[1]+"]",n.push(o)}return(e=>e.replace(/\\u([0-9A-F]{4})/g,(function(e,r){return String.fromCharCode(parseInt(r,16))})))(o=1==(t=n.length)?n[0]:"(("+(o=n.join(")|("))+"))")},m=r=>{let t=r.match(new RegExp(e.symbol));if(t){let e,r,o=t[0].slice(0,2),n=s(o),l=n.slice(0,4);if("fr"==t[0].slice(-2))return e=i(l+"00"),r=i(l+"5f"),$(e,r);if("r"==t[0].slice(-1))return e=i(n.slice(0,5)+"0"),r=i(n.slice(0,5)+"f"),$(e,r);if("f"==t[0].slice(-1)){return"("+[0,1,2,3,4,5].map((function(e){return i(l+e+n.slice(-1))})).join("|")+")"}return o}return""},x=t=>{if(!(t=t.match(new RegExp(`^${e.full}`))[0]))return"";let l,c,a,u,d,p,g,x,h,b,y=20,A=r.symbol,R=r.coord,C=r.box+R+"("+A+R+")*",F="("+r.sort+"("+A+")+)",I="R"+A+A,S=A+"f?r?",v="("+R+")?",w="("+f.full+")?";if("Q"==t)return[r.sign];if("Q-"==t)return[r.sign+"("+f.full+")?"];if("QT"==t)return[r.sortable];if("QT-"==t)return[r.sortable+"("+f.full+")?"];let E,D=[],z=t.indexOf("T")+1;if(z){b="("+r.sort;let e=t.slice(0,z);if(t=t.replace(e,""),"QT"==e)b+="("+A+")+)";else if(l=e.match(new RegExp("("+S+"|"+I+")","g"))){let e;for(c=0;c<l.length;c+=1)(e=l[c].match(new RegExp("^"+S)))?b+=m(e[0]):(u=s(l[c].slice(1,3)),d=s(l[c].slice(-2)),u=i(u.slice(0,4)+"00"),d=i(d.slice(0,4)+"5f"),b+=$(u,d));b+="("+A+")*)"}}if((l=t.match(new RegExp("(V[0-9]+)","g")))&&(y=1*l.toString().slice(1)),l=t.match(new RegExp("("+I+v+"|"+S+v+")","g")))for(c=0;c<l.length;c+=1)"R"!=(a=l[c].toString())[0]?(E=a.match(new RegExp(S))[0],g=m(E),E.length>a.length?(x=(p=o(a.slice(-4)))[0],h=p[1],g+=$(n([x-y,x+y])),g+=$(n([h-y,h+y]))):g+=R,g=C+g+"("+A+R+")*",g=z?b+g:F+"?"+g,t.indexOf("-")>0&&(g+=w),D.push(g)):(a=l[c].toString(),u=s(a.slice(1,3)),d=s(a.slice(3,5)),u=i(u.slice(0,4)+"00"),d=i(d.slice(0,4)+"5f"),g=$(u,d),a.length>5?(x=(p=o(a.slice(5,9)))[0],h=p[1],g+=$(n([x-y,x+y])),g+=$(n([h-y,h+y]))):g+=R,g=C+g+"("+A+R+")*",g=z?b+g:F+"?"+g,t.indexOf("-")>0&&(g+=w),D.push(g));return D.length||(t.indexOf("-")>0&&(g+=w),D.push(b+C)),D},h=(e,r)=>{if(!r)return[];let t,o,n,s,i,l=x(e);if(!l)return[];for(i=0;i<l.length;i+=1)t=l[i],o=r.match(new RegExp(t,"g")),r=o?o.join(" "):"";return s=r?(n=r.split(" ")).filter((function(e){return!(e in n)&&(n[e]=!0)}),{}):[]},b=(e,r)=>{if(!r)return[];let t,o,n,s,i,l=x(e);if(!l)return[];for(i=0;i<l.length;i+=1)t="^"+(t=l[i])+".*",o=r.match(new RegExp(t,"mg")),r=o?o.join("\n"):"";return s=r?(n=r.split("\n")).filter((function(e){return!(e in n)&&(n[e]=!0)}),{}):[]};export{u as compose,b as lines,a as parse,$ as range,e as re,x as regex,h as results,g as swu2query,m as symbolRanges};