UNPKG

8.01 kBJavaScriptView Raw
1/* Sutton SignWriting Core Module v1.4.2 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */
2!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={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.range=`R${r.base}${r.base}`,r.item=`(?:${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={symbol:"(?:(?:\ud8c0[\udc01-\udfff])|(?:[\ud8c1-\ud8fc][\udc00-\udfff])|(?:\ud8fd[\udc00-\udc80]))",coord:"(?:\ud836[\udc0c-\uddff]){2}",sort:"𝠀",box:"\ud836[\udc01-\udc04]"};o.prefix=`(?:${o.sort}(?:${o.symbol})+)`,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,s=e=>String.fromCodePoint(120844+parseInt(e)-250),i=e=>[t(e.slice(0,2)),t(e.slice(2,4))],n=e=>e.map((e=>s(e))).join(""),l=e=>{const r=(e=>parseInt(e.codePointAt(0)))(e)-262145,o=parseInt(r/96),t=parseInt((r-96*o)/16),s=parseInt(r-96*o-16*t);return"S"+(o+256).toString(16)+t.toString(16)+s.toString(16)},c=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=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=i(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 u={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}"};u.colorbase=`(?:${u.colorhex}|${u.colorname})`,u.color=`_${u.colorbase}_`,u.colors=`_${u.colorbase}(?:,${u.colorbase})?_`,u.background=`G${u.color}`,u.detail=`D${u.colors}`,u.detailsym=`D[0-9]{2}${u.colors}`,u.classes=`${u.classbase}(?: ${u.classbase})*`,u.full=`-(${u.colorize})?(${u.padding})?(${u.background})?(${u.detail})?(${u.zoom})?(?:-((?:${u.detailsym})*))?(?:-(${u.classes})?!(?:(${u.id})!)?)?`;const f=e=>{const r=`^(${o.prefix})?(${o.signbox})(${u.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: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()],p=(e,r)=>{if(e>r)return"";let o,t="",s=[];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],s.push(t)):(t="\\u"+e[0]+"[\\u"+e[1]+"-\\u"+r[1]+"]",s.push(t));else{t="DFFF"==e[1]?"\\u"+e[0]+"\\uDFFF":"\\u"+e[0]+"[\\u"+e[1]+"-\\uDFFF]",s.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]",s.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]",s.push(t)),t="DC00"==r[1]?"\\u"+r[0]+"\\uDC00":"\\u"+r[0]+"[\\uDC00-\\u"+r[1]+"]",s.push(t)}return o=s.length,1==o?t=s[0]:(t=s.join(")|("),t="(("+t+"))"),t.replace(/\\u([0-9A-F]{4})/g,(function(e,r){return String.fromCharCode(parseInt(r,16))}))},g=e=>{let o=e.match(new RegExp(r.symbol));if(o){let e,r,t=o[0].slice(0,2),s=l(t),i=s.slice(0,4);if("fr"==o[0].slice(-2))return e=c(i+"00"),r=c(i+"5f"),p(e,r);if("r"==o[0].slice(-1))return e=c(s.slice(0,5)+"0"),r=c(s.slice(0,5)+"f"),p(e,r);if("f"==o[0].slice(-1)){return"("+[0,1,2,3,4,5].map((function(e){return c(i+e+s.slice(-1))})).join("|")+")"}return t}return""},m=e=>(from=l(e.slice(1,3)),to=l(e.slice(-2)),from=c(from.slice(0,4)+"00"),to=c(to.slice(0,4)+"5f"),p(from,to)),$=e=>{if(!(e=e.match(new RegExp(`^${r.full}`))[0]))return"";let t,n,l,c,a,f,d,$,h,x,y,b=20,A=o.symbol,R=o.coord,w=o.box+R+"("+A+R+")*",v="("+o.sort+"("+A+")+)",E="("+u.full+")?";if("Q"==e)return[o.sign];if("Q-"==e)return[o.sign+"("+u.full+")?"];if("QT"==e)return[o.sortable];if("QT-"==e)return[o.sortable+"("+u.full+")?"];let C=[],F=e.indexOf("T")+1;if(F){y="("+o.sort;let s=e.slice(0,F);if(e=e.replace(s,""),"QT"==s)y+="("+A+")+)";else if(t=s.match(new RegExp("("+r.list+")","g")),t){for(a=0;a<t.length;a+=1)if(c=[],n=t[a].match(new RegExp("("+r.symbol+"|"+r.range+")","g")),n){for(f=0;f<n.length;f+=1)l=n[f].match(new RegExp(r.symbol)),l?c.push(g(l[0])):c.push(m(n[f]));1==c.length?y+=c[0]:y+="("+c.join("|")+")"}y+="("+o.symbol+")*)"}}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=[],n=t[a].match(new RegExp("("+r.symbol+"|"+r.range+")","g")),n){for(f=0;f<n.length;f+=1)l=n[f].match(new RegExp(r.symbol)),l?c.push(g(l[0])):c.push(m(n[f]));$=1==c.length?c[0]:"("+c.join("|")+")"}d=t[a].match(new RegExp(`${o.coord}`)),d?(d=i(d[0]),h=d[0],x=d[1],$+=p(s(h-b),s(h+b)),$+=p(s(x-b),s(x+b))):$+=o.coord,$=w+$+"("+A+R+")*",$=F?y+$:v+"?"+$,e.indexOf("-")>0&&($+=E),C.push($)}return C.length||(e.indexOf("-")>0&&($+=E),C.push(y+w)),C};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?n(e.coord):"")})).join("")),o+=e.style?"-":"",o=o.match(new RegExp(`^${r.full}`))[0],o},e.lines=(e,r)=>{if(!r)return[];let o,t,s,i,n,l=$(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?(s=r.split("\n"),i=s.filter((function(e){return!(e in s)&&(s[e]=!0)}),{})):i=[],i},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]?a(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=p,e.re=r,e.regex=$,e.results=(e,r)=>{if(!r)return[];let o,t,s,i,n,l=$(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?(s=r.split(" "),i=s.filter((function(e){return!(e in s)&&(s[e]=!0)}),{})):i=[],i},e.swu2query=(e,r)=>{let o="";const t=f(e);if(t.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)&&t.sequence&&(o+="A",o+=t.sequence.map((e=>e+(s?"fr":""))).join(""),o+="T"),(i||l)&&t.spatials&&(o+=t.spatials.map((e=>e.symbol+(l?"fr":"")+(c?n(e.coord):""))).join(""))),o?"Q"+o:void 0}},e.symbolRanges=g,Object.defineProperty(e,"__esModule",{value:!0})}));