UNPKG

string-match-left-right

Version:

Match substrings on the left or right of a given index, ignoring whitespace

30 lines (28 loc) 10.4 kB
/** * @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/} */ "use strict";var stringMatchLeftRight=(()=>{var W=Object.create;var w=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames,B=Object.getOwnPropertySymbols,Q=Object.getPrototypeOf,x=Object.prototype.hasOwnProperty,Y=Object.prototype.propertyIsEnumerable;var L=(e,t,n)=>t in e?w(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,O=(e,t)=>{for(var n in t||(t={}))x.call(t,n)&&L(e,n,t[n]);if(B)for(var n of B(t))Y.call(t,n)&&L(e,n,t[n]);return e};var K=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),G=(e,t)=>{for(var n in t)w(e,n,{get:t[n],enumerable:!0})},I=(e,t,n,h)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of q(t))!x.call(e,c)&&c!==n&&w(e,c,{get:()=>t[c],enumerable:!(h=_(t,c))||h.enumerable});return e};var Z=(e,t,n)=>(n=e!=null?W(Q(e)):{},I(t||!e||!e.__esModule?w(n,"default",{value:e,enumerable:!0}):n,e)),z=e=>I(w({},"__esModule",{value:!0}),e);var F=K((he,J)=>{"use strict";J.exports=X;function V(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function X(e){if(e=e||{},e.circles)return ee(e);let t=new Map;if(t.set(Date,o=>new Date(o)),t.set(Map,(o,s)=>new Map(h(Array.from(o),s))),t.set(Set,(o,s)=>new Set(h(Array.from(o),s))),e.constructorHandlers)for(let o of e.constructorHandlers)t.set(o[0],o[1]);let n=null;return e.proto?u:c;function h(o,s){let r=Object.keys(o),i=new Array(r.length);for(let f=0;f<r.length;f++){let l=r[f],g=o[l];typeof g!="object"||g===null?i[l]=g:g.constructor!==Object&&(n=t.get(g.constructor))?i[l]=n(g,s):ArrayBuffer.isView(g)?i[l]=V(g):i[l]=s(g)}return i}function c(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return h(o,c);if(o.constructor!==Object&&(n=t.get(o.constructor)))return n(o,c);let s={};for(let r in o){if(Object.hasOwnProperty.call(o,r)===!1)continue;let i=o[r];typeof i!="object"||i===null?s[r]=i:i.constructor!==Object&&(n=t.get(i.constructor))?s[r]=n(i,c):ArrayBuffer.isView(i)?s[r]=V(i):s[r]=c(i)}return s}function u(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return h(o,u);if(o.constructor!==Object&&(n=t.get(o.constructor)))return n(o,u);let s={};for(let r in o){let i=o[r];typeof i!="object"||i===null?s[r]=i:i.constructor!==Object&&(n=t.get(i.constructor))?s[r]=n(i,u):ArrayBuffer.isView(i)?s[r]=V(i):s[r]=u(i)}return s}}function ee(e){let t=[],n=[],h=new Map;if(h.set(Date,r=>new Date(r)),h.set(Map,(r,i)=>new Map(u(Array.from(r),i))),h.set(Set,(r,i)=>new Set(u(Array.from(r),i))),e.constructorHandlers)for(let r of e.constructorHandlers)h.set(r[0],r[1]);let c=null;return e.proto?s:o;function u(r,i){let f=Object.keys(r),l=new Array(f.length);for(let g=0;g<f.length;g++){let $=f[g],a=r[$];if(typeof a!="object"||a===null)l[$]=a;else if(a.constructor!==Object&&(c=h.get(a.constructor)))l[$]=c(a,i);else if(ArrayBuffer.isView(a))l[$]=V(a);else{let m=t.indexOf(a);m!==-1?l[$]=n[m]:l[$]=i(a)}}return l}function o(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return u(r,o);if(r.constructor!==Object&&(c=h.get(r.constructor)))return c(r,o);let i={};t.push(r),n.push(i);for(let f in r){if(Object.hasOwnProperty.call(r,f)===!1)continue;let l=r[f];if(typeof l!="object"||l===null)i[f]=l;else if(l.constructor!==Object&&(c=h.get(l.constructor)))i[f]=c(l,o);else if(ArrayBuffer.isView(l))i[f]=V(l);else{let g=t.indexOf(l);g!==-1?i[f]=n[g]:i[f]=o(l)}}return t.pop(),n.pop(),i}function s(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return u(r,s);if(r.constructor!==Object&&(c=h.get(r.constructor)))return c(r,s);let i={};t.push(r),n.push(i);for(let f in r){let l=r[f];if(typeof l!="object"||l===null)i[f]=l;else if(l.constructor!==Object&&(c=h.get(l.constructor)))i[f]=c(l,s);else if(ArrayBuffer.isView(l))i[f]=V(l);else{let g=t.indexOf(l);g!==-1?i[f]=n[g]:i[f]=s(l)}}return t.pop(),n.pop(),i}}});var le={};G(le,{defaultGetNextIdx:()=>U,defaults:()=>k,matchLeft:()=>se,matchLeftIncl:()=>oe,matchRight:()=>ae,matchRightIncl:()=>ie,version:()=>re});function j(e){return typeof e!="string"?e:e.length?[e]:[]}var v=Z(F(),1);var fe=(0,v.default)();function A(e){if(e==null||typeof e!="object")return!1;let t=Object.getPrototypeOf(e);return t!==null&&t!==Object.prototype&&Object.getPrototypeOf(t)!==null?!1:!(Symbol.iterator in e)&&!(Symbol.toStringTag in e)}function M(e){return typeof e=="string"}function H(e,t){return A(e)&&M(t)&&t in e}var P="9.0.23";var re=P,k={cb:void 0,i:!1,trimBeforeMatching:!1,trimCharsBeforeMatching:[],maxMismatches:0,firstMustMatch:!1,lastMustMatch:!1,hungry:!1},U=e=>e+1;function ne(e,t,n,h,c=!1,u=U){var b;let o=typeof n=="function"?n():n;if(+t<0&&c&&o==="EOL")return o;let s=O(O({},k),h);if(t>=e.length&&!c)return!1;let r=c?1:n.length,i=0,f=!1,l=!1,g=!1,$=s.maxMismatches,a=t,m=!1,d=!1,p=!1;function E(){return i===1&&$<s.maxMismatches-1}for(;e[a];){let y=u(a);if(s.trimBeforeMatching&&e[a].trim()===""){if(!e[y]&&c&&n==="EOL")return!0;a=u(a);continue}if(s&&!s.i&&((b=s==null?void 0:s.trimCharsBeforeMatching)!=null&&b.includes(e[a]))||s!=null&&s.i&&s.trimCharsBeforeMatching&&s.trimCharsBeforeMatching.map(T=>T.toLowerCase()).includes(e[a].toLowerCase())){if(c&&n==="EOL"&&!e[y])return!0;a=u(a);continue}let N=y>a?n[n.length-r]:n[r-1];if(!s.i&&e[a]===N||s.i&&e[a].toLowerCase()===N.toLowerCase()){if(m||(m=!0),g||(g=!0),r===n.length){if(d=!0,$!==s.maxMismatches)return!1}else r===1&&(p=!0);if(r-=1,i++,E())return!1;if(!r)return i!==n.length||$===s.maxMismatches||!f?a:!1}else if(!f&&!i&&(f=!0),s.maxMismatches&&$&&a){$-=1;for(let T=0;T<=$;T++){let D=y>a?n[n.length-r+1+T]:n[r-2-T],R=e[u(a)];if(D&&(!s.i&&e[a]===D||s.i&&e[a].toLowerCase()===D.toLowerCase())&&(!s.firstMustMatch||r!==n.length)){if(i++,E())return!1;r-=2,m=!0;break}else if(R&&D&&(!s.i&&R===D||s.i&&R.toLowerCase()===D.toLowerCase())&&(!s.firstMustMatch||r!==n.length)){if(!i&&!s.hungry)return!1;r-=1,m=!0;break}else if(D===void 0&&$>=0&&m&&(!s.firstMustMatch||d)&&(!s.lastMustMatch||p))return a}m||(l=a)}else return a===0&&r===1&&!s.lastMustMatch&&g?0:!1;if(l!==!1&&l!==a&&(l=!1),r<1)return a;a=u(a)}if(r>0)return c&&o==="EOL"?!0:s&&s.maxMismatches>=r&&g?l||0:!1}function S(e,t,n,h,c){var f,l,g;if(A(c)&&H(c,"trimBeforeMatching")&&c&&typeof c.trimBeforeMatching!="boolean")throw new Error(`string-match-left-right/${e}(): [THROW_ID_09] opts.trimBeforeMatching should be boolean!${Array.isArray(c.trimBeforeMatching)?" Did you mean to use opts.trimCharsBeforeMatching?":""}`);let u=O(O({},k),c);if(typeof u.trimCharsBeforeMatching=="string"&&(u.trimCharsBeforeMatching=j(u.trimCharsBeforeMatching)),u.trimCharsBeforeMatching=u.trimCharsBeforeMatching.map($=>M($)?$:String($)),!M(t)||!t.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,s;if(M(h))o=[h];else if(Array.isArray(h))o=h;else if(!h)o=h;else if(typeof h=="function")o=[],o.push(h);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 h}, equal to: ${JSON.stringify(h,null,4)}`);if(c&&!A(c))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 c}", and equal to: ${JSON.stringify(c,null,4)}`);let r=0,i="";if((f=u==null?void 0:u.trimCharsBeforeMatching)!=null&&f.some(($,a)=>$.length>1?(r=a,i=$,!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 ${r} is longer than 1 character, ${i.length} (equals to ${i}). 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&&M(o[0])&&!o[0].trim()){if(typeof u.cb=="function"){let a,m=n;if((e==="matchLeftIncl"||e==="matchRight")&&(m+=1),e[5]==="L")for(let C=m;C--;){let b=t[C];if((!u.trimBeforeMatching||u.trimBeforeMatching&&(b!=null&&b.trim()))&&(!((l=u.trimCharsBeforeMatching)!=null&&l.length)||b!==void 0&&!u.trimCharsBeforeMatching.includes(b))){a=C;break}}else if(e.startsWith("matchRight"))for(let C=m;C<t.length;C++){let b=t[C];if((!u.trimBeforeMatching||u.trimBeforeMatching&&b.trim())&&(!((g=u.trimCharsBeforeMatching)!=null&&g.length)||!u.trimCharsBeforeMatching.includes(b))){a=C;break}}if(a===void 0)return!1;let d=t[a],p=a+1,E="";return p&&p>0&&(E=t.slice(0,p)),e[5]==="L"||a&&a>0&&(E=t.slice(a)),u.cb(d,E,a)}let $="";throw c||($=" 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!${$}`)}for(let $=0,a=o.length;$<a;$++){s=typeof o[$]=="function";let m=o[$],d,p,E="",C=n;e==="matchRight"?C+=1:e==="matchLeft"&&(C-=1);let b=ne(t,C,m,u,s,y=>e[5]==="L"?y-1:y+1);if(b&&s&&typeof m=="function"&&m()==="EOL")return m()&&(!u.cb||u.cb(d,E,p))?m():!1;if(Number.isInteger(b)&&(p=e.startsWith("matchLeft")?b-1:b+1,e[5]==="L"?E=t.slice(0,b):E=t.slice(p)),p<0&&(p=void 0),t[p]&&(d=t[p]),Number.isInteger(b)&&(!u.cb||u.cb(d,E,p)))return m}return!1}function oe(e,t,n,h){return S("matchLeftIncl",e,t,n,h)}function se(e,t,n,h){return S("matchLeft",e,t,n,h)}function ie(e,t,n,h){return S("matchRightIncl",e,t,n,h)}function ae(e,t,n,h){return S("matchRight",e,t,n,h)}return z(le);})(); /** * @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 codsen-utils * @fileoverview Various utility functions * @version 1.6.8 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/codsen-utils/} */