UNPKG

7.73 kBJavaScriptView Raw
1/* Sutton SignWriting Core Module v1.4.2 (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.item=`(?:${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={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 o=e=>parseInt(e.codePointAt(0))-120844+250,t=e=>String.fromCodePoint(120844+parseInt(e)-250),i=e=>[o(e.slice(0,2)),o(e.slice(2,4))],s=e=>e.map((e=>t(e))).join(""),n=e=>{const r=(e=>parseInt(e.codePointAt(0)))(e)-262145,o=parseInt(r/96),t=parseInt((r-96*o)/16),i=parseInt(r-96*o-16*t);return"S"+(o+256).toString(16)+t.toString(16)+i.toString(16)},l=e=>{return r=262145+96*(parseInt(e.slice(1,4),16)-256)+16*parseInt(e.slice(4,5),16)+parseInt(e.slice(5,6),16),String.fromCodePoint(r);var r},a=o=>o.match(new RegExp(`(${e.list}${e.coord})`,"g")).map((e=>{let o,t;return o=e.match(new RegExp(`${r.coord}`)),o?(o=i(o[0]),t=e.slice(0,-4)):(o=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:o,coord:o}:t.includes("R")?{range:[t.slice(1,3),t.slice(3,5)],coord:o}:{symbol:t,coord:o}})),c=r=>{const o="string"==typeof r?r.match(new RegExp(`^${e.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(`(${e.list})`,"g")).map((r=>r.includes("o")?["or"].concat(r.match(new RegExp(`(${e.item})`,"g")).map((e=>"R"!=e[0]?e:[e.slice(1,3),e.slice(3,5)]))):"R"!=r[0]?r:[r.slice(1,3),r.slice(3,5)]))}):void 0,signbox:o&&o[2]?a(o[2]):void 0,variance:o&&o[3]?parseInt(o[3].slice(1)):void 0,style:!(!o||!o[4])||void 0};var t},u=r=>{if(!r||!r.query)return;let o="Q";return r.prefix&&r.prefix.required&&(Array.isArray(r.prefix.parts)&&(o+="A",o+=r.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(r.signbox)&&(o+=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]}${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?s(e.coord):"")})).join("")),o+=r.style?"-":"",o=o.match(new RegExp(`^${e.full}`))[0],o};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 p=e=>{const o=`^(${r.prefix})?(${r.signbox})(${f.full})?`,t="string"==typeof e?e.match(new RegExp(o)):void 0;return t?{sequence:t[1]?t[1].slice(2).match(/.{2}/g):void 0,box:t[2].slice(0,2),max:i(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:i(e.slice(2))}))),style:t[3]}:{}},d=e=>[e.charCodeAt(0).toString(16).toUpperCase(),e.charCodeAt(1).toString(16).toUpperCase()],g=(e,r)=>{let o="";const t=p(e);if(t.box){const e=r.indexOf("A")>-1,i=r.indexOf("a")>-1,n=r.indexOf("S")>-1,l=r.indexOf("s")>-1,a=r.indexOf("L")>-1;return(e||i||n||l)&&((e||i)&&t.sequence&&(o+="A",o+=t.sequence.map((e=>e+(i?"fr":""))).join(""),o+="T"),(n||l)&&t.spatials&&(o+=t.spatials.map((e=>e.symbol+(l?"fr":"")+(a?s(e.coord):""))).join(""))),o?"Q"+o:void 0}},m=(e,r)=>{if(e>r)return"";let o,t="",i=[];if(e=d(e),r=d(r),2!=e.length&&2!=r.length)return"";if(e[0]==r[0])e[1]==r[1]?(t="\\u"+e[0]+"\\u"+e[1],i.push(t)):(t="\\u"+e[0]+"[\\u"+e[1]+"-\\u"+r[1]+"]",i.push(t));else{t="DFFF"==e[1]?"\\u"+e[0]+"\\uDFFF":"\\u"+e[0]+"[\\u"+e[1]+"-\\uDFFF]",i.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]",i.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]",i.push(t)),t="DC00"==r[1]?"\\u"+r[0]+"\\uDC00":"\\u"+r[0]+"[\\uDC00-\\u"+r[1]+"]",i.push(t)}return o=i.length,1==o?t=i[0]:(t=i.join(")|("),t="(("+t+"))"),t.replace(/\\u([0-9A-F]{4})/g,(function(e,r){return String.fromCharCode(parseInt(r,16))}))},$=r=>{let o=r.match(new RegExp(e.symbol));if(o){let e,r,t=o[0].slice(0,2),i=n(t),s=i.slice(0,4);if("fr"==o[0].slice(-2))return e=l(s+"00"),r=l(s+"5f"),m(e,r);if("r"==o[0].slice(-1))return e=l(i.slice(0,5)+"0"),r=l(i.slice(0,5)+"f"),m(e,r);if("f"==o[0].slice(-1)){return"("+[0,1,2,3,4,5].map((function(e){return l(s+e+i.slice(-1))})).join("|")+")"}return t}return""},h=e=>(from=n(e.slice(1,3)),to=n(e.slice(-2)),from=l(from.slice(0,4)+"00"),to=l(to.slice(0,4)+"5f"),m(from,to)),x=o=>{if(!(o=o.match(new RegExp(`^${e.full}`))[0]))return"";let s,n,l,a,c,u,p,d,g,x,b,y=20,A=r.symbol,R=r.coord,v=r.box+R+"("+A+R+")*",w="("+r.sort+"("+A+")+)",E="("+f.full+")?";if("Q"==o)return[r.sign];if("Q-"==o)return[r.sign+"("+f.full+")?"];if("QT"==o)return[r.sortable];if("QT-"==o)return[r.sortable+"("+f.full+")?"];let C=[],F=o.indexOf("T")+1;if(F){b="("+r.sort;let t=o.slice(0,F);if(o=o.replace(t,""),"QT"==t)b+="("+A+")+)";else if(s=t.match(new RegExp("("+e.list+")","g")),s){for(c=0;c<s.length;c+=1)if(a=[],n=s[c].match(new RegExp("("+e.symbol+"|"+e.range+")","g")),n){for(u=0;u<n.length;u+=1)l=n[u].match(new RegExp(e.symbol)),l?a.push($(l[0])):a.push(h(n[u]));1==a.length?b+=a[0]:b+="("+a.join("|")+")"}b+="("+r.symbol+")*)"}}if(s=o.match(new RegExp("(V[0-9]+)","g")),s&&(y=1*s.toString().slice(1)),s=o.match(new RegExp(e.list+e.coord,"g")),s)for(c=0;c<s.length;c+=1){if(a=[],n=s[c].match(new RegExp("("+e.symbol+"|"+e.range+")","g")),n){for(u=0;u<n.length;u+=1)l=n[u].match(new RegExp(e.symbol)),l?a.push($(l[0])):a.push(h(n[u]));d=1==a.length?a[0]:"("+a.join("|")+")"}p=s[c].match(new RegExp(`${r.coord}`)),p?(p=i(p[0]),g=p[0],x=p[1],d+=m(t(g-y),t(g+y)),d+=m(t(x-y),t(x+y))):d+=r.coord,d=v+d+"("+A+R+")*",d=F?b+d:w+"?"+d,o.indexOf("-")>0&&(d+=E),C.push(d)}return C.length||(o.indexOf("-")>0&&(d+=E),C.push(b+v)),C},b=(e,r)=>{if(!r)return[];let o,t,i,s,n,l=x(e);if(!l)return[];for(n=0;n<l.length;n+=1)o=l[n],t=r.match(new RegExp(o,"g")),r=t?t.join(" "):"";return r?(i=r.split(" "),s=i.filter((function(e){return!(e in i)&&(i[e]=!0)}),{})):s=[],s},y=(e,r)=>{if(!r)return[];let o,t,i,s,n,l=x(e);if(!l)return[];for(n=0;n<l.length;n+=1)o=l[n],o="^"+o+".*",t=r.match(new RegExp(o,"mg")),r=t?t.join("\n"):"";return r?(i=r.split("\n"),s=i.filter((function(e){return!(e in i)&&(i[e]=!0)}),{})):s=[],s};export{u as compose,y as lines,c as parse,m as range,e as re,x as regex,b as results,g as swu2query,$ as symbolRanges};