string-find-heads-tails
Version:
Finds where are arbitrary templating marker heads and tails located
57 lines (51 loc) • 17.2 kB
JavaScript
/**
* @name string-find-heads-tails
* @fileoverview Finds where are arbitrary templating marker heads and tails located
* @version 6.0.23
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-find-heads-tails/}
*/
;var stringFindHeadsTails=(()=>{var J=Object.create;var E=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var Q=Object.getOwnPropertyNames,B=Object.getOwnPropertySymbols,U=Object.getPrototypeOf,_=Object.prototype.hasOwnProperty,z=Object.prototype.propertyIsEnumerable;var W=(e,r,n)=>r in e?E(e,r,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[r]=n,O=(e,r)=>{for(var n in r||(r={}))_.call(r,n)&&W(e,n,r[n]);if(B)for(var n of B(r))z.call(r,n)&&W(e,n,r[n]);return e};var G=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports),K=(e,r)=>{for(var n in r)E(e,n,{get:r[n],enumerable:!0})},H=(e,r,n,f)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of Q(r))!_.call(e,i)&&i!==n&&E(e,i,{get:()=>r[i],enumerable:!(f=q(r,i))||f.enumerable});return e};var Z=(e,r,n)=>(n=e!=null?J(U(e)):{},H(r||!e||!e.__esModule?E(n,"default",{value:e,enumerable:!0}):n,e)),X=e=>H(E({},"__esModule",{value:!0}),e);var v=G((ue,k)=>{"use strict";k.exports=Y;function C(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function Y(e){if(e=e||{},e.circles)return ee(e);let r=new Map;if(r.set(Date,o=>new Date(o)),r.set(Map,(o,a)=>new Map(f(Array.from(o),a))),r.set(Set,(o,a)=>new Set(f(Array.from(o),a))),e.constructorHandlers)for(let o of e.constructorHandlers)r.set(o[0],o[1]);let n=null;return e.proto?c:i;function f(o,a){let s=Object.keys(o),l=new Array(s.length);for(let g=0;g<s.length;g++){let u=s[g],m=o[u];typeof m!="object"||m===null?l[u]=m:m.constructor!==Object&&(n=r.get(m.constructor))?l[u]=n(m,a):ArrayBuffer.isView(m)?l[u]=C(m):l[u]=a(m)}return l}function i(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return f(o,i);if(o.constructor!==Object&&(n=r.get(o.constructor)))return n(o,i);let a={};for(let s in o){if(Object.hasOwnProperty.call(o,s)===!1)continue;let l=o[s];typeof l!="object"||l===null?a[s]=l:l.constructor!==Object&&(n=r.get(l.constructor))?a[s]=n(l,i):ArrayBuffer.isView(l)?a[s]=C(l):a[s]=i(l)}return a}function c(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return f(o,c);if(o.constructor!==Object&&(n=r.get(o.constructor)))return n(o,c);let a={};for(let s in o){let l=o[s];typeof l!="object"||l===null?a[s]=l:l.constructor!==Object&&(n=r.get(l.constructor))?a[s]=n(l,c):ArrayBuffer.isView(l)?a[s]=C(l):a[s]=c(l)}return a}}function ee(e){let r=[],n=[],f=new Map;if(f.set(Date,s=>new Date(s)),f.set(Map,(s,l)=>new Map(c(Array.from(s),l))),f.set(Set,(s,l)=>new Set(c(Array.from(s),l))),e.constructorHandlers)for(let s of e.constructorHandlers)f.set(s[0],s[1]);let i=null;return e.proto?a:o;function c(s,l){let g=Object.keys(s),u=new Array(g.length);for(let m=0;m<g.length;m++){let h=g[m],t=s[h];if(typeof t!="object"||t===null)u[h]=t;else if(t.constructor!==Object&&(i=f.get(t.constructor)))u[h]=i(t,l);else if(ArrayBuffer.isView(t))u[h]=C(t);else{let d=r.indexOf(t);d!==-1?u[h]=n[d]:u[h]=l(t)}}return u}function o(s){if(typeof s!="object"||s===null)return s;if(Array.isArray(s))return c(s,o);if(s.constructor!==Object&&(i=f.get(s.constructor)))return i(s,o);let l={};r.push(s),n.push(l);for(let g in s){if(Object.hasOwnProperty.call(s,g)===!1)continue;let u=s[g];if(typeof u!="object"||u===null)l[g]=u;else if(u.constructor!==Object&&(i=f.get(u.constructor)))l[g]=i(u,o);else if(ArrayBuffer.isView(u))l[g]=C(u);else{let m=r.indexOf(u);m!==-1?l[g]=n[m]:l[g]=o(u)}}return r.pop(),n.pop(),l}function a(s){if(typeof s!="object"||s===null)return s;if(Array.isArray(s))return c(s,a);if(s.constructor!==Object&&(i=f.get(s.constructor)))return i(s,a);let l={};r.push(s),n.push(l);for(let g in s){let u=s[g];if(typeof u!="object"||u===null)l[g]=u;else if(u.constructor!==Object&&(i=f.get(u.constructor)))l[g]=i(u,a);else if(ArrayBuffer.isView(u))l[g]=C(u);else{let m=r.indexOf(u);m!==-1?l[g]=n[m]:l[g]=a(u)}}return r.pop(),n.pop(),l}}});var ae={};K(ae,{defaults:()=>j,strFindHeadsTails:()=>oe,version:()=>se});var L=Z(v(),1);var ce=(0,L.default)();function D(e){if(e==null||typeof e!="object")return!1;let r=Object.getPrototypeOf(e);return r!==null&&r!==Object.prototype&&Object.getPrototypeOf(r)!==null?!1:!(Symbol.iterator in e)&&!(Symbol.toStringTag in e)}function p(e){return typeof e=="string"}function N(e){return e!=null}function P(e,r){return D(e)&&p(r)&&r in e}function I(e){return typeof e!="string"?e:e.length?[e]:[]}var V={cb:void 0,i:!1,trimBeforeMatching:!1,trimCharsBeforeMatching:[],maxMismatches:0,firstMustMatch:!1,lastMustMatch:!1,hungry:!1},te=e=>e+1;function re(e,r,n,f,i=!1,c=te){var w;let o=typeof n=="function"?n():n;if(+r<0&&i&&o==="EOL")return o;let a=O(O({},V),f);if(r>=e.length&&!i)return!1;let s=i?1:n.length,l=0,g=!1,u=!1,m=!1,h=a.maxMismatches,t=r,d=!1,$=!1,y=!1;function A(){return l===1&&h<a.maxMismatches-1}for(;e[t];){let b=c(t);if(a.trimBeforeMatching&&e[t].trim()===""){if(!e[b]&&i&&n==="EOL")return!0;t=c(t);continue}if(a&&!a.i&&((w=a==null?void 0:a.trimCharsBeforeMatching)!=null&&w.includes(e[t]))||a!=null&&a.i&&a.trimCharsBeforeMatching&&a.trimCharsBeforeMatching.map(T=>T.toLowerCase()).includes(e[t].toLowerCase())){if(i&&n==="EOL"&&!e[b])return!0;t=c(t);continue}let S=b>t?n[n.length-s]:n[s-1];if(!a.i&&e[t]===S||a.i&&e[t].toLowerCase()===S.toLowerCase()){if(d||(d=!0),m||(m=!0),s===n.length){if($=!0,h!==a.maxMismatches)return!1}else s===1&&(y=!0);if(s-=1,l++,A())return!1;if(!s)return l!==n.length||h===a.maxMismatches||!g?t:!1}else if(!g&&!l&&(g=!0),a.maxMismatches&&h&&t){h-=1;for(let T=0;T<=h;T++){let x=b>t?n[n.length-s+1+T]:n[s-2-T],M=e[c(t)];if(x&&(!a.i&&e[t]===x||a.i&&e[t].toLowerCase()===x.toLowerCase())&&(!a.firstMustMatch||s!==n.length)){if(l++,A())return!1;s-=2,d=!0;break}else if(M&&x&&(!a.i&&M===x||a.i&&M.toLowerCase()===x.toLowerCase())&&(!a.firstMustMatch||s!==n.length)){if(!l&&!a.hungry)return!1;s-=1,d=!0;break}else if(x===void 0&&h>=0&&d&&(!a.firstMustMatch||$)&&(!a.lastMustMatch||y))return t}d||(u=t)}else return t===0&&s===1&&!a.lastMustMatch&&m?0:!1;if(u!==!1&&u!==t&&(u=!1),s<1)return t;t=c(t)}if(s>0)return i&&o==="EOL"?!0:a&&a.maxMismatches>=s&&m?u||0:!1}function ne(e,r,n,f,i){var g,u,m;if(D(i)&&P(i,"trimBeforeMatching")&&i&&typeof i.trimBeforeMatching!="boolean")throw new Error(`string-match-left-right/${e}(): [THROW_ID_09] opts.trimBeforeMatching should be boolean!${Array.isArray(i.trimBeforeMatching)?" Did you mean to use opts.trimCharsBeforeMatching?":""}`);let c=O(O({},V),i);if(typeof c.trimCharsBeforeMatching=="string"&&(c.trimCharsBeforeMatching=I(c.trimCharsBeforeMatching)),c.trimCharsBeforeMatching=c.trimCharsBeforeMatching.map(h=>p(h)?h:String(h)),!p(r)||!r.length)return!1;if(!Number.isInteger(n)||n<0)throw new Error(`string-match-left-right/${e}(): [THROW_ID_03] the second argument should be a natural number. Currently it's of a type: ${typeof n}, equal to:
${JSON.stringify(n,null,4)}`);let o,a;if(p(f))o=[f];else if(Array.isArray(f))o=f;else if(!f)o=f;else if(typeof f=="function")o=[],o.push(f);else throw new Error(`string-match-left-right/${e}(): [THROW_ID_05] the third argument, whatToMatch, is neither string nor array of strings! It's ${typeof f}, equal to:
${JSON.stringify(f,null,4)}`);if(i&&!D(i))throw new Error(`string-match-left-right/${e}(): [THROW_ID_06] the fourth argument, options object, should be a plain object. Currently it's of a type "${typeof i}", and equal to:
${JSON.stringify(i,null,4)}`);let s=0,l="";if((g=c==null?void 0:c.trimCharsBeforeMatching)!=null&&g.some((h,t)=>h.length>1?(s=t,l=h,!0):!1))throw new Error(`string-match-left-right/${e}(): [THROW_ID_07] the fourth argument, options object contains trimCharsBeforeMatching. It was meant to list the single characters but one of the entries at index ${s} is longer than 1 character, ${l.length} (equals to ${l}). Please split it into separate characters and put into array as separate elements.`);if(!o||!Array.isArray(o)||Array.isArray(o)&&!o.length||Array.isArray(o)&&o.length===1&&p(o[0])&&!o[0].trim()){if(typeof c.cb=="function"){let t,d=n;if((e==="matchLeftIncl"||e==="matchRight")&&(d+=1),e[5]==="L")for(let w=d;w--;){let b=r[w];if((!c.trimBeforeMatching||c.trimBeforeMatching&&(b!=null&&b.trim()))&&(!((u=c.trimCharsBeforeMatching)!=null&&u.length)||b!==void 0&&!c.trimCharsBeforeMatching.includes(b))){t=w;break}}else if(e.startsWith("matchRight"))for(let w=d;w<r.length;w++){let b=r[w];if((!c.trimBeforeMatching||c.trimBeforeMatching&&b.trim())&&(!((m=c.trimCharsBeforeMatching)!=null&&m.length)||!c.trimCharsBeforeMatching.includes(b))){t=w;break}}if(t===void 0)return!1;let $=r[t],y=t+1,A="";return y&&y>0&&(A=r.slice(0,y)),e[5]==="L"||t&&t>0&&(A=r.slice(t)),c.cb($,A,t)}let h="";throw i||(h=" More so, the whole options object, the fourth input argument, is missing!"),new Error(`string-match-left-right/${e}(): [THROW_ID_08] the third argument, "whatToMatch", was given as an empty string. This means, you intend to match purely by a callback. The callback was not set though, the opts key "cb" is not set!${h}`)}for(let h=0,t=o.length;h<t;h++){a=typeof o[h]=="function";let d=o[h],$,y,A="",w=n;e==="matchRight"?w+=1:e==="matchLeft"&&(w-=1);let b=re(r,w,d,c,a,S=>e[5]==="L"?S-1:S+1);if(b&&a&&typeof d=="function"&&d()==="EOL")return d()&&(!c.cb||c.cb($,A,y))?d():!1;if(Number.isInteger(b)&&(y=e.startsWith("matchLeft")?b-1:b+1,e[5]==="L"?A=r.slice(0,b):A=r.slice(y)),y<0&&(y=void 0),r[y]&&($=r[y]),Number.isInteger(b)&&(!c.cb||c.cb($,A,y)))return d}return!1}function R(e,r,n,f){return ne("matchRightIncl",e,r,n,f)}var F="6.0.23";var se=F,j={fromIndex:0,throwWhenSomethingWrongIsDetected:!0,allowWholeValueToBeOnlyHeadsOrTails:!0,source:"string-find-heads-tails",matchHeadsAndTailsStrictlyInPairsByTheirOrder:!1,relaxedAPI:!1};function oe(e,r,n,f){if(N(f)&&!D(f))throw new TypeError(`string-find-heads-tails: [THROW_ID_01] the fourth input argument, an Optional Options Object, must be a plain object! Currently it's equal to: ${f} (type: ${typeof f})`);let i=O(O({},j),f);if(p(i.fromIndex)&&/^\d*$/.test(i.fromIndex))i.fromIndex=Number(i.fromIndex);else if(!Number.isInteger(i.fromIndex)||i.fromIndex<0)throw new TypeError(`${i.source} [THROW_ID_18] the fourth input argument must be a natural number or zero! Currently it's: ${i.fromIndex}`);if(!p(e)||e.length===0){if(i.relaxedAPI)return[];throw new TypeError(`string-find-heads-tails: [THROW_ID_02] the first input argument, input string, must be a non-zero-length string! Currently it's: ${typeof e}, equal to: ${e}`)}let c,o;if(!p(r)&&!Array.isArray(r)){if(i.relaxedAPI)return[];throw new TypeError(`string-find-heads-tails: [THROW_ID_03] the second input argument, heads, must be either a string or an array of strings! Currently it's: ${typeof r}, equal to:
${JSON.stringify(r,null,4)}`)}else if(p(r))if(r.length===0){if(i.relaxedAPI)return[];throw new TypeError("string-find-heads-tails: [THROW_ID_04] the second input argument, heads, must be a non-empty string! Currently it's empty.")}else r=I(r);else if(Array.isArray(r))if(r.length===0){if(i.relaxedAPI)return[];throw new TypeError("string-find-heads-tails: [THROW_ID_05] the second input argument, heads, must be a non-empty array and contain at least one string! Currently it's empty.")}else if(r.every((t,d)=>(c=t,o=d,p(t)))){if(!r.every((t,d)=>(o=d,p(t)&&t.length&&t.trim()!=="")))if(i.relaxedAPI){if(r=r.filter(t=>p(t)&&t.length),r.length===0)return[]}else throw new TypeError(`string-find-heads-tails: [THROW_ID_07] the second input argument, heads, should not contain empty strings! For example, there's one detected at index ${o} of heads array:
${JSON.stringify(r,null,4)}.`)}else if(i.relaxedAPI){if(r=r.filter(t=>p(t)&&t.length),r.length===0)return[]}else throw new TypeError(`string-find-heads-tails: [THROW_ID_06] the second input argument, heads, contains non-string elements! For example, element at ${o}th index is ${typeof c}, equal to:
${JSON.stringify(c,null,4)}. Whole heads array looks like:
${JSON.stringify(r,null,4)}`);if(!p(n)&&!Array.isArray(n)){if(i.relaxedAPI)return[];throw new TypeError(`string-find-heads-tails: [THROW_ID_08] the third input argument, tails, must be either a string or an array of strings! Currently it's: ${typeof n}, equal to:
${JSON.stringify(n,null,4)}`)}else if(p(n))if(n.length===0){if(i.relaxedAPI)return[];throw new TypeError("string-find-heads-tails: [THROW_ID_09] the third input argument, tails, must be a non-empty string! Currently it's empty.")}else n=I(n);else if(Array.isArray(n))if(n.length===0){if(i.relaxedAPI)return[];throw new TypeError("string-find-heads-tails: [THROW_ID_10] the third input argument, tails, must be a non-empty array and contain at least one string! Currently it's empty.")}else if(n.every((t,d)=>(c=t,o=d,p(t)))){if(!n.every((t,d)=>(o=d,p(t)&&t.length&&t.trim()!=="")))if(i.relaxedAPI){if(n=n.filter(t=>p(t)&&t.length),n.length===0)return[]}else throw new TypeError(`string-find-heads-tails: [THROW_ID_12] the third input argument, tails, should not contain empty strings! For example, there's one detected at index ${o}. Whole tails array is equal to:
${JSON.stringify(n,null,4)}`)}else if(i.relaxedAPI){if(n=n.filter(t=>p(t)&&t.length),n.length===0)return[]}else throw new TypeError(`string-find-heads-tails: [THROW_ID_11] the third input argument, tails, contains non-string elements! For example, element at ${o}th index is ${typeof c}, equal to:
${JSON.stringify(c,null,4)}. Whole tails array is equal to:
${JSON.stringify(n,null,4)}`);let a=i.source===j.source;if(i.throwWhenSomethingWrongIsDetected&&!i.allowWholeValueToBeOnlyHeadsOrTails){if(I(r).includes(e))throw new Error(`${i.source}${a?": [THROW_ID_16]":""} the whole input string can't be equal to ${p(r)?"":"one of "}heads (${e})!`);if(I(n).includes(e))throw new Error(`${i.source}${a?": [THROW_ID_17]":""} the whole input string can't be equal to ${p(n)?"":"one of "}tails (${e})!`)}let s=r.concat(n).map(t=>t.charAt(0)).reduce((t,d)=>d.charCodeAt(0)>t[1]?[t[0],d.charCodeAt(0)]:d.charCodeAt(0)<t[0]?[d.charCodeAt(0),t[1]]:t,[r[0].charCodeAt(0),r[0].charCodeAt(0)]),l=[],g=!1,u={},m="",h;for(let t=i.fromIndex,d=e.length;t<d;t++){let $=e[t].charCodeAt(0);if($<=s[1]&&$>=s[0]){let y=R(e,t,r);if(y&&i.matchHeadsAndTailsStrictlyInPairsByTheirOrder){for(let w=r.length;w--;)if(r[w]===y){h=w;break}}if(p(y))if(g){if(i.throwWhenSomethingWrongIsDetected)throw new TypeError(`${i.source}${a?": [THROW_ID_19]":""} When processing "${e}", we found heads (${e.slice(t,t+y.length)}) starting at character with index number "${t}" and there was another set of heads before it! Generally speaking, there should be "heads-tails-heads-tails", not "heads-heads-tails"!
We're talking about the area of the code:
--------------------------------------starts
${e.slice(Math.max(t-200,0),t)}
\x1B[33m------->\x1B[39m ${`\x1B[31m${e.slice(t,t+y.length)}\x1B[39m`} \x1B[33m<-------\x1B[39m
${e.slice(t+y.length,Math.min(d,t+200))}
--------------------------------------ends
To turn off this error being thrown, set resolvedOpts.throwWhenSomethingWrongIsDetected to Boolean false.`)}else{u={},u.headsStartAt=t,u.headsEndAt=t+y.length,g=!0,t+=y.length-1,m&&(m="");continue}let A=R(e,t,n);if(g&&A&&i.matchHeadsAndTailsStrictlyInPairsByTheirOrder&&h!==void 0&&n[h]!==void 0&&n[h]!==A){let w;for(let b=n.length;b--;)if(n[b]===A){w=b;break}throw new TypeError(`${i.source}${a?": [THROW_ID_20]":""} When processing "${e}", we had "resolvedOpts.matchHeadsAndTailsStrictlyInPairsByTheirOrder" on. We found heads (${r[h]}) but the tails the followed it were not of the same index, ${h} (${n[h]}) but ${w} (${A}).`)}if(p(A))if(g){u.tailsStartAt=t,u.tailsEndAt=t+A.length,l.push(u),u={},g=!1,t+=A.length-1;continue}else i.throwWhenSomethingWrongIsDetected&&(m=`${i.source}${a?": [THROW_ID_21]":""} When processing "${e}", we found tails (${e.slice(t,t+A.length)}) starting at character with index number "${t}" but there were no heads preceding it. That's very naughty!`)}if(i.throwWhenSomethingWrongIsDetected&&t===d-1){if(Object.keys(u).length!==0)throw new TypeError(`${i.source}${a?": [THROW_ID_22]":""} When processing "${e}", we reached the end of the string and yet didn't find any tails (${JSON.stringify(n,null,4)}) to match the last detected heads (${e.slice(u.headsStartAt,u.headsEndAt)})!`);if(m)throw new Error(m)}}return l}return X(ae);})();
/**
* @name codsen-utils
* @fileoverview Various utility functions
* @version 1.6.8
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/codsen-utils/}
*/
/**
* @name arrayiffy-if-string
* @fileoverview Put non-empty strings into arrays, turn empty-ones into empty arrays. Bypass everything else.
* @version 5.0.10
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/arrayiffy-if-string/}
*/
/**
* @name string-match-left-right
* @fileoverview Match substrings on the left or right of a given index, ignoring whitespace
* @version 9.0.23
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-match-left-right/}
*/