UNPKG

g1lib

Version:

An ubiquitous static javascript toolbox lib for Ǧ1 / Duniter ecosystem with reliability in mind.

2 lines (1 loc) 4.62 kB
const x={"(":String.fromCharCode(8960),")":String.fromCharCode(8961),"|":String.fromCharCode(8962),"{":String.fromCharCode(8963),"}":String.fromCharCode(8964),",":String.fromCharCode(8965),"[":String.fromCharCode(8966),"]":String.fromCharCode(8967),"-":String.fromCharCode(8968),"<":String.fromCharCode(8969),">":String.fromCharCode(8970),":":String.fromCharCode(8971),"=":String.fromCharCode(8972),"@":String.fromCharCode(8973),"\\":String.fromCharCode(8974)},d=N(x);function N(n){const t={};for(const i in n)t[n[i]]=i;return t}function T(n){return n.replace(/\\(.)/g,(t,i)=>x[i]?x[i]:i)}function J(n){return n.split("").map(t=>d[t]?d[t]:t).join("")}function y(n){return n.split("").map(t=>d[t]?`\\${d[t]}`:t).join("")}function M(n){const t=T(n).split(""),i=A(t);if(t.length)throw new Error(`fail to build tree from : "${y(n)}" parsed: ${JSON.stringify(i)} unparsed/failed: ${y(t.join(""))}`);let e,o=i;do e=JSON.stringify(o),o=b(o);while(JSON.stringify(o)!==e);return w(o),o}function p(n){n.str.length&&n.tree.alt[n.tree.alt.length-1].step.push({str:n.str}),n.str=""}function j(n,t){t.str+=n}function S(n){n.tree.alt.push({step:[]})}function v(n){n.tree.alt[n.tree.alt.length-1].step.push(A(n.input))}function z(){return!0}const m={")":(n,t)=>z(p(t)),"|":(n,t)=>(p(t),S(t),void 0),"(":(n,t)=>(p(t),v(t),void 0),default:(n,t)=>j(n,t)};function A(n){const t={input:n,str:"",tree:{alt:[{step:[]}]}};for(;t.input.length;){const i=t.input.shift();if(typeof m[i]<"u"){if(m[i](i,t))return t.tree}else m.default(i,t)}return p(t),t.tree}function P(n){const t={input:n,str:"",tree:{alt:[{step:[]}]}};return n.forEach(i=>{h(i)?j(i.str,t):(p(t),t.tree.alt[t.tree.alt.length-1].step.push(i))}),p(t),t.tree.alt[t.tree.alt.length-1].step}function b(n){if(h(n))return n;const t=f(n)?"alt":"step";if(n[t]=n[t].map(b),n[t].length===0)return{str:""};if(n[t].length===1)return n[t][0];if(f(n))return n.alt=k(n.alt),n.alt=[].concat(...n.alt.map(i=>f(i)?i.alt:[i])),n;if(c(n))return n.step=P(n.step),n}function h(n){return typeof n=="object"&&typeof n.str=="string"}function c(n){return typeof n=="object"&&typeof n.step<"u"}function f(n){return typeof n=="object"&&typeof n.alt<"u"}function k(n){const t={};return n.forEach(i=>t[JSON.stringify(i)]=i),Object.keys(t).sort().map(JSON.parse)}function K(n,t){function i(e){if(h(e)){if(!e.str.includes(n))return{notMatching:e.str};if(e.str.split(n).length>2)throw new Error("Error: @@ can only appear once in expression (idSec at left, password \xE0 right)");return{matching:e.str}}if(c(e)){let o=0,a=!1,l="",s="";if(e.step.forEach(O=>{const u=i(O);u.matching?(o++,typeof u.notMatching<"u"&&(a=!0),l+=u.matching,s+=u.notMatching):(l+=u.notMatching,s+=u.notMatching)}),o>1)throw new Error("Error: @@ can only appear once in expression (idSec at left, password \xE0 right)");return o&&a?{matching:l,notMatching:s}:o?{matching:l}:{notMatching:l}}if(f(e)){const o=[],a=[];if(e.alt.forEach(l=>{const s=i(l);typeof s.matching<"u"&&o.push(s.matching),typeof s.notMatching<"u"&&a.push(s.notMatching)}),o.length&&a.length)return{matching:`(${o.join("|")})`,notMatching:`(${a.join("|")})`};if(o.length)return{matching:`(${o.join("|")})`};if(a.length)return{notMatching:`(${a.join("|")})`}}throw new Error(`Error: how to splitAround ${n} with ${JSON.stringify(e)}`)}const r=i(t);return typeof r.matching<"u"&&(r.matching=g(M(r.matching))),typeof r.notMatching<"u"&&(r.notMatching=g(M(r.notMatching))),r}function R(n){return y(g(n))}function g(n){if(h(n))return n.str;if(c(n))return n.step.map(g).join("");if(f(n))return`(${n.alt.map(g).join("|")})`;throw new Error(`Error: how to serialize ${JSON.stringify(n)} RAW: ${n}`)}function w(n){if(h(n))return n.altCount=1,n.indexCost=1,1;if(f(n))return n.altCount=n.alt.reduce((t,i)=>t+w(i),0),n.indexCost=n.altCount-1,n.altCount;if(c(n)){n.altCount=n.step.reduce((i,r)=>i*w(r),1);let t=1;for(let i=n.step.length-1;i>=0;i--){const r=n.step[i];f(r)&&(r.alt.forEach(e=>E(e,t)),t*=r.altCount)}return n.indexCost=n.altCount,n.altCount}}function E(n,t){n.indexCost*=t,c(n)&&n.step.forEach(i=>E(i,t)),f(n)&&n.alt.forEach(i=>E(i,t))}function $(n){return n.altCount}function W(n,t){return J(C({index:n},t))}function D(n,t){return C({index:n},t)}function C(n,t){if(h(t))return t.str;if(c(t))return t.step.map(i=>C(n,i)).join("");if(f(t))for(const i of t.alt){if(n.index<i.indexCost)return C(n,i);n.index-=i.indexCost}throw new Error(`index out of bound : ${n.index+$(t)} asked, 0-${$(t)-1} available in ${R(t)}`)}export{C as _getAlternative,$ as altCount,M as buildTreeStruct,W as getAlternative,D as getRawAlternative,g as rawSerialize,R as serialize,K as splitAround};