string-remove-duplicate-heads-tails
Version:
Detect and (recursively) remove head and tail wrappings around the input string
94 lines (92 loc) • 26.7 kB
JavaScript
/**
* @name string-remove-duplicate-heads-tails
* @fileoverview Detect and (recursively) remove head and tail wrappings around the input string
* @version 7.0.23
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-remove-duplicate-heads-tails/}
*/
;var stringRemoveDuplicateHeadsTails=(()=>{var se=Object.create;var _=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var oe=Object.getOwnPropertyNames,W=Object.getOwnPropertySymbols,ae=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty,le=Object.prototype.propertyIsEnumerable;var V=(e,t,i)=>t in e?_(e,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):e[t]=i,$=(e,t)=>{for(var i in t||(t={}))L.call(t,i)&&V(e,i,t[i]);if(W)for(var i of W(t))le.call(t,i)&&V(e,i,t[i]);return e};var ue=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),ce=(e,t)=>{for(var i in t)_(e,i,{get:t[i],enumerable:!0})},q=(e,t,i,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of oe(t))!L.call(e,c)&&c!==i&&_(e,c,{get:()=>t[c],enumerable:!(n=ie(t,c))||n.enumerable});return e};var fe=(e,t,i)=>(i=e!=null?se(ae(e)):{},q(t||!e||!e.__esModule?_(i,"default",{value:e,enumerable:!0}):i,e)),ge=e=>q(_({},"__esModule",{value:!0}),e);var B=(e,t,i)=>V(e,typeof t!="symbol"?t+"":t,i);var U=ue((Ne,P)=>{"use strict";P.exports=he;function v(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function he(e){if(e=e||{},e.circles)return me(e);let t=new Map;if(t.set(Date,o=>new Date(o)),t.set(Map,(o,u)=>new Map(n(Array.from(o),u))),t.set(Set,(o,u)=>new Set(n(Array.from(o),u))),e.constructorHandlers)for(let o of e.constructorHandlers)t.set(o[0],o[1]);let i=null;return e.proto?r:c;function n(o,u){let a=Object.keys(o),s=new Array(a.length);for(let l=0;l<a.length;l++){let f=a[l],p=o[f];typeof p!="object"||p===null?s[f]=p:p.constructor!==Object&&(i=t.get(p.constructor))?s[f]=i(p,u):ArrayBuffer.isView(p)?s[f]=v(p):s[f]=u(p)}return s}function c(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return n(o,c);if(o.constructor!==Object&&(i=t.get(o.constructor)))return i(o,c);let u={};for(let a in o){if(Object.hasOwnProperty.call(o,a)===!1)continue;let s=o[a];typeof s!="object"||s===null?u[a]=s:s.constructor!==Object&&(i=t.get(s.constructor))?u[a]=i(s,c):ArrayBuffer.isView(s)?u[a]=v(s):u[a]=c(s)}return u}function r(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return n(o,r);if(o.constructor!==Object&&(i=t.get(o.constructor)))return i(o,r);let u={};for(let a in o){let s=o[a];typeof s!="object"||s===null?u[a]=s:s.constructor!==Object&&(i=t.get(s.constructor))?u[a]=i(s,r):ArrayBuffer.isView(s)?u[a]=v(s):u[a]=r(s)}return u}}function me(e){let t=[],i=[],n=new Map;if(n.set(Date,a=>new Date(a)),n.set(Map,(a,s)=>new Map(r(Array.from(a),s))),n.set(Set,(a,s)=>new Set(r(Array.from(a),s))),e.constructorHandlers)for(let a of e.constructorHandlers)n.set(a[0],a[1]);let c=null;return e.proto?u:o;function r(a,s){let l=Object.keys(a),f=new Array(l.length);for(let p=0;p<l.length;p++){let h=l[p],g=a[h];if(typeof g!="object"||g===null)f[h]=g;else if(g.constructor!==Object&&(c=n.get(g.constructor)))f[h]=c(g,s);else if(ArrayBuffer.isView(g))f[h]=v(g);else{let m=t.indexOf(g);m!==-1?f[h]=i[m]:f[h]=s(g)}}return f}function o(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return r(a,o);if(a.constructor!==Object&&(c=n.get(a.constructor)))return c(a,o);let s={};t.push(a),i.push(s);for(let l in a){if(Object.hasOwnProperty.call(a,l)===!1)continue;let f=a[l];if(typeof f!="object"||f===null)s[l]=f;else if(f.constructor!==Object&&(c=n.get(f.constructor)))s[l]=c(f,o);else if(ArrayBuffer.isView(f))s[l]=v(f);else{let p=t.indexOf(f);p!==-1?s[l]=i[p]:s[l]=o(f)}}return t.pop(),i.pop(),s}function u(a){if(typeof a!="object"||a===null)return a;if(Array.isArray(a))return r(a,u);if(a.constructor!==Object&&(c=n.get(a.constructor)))return c(a,u);let s={};t.push(a),i.push(s);for(let l in a){let f=a[l];if(typeof f!="object"||f===null)s[l]=f;else if(f.constructor!==Object&&(c=n.get(f.constructor)))s[l]=c(f,u);else if(ArrayBuffer.isView(f))s[l]=v(f);else{let p=t.indexOf(f);p!==-1?s[l]=i[p]:s[l]=u(f)}}return t.pop(),i.pop(),s}}});var De={};ce(De,{defaults:()=>ne,remDup:()=>Ee,version:()=>Ae});var Q=fe(U(),1);var Se=(0,Q.default)();function R(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 E(e){return typeof e=="string"}function D(e){return Number.isSafeInteger(e)&&e>=0}function M(e){return e!=null}function G(e,t){return R(e)&&E(t)&&t in e}function S(e){return typeof e!="string"?e:e.length?[e]:[]}var z={cb:void 0,i:!1,trimBeforeMatching:!1,trimCharsBeforeMatching:[],maxMismatches:0,firstMustMatch:!1,lastMustMatch:!1,hungry:!1},pe=e=>e+1;function de(e,t,i,n,c=!1,r=pe){var w;let o=typeof i=="function"?i():i;if(+t<0&&c&&o==="EOL")return o;let u=$($({},z),n);if(t>=e.length&&!c)return!1;let a=c?1:i.length,s=0,l=!1,f=!1,p=!1,h=u.maxMismatches,g=t,m=!1,T=!1,d=!1;function O(){return s===1&&h<u.maxMismatches-1}for(;e[g];){let y=r(g);if(u.trimBeforeMatching&&e[g].trim()===""){if(!e[y]&&c&&i==="EOL")return!0;g=r(g);continue}if(u&&!u.i&&((w=u==null?void 0:u.trimCharsBeforeMatching)!=null&&w.includes(e[g]))||u!=null&&u.i&&u.trimCharsBeforeMatching&&u.trimCharsBeforeMatching.map(b=>b.toLowerCase()).includes(e[g].toLowerCase())){if(c&&i==="EOL"&&!e[y])return!0;g=r(g);continue}let A=y>g?i[i.length-a]:i[a-1];if(!u.i&&e[g]===A||u.i&&e[g].toLowerCase()===A.toLowerCase()){if(m||(m=!0),p||(p=!0),a===i.length){if(T=!0,h!==u.maxMismatches)return!1}else a===1&&(d=!0);if(a-=1,s++,O())return!1;if(!a)return s!==i.length||h===u.maxMismatches||!l?g:!1}else if(!l&&!s&&(l=!0),u.maxMismatches&&h&&g){h-=1;for(let b=0;b<=h;b++){let N=y>g?i[i.length-a+1+b]:i[a-2-b],C=e[r(g)];if(N&&(!u.i&&e[g]===N||u.i&&e[g].toLowerCase()===N.toLowerCase())&&(!u.firstMustMatch||a!==i.length)){if(s++,O())return!1;a-=2,m=!0;break}else if(C&&N&&(!u.i&&C===N||u.i&&C.toLowerCase()===N.toLowerCase())&&(!u.firstMustMatch||a!==i.length)){if(!s&&!u.hungry)return!1;a-=1,m=!0;break}else if(N===void 0&&h>=0&&m&&(!u.firstMustMatch||T)&&(!u.lastMustMatch||d))return g}m||(f=g)}else return g===0&&a===1&&!u.lastMustMatch&&p?0:!1;if(f!==!1&&f!==g&&(f=!1),a<1)return g;g=r(g)}if(a>0)return c&&o==="EOL"?!0:u&&u.maxMismatches>=a&&p?f||0:!1}function K(e,t,i,n,c){var l,f,p;if(R(c)&&G(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 r=$($({},z),c);if(typeof r.trimCharsBeforeMatching=="string"&&(r.trimCharsBeforeMatching=S(r.trimCharsBeforeMatching)),r.trimCharsBeforeMatching=r.trimCharsBeforeMatching.map(h=>E(h)?h:String(h)),!E(t)||!t.length)return!1;if(!Number.isInteger(i)||i<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 i}, equal to:
${JSON.stringify(i,null,4)}`);let o,u;if(E(n))o=[n];else if(Array.isArray(n))o=n;else if(!n)o=n;else if(typeof n=="function")o=[],o.push(n);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 n}, equal to:
${JSON.stringify(n,null,4)}`);if(c&&!R(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 a=0,s="";if((l=r==null?void 0:r.trimCharsBeforeMatching)!=null&&l.some((h,g)=>h.length>1?(a=g,s=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 ${a} is longer than 1 character, ${s.length} (equals to ${s}). 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&&E(o[0])&&!o[0].trim()){if(typeof r.cb=="function"){let g,m=i;if((e==="matchLeftIncl"||e==="matchRight")&&(m+=1),e[5]==="L")for(let w=m;w--;){let y=t[w];if((!r.trimBeforeMatching||r.trimBeforeMatching&&(y!=null&&y.trim()))&&(!((f=r.trimCharsBeforeMatching)!=null&&f.length)||y!==void 0&&!r.trimCharsBeforeMatching.includes(y))){g=w;break}}else if(e.startsWith("matchRight"))for(let w=m;w<t.length;w++){let y=t[w];if((!r.trimBeforeMatching||r.trimBeforeMatching&&y.trim())&&(!((p=r.trimCharsBeforeMatching)!=null&&p.length)||!r.trimCharsBeforeMatching.includes(y))){g=w;break}}if(g===void 0)return!1;let T=t[g],d=g+1,O="";return d&&d>0&&(O=t.slice(0,d)),e[5]==="L"||g&&g>0&&(O=t.slice(g)),r.cb(T,O,g)}let h="";throw c||(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,g=o.length;h<g;h++){u=typeof o[h]=="function";let m=o[h],T,d,O="",w=i;e==="matchRight"?w+=1:e==="matchLeft"&&(w-=1);let y=de(t,w,m,r,u,A=>e[5]==="L"?A-1:A+1);if(y&&u&&typeof m=="function"&&m()==="EOL")return m()&&(!r.cb||r.cb(T,O,d))?m():!1;if(Number.isInteger(y)&&(d=e.startsWith("matchLeft")?y-1:y+1,e[5]==="L"?O=t.slice(0,y):O=t.slice(d)),d<0&&(d=void 0),t[d]&&(T=t[d]),Number.isInteger(y)&&(!r.cb||r.cb(T,O,d)))return m}return!1}function x(e,t,i,n){return K("matchLeftIncl",e,t,i,n)}function I(e,t,i,n){return K("matchRightIncl",e,t,i,n)}function j(e,t=1){let i="\xA0";function n(r){return Array.from(r).reverse().join("")}function c(r,o,u){let a=u?`
`:"\r",s=u?"\r":`
`;if(!r)return r;let l=0,f=0,p="";for(let h=0,g=r.length;h<g;h++)(r[h]===a||r[h]===s&&r[h-1]!==a)&&f++,`\r
`.includes(r[h])||r[h]===i?(l=0,r[h]===i?p+=r[h]:r[h]===a?f<=o&&(p+=r[h],r[h+1]===s&&(p+=r[h+1],h++)):r[h]===s&&(r==null?void 0:r[h-1])!==a&&f<=o&&(p+=r[h])):(l++,!r[h+1]&&!f&&(p+=" "));return p}if(typeof e=="string"&&e.length){let r=1;typeof+t=="number"&&Number.isInteger(+t)&&+t>=0&&(r=+t);let o="",u="";if(!e.trim())o=e;else if(!e[0].trim()){for(let a=0,s=e.length;a<s;a++)if(e[a].trim()){o=e.slice(0,a);break}}if(e.trim()&&(e.slice(-1).trim()===""||e.slice(-1)===i)){for(let a=e.length;a--;)if(e[a].trim()){u=e.slice(a+1);break}}return`${c(o,r,!1)}${e.trim()}${n(c(n(u),r,!0))}`}return e}var ye={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function k(e,t){if(!Array.isArray(e)||!e.length)return e;let i=$($({},ye),t),n,c;if(i.strictlyTwoElementsInRangeArrays&&!e.every((u,a)=>!Array.isArray(u)||u.length!==2?(n=a,c=u.length,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_03] The first argument should be an array and must consist of arrays which are natural number indexes representing TWO string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) has not two but ${c} elements!`);if(!e.every((u,a)=>!Array.isArray(u)||!Number.isInteger(u[0])||u[0]<0||!Number.isInteger(u[1])||u[1]<0?(n=a,!1):!0))throw new TypeError(`ranges-sort: [THROW_ID_04] The first argument should be an array and must consist of arrays which are natural number indexes representing string index ranges. However, ${n}th range (${JSON.stringify(e[n],null,4)}) does not consist of only natural numbers!`);let r=e.length**2,o=0;return Array.from(e).sort((u,a)=>(i.progressFn&&(o+=1,i.progressFn(Math.floor(o*100/r))),u[0]===a[0]?u[1]<a[1]?-1:u[1]>a[1]?1:0:u[0]<a[0]?-1:1))}var Z={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function $e(e,t){var s;function i(l){return!!l&&typeof l=="object"&&!Array.isArray(l)}if(!Array.isArray(e)||!e.length)return null;let n;if(t)if(i(t)){if(n=$($({},Z),t),n.progressFn&&i(n.progressFn)&&!Object.keys(n.progressFn).length)n.progressFn=null;else if(n.progressFn&&typeof n.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] resolvedOpts.progressFn must be a function! It was given of a type: "${typeof n.progressFn}", equal to ${JSON.stringify(n.progressFn,null,4)}`);if(![1,2,"1","2"].includes(n.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] resolvedOpts.mergeType was customised to a wrong thing! It was given of a type: "${typeof n.mergeType}", equal to ${JSON.stringify(n.mergeType,null,4)}`);if(typeof n.joinRangesThatTouchEdges!="boolean")throw new Error(`ranges-merge: [THROW_ID_04] resolvedOpts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof n.joinRangesThatTouchEdges}", equal to ${JSON.stringify(n.joinRangesThatTouchEdges,null,4)}`)}else throw new Error(`ranges-merge: [THROW_ID_03] the second input argument must be a plain object. It was given as:
${JSON.stringify(t,null,4)} (type ${typeof t})`);else n=$({},Z);let c=e.filter(l=>Array.isArray(l)).map(l=>[...l]).filter(l=>l[2]!==void 0||l[0]!==l[1]),r,o,u;n.progressFn?r=k(c,{progressFn:l=>{u=Math.floor(l/5),u!==o&&(o=u,n.progressFn!=null&&n.progressFn(u))}}):r=k(c);let a=r.length-1;for(let l=a;l>0;l--)n.progressFn&&(u=Math.floor((1-l/a)*78)+21,u!==o&&u>o&&(o=u,n.progressFn(u))),(r[l][0]<=r[l-1][0]||!n.joinRangesThatTouchEdges&&r[l][0]<r[l-1][1]||n.joinRangesThatTouchEdges&&r[l][0]<=r[l-1][1])&&(r[l-1][0]=Math.min(r[l][0],r[l-1][0]),r[l-1][1]=Math.max(r[l][1],r[l-1][1]),r[l][2]!==void 0&&(r[l-1][0]>=r[l][0]||r[l-1][1]<=r[l][1])&&r[l-1][2]!==null&&(r[l][2]===null&&r[l-1][2]!==null?r[l-1][2]=null:r[l-1][2]!=null?+((s=n||{})==null?void 0:s.mergeType)==2&&r[l-1][0]===r[l][0]?r[l-1][2]=r[l][2]:r[l-1][2]+=r[l][2]:r[l-1][2]=r[l][2]),r.splice(l,1),l=r.length);return r.length?r:null}var be={limitToBeAddedWhitespace:!1,limitLinebreaksCount:1,mergeType:1},J=class{constructor(e){B(this,"ranges");B(this,"opts");let t=$($({},be),e);if(t.mergeType&&t.mergeType!==1&&t.mergeType!==2)if(E(t.mergeType)&&t.mergeType.trim()==="1")t.mergeType=1;else if(E(t.mergeType)&&t.mergeType.trim()==="2")t.mergeType=2;else throw new Error(`ranges-push: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof t.mergeType}", equal to ${JSON.stringify(t.mergeType,null,4)}`);this.opts=t,this.ranges=[]}add(e,t,i){var r;if(e==null&&t==null)return;if(M(e)&&!M(t)){if(Array.isArray(e)){if(e.length){if(e.some(o=>Array.isArray(o))){e.forEach(o=>{Array.isArray(o)&&this.add(...o)});return}e.length&&D(+e[0])&&D(+e[1])&&this.add(...e)}return}throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_12] the first input argument, "from" is set (${JSON.stringify(e,null,0)}) but second-one, "to" is not (${JSON.stringify(t,null,0)})`)}else if(!M(e)&&M(t))throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_13] the second input argument, "to" is set (${JSON.stringify(t,null,0)}) but first-one, "from" is not (${JSON.stringify(e,null,0)})`);let n=+e,c=+t;if(D(i)&&(i=String(i)),D(n)&&D(c)){if(M(i)&&!E(i)&&!D(i))throw new TypeError(`ranges-push/Ranges/add(): [THROW_ID_08] The third argument, the value to add, was given not as string but ${typeof i}, equal to:
${JSON.stringify(i,null,4)}`);if(M(this.ranges)&&Array.isArray(this.last())&&n===this.last()[1]){if(this.last()[1]=c,this.last()[2],this.last()[2]!==null&&M(i)){let o=this.last()[2]&&this.last()[2].length&&(!((r=this.opts)!=null&&r.mergeType)||this.opts.mergeType===1)?`${this.last()[2]}${i}`:i;this.opts.limitToBeAddedWhitespace&&(o=j(o,this.opts.limitLinebreaksCount)),E(o)&&!o.length||(this.last()[2]=o)}}else{this.ranges||(this.ranges=[]);let o=i!==void 0&&!(E(i)&&!i.length)?[n,c,i&&this.opts.limitToBeAddedWhitespace?j(i,this.opts.limitLinebreaksCount):i]:[n,c];this.ranges.push(o)}}else throw D(n)&&n>=0?new TypeError(`ranges-push/Ranges/add(): [THROW_ID_10] "to" value, the second input argument, must be a natural number or zero! Currently it's of a type "${typeof c}" equal to: ${JSON.stringify(c,null,4)}`):new TypeError(`ranges-push/Ranges/add(): [THROW_ID_09] "from" value, the first input argument, must be a natural number or zero! Currently it's of a type "${typeof n}" equal to: ${JSON.stringify(n,null,4)}`)}push(e,t,i){this.add(e,t,i)}current(){return Array.isArray(this.ranges)&&this.ranges.length?(this.ranges=$e(this.ranges,{mergeType:this.opts.mergeType}),this.ranges&&this.opts.limitToBeAddedWhitespace?this.ranges.map(e=>M(e[2])?[e[0],e[1],j(e[2],this.opts.limitLinebreaksCount)]:e):this.ranges):null}wipe(){this.ranges=[]}replace(e){if(Array.isArray(e)&&e.length)if(Array.isArray(e[0])&&D(e[0][0]))this.ranges=Array.from(e);else throw new Error(`ranges-push/Ranges/replace(): [THROW_ID_11] Single range was given but we expected array of arrays! The first element, ${JSON.stringify(e[0],null,4)} should be an array and its first element should be an integer, a string index.`);else this.ranges=[]}last(){return Array.isArray(this.ranges)&&this.ranges.length?this.ranges[this.ranges.length-1]:null}};var X={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function Y(e,t){function i(s){return!!s&&typeof s=="object"&&!Array.isArray(s)}if(!Array.isArray(e)||!e.length)return null;let n;if(t)if(i(t)){if(n=$($({},X),t),n.progressFn&&i(n.progressFn)&&!Object.keys(n.progressFn).length)n.progressFn=null;else if(n.progressFn&&typeof n.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "${typeof n.progressFn}", equal to ${JSON.stringify(n.progressFn,null,4)}`);if(![1,2,"1","2"].includes(n.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof n.mergeType}", equal to ${JSON.stringify(n.mergeType,null,4)}`);if(typeof n.joinRangesThatTouchEdges!="boolean")throw new Error(`ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "${typeof n.joinRangesThatTouchEdges}", equal to ${JSON.stringify(n.joinRangesThatTouchEdges,null,4)}`)}else throw new Error(`emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:
${JSON.stringify(t,null,4)} (type ${typeof t})`);else n=$({},X);let c=e.filter(s=>Array.isArray(s)).map(s=>[...s]).filter(s=>s[2]!==void 0||s[0]!==s[1]),r,o,u;n.progressFn?r=k(c,{progressFn:s=>{u=Math.floor(s/5),u!==o&&(o=u,n.progressFn(u))}}):r=k(c);let a=r.length-1;for(let s=a;s>0;s--)n.progressFn&&(u=Math.floor((1-s/a)*78)+21,u!==o&&u>o&&(o=u,n.progressFn(u))),(r[s][0]<=r[s-1][0]||!n.joinRangesThatTouchEdges&&r[s][0]<r[s-1][1]||n.joinRangesThatTouchEdges&&r[s][0]<=r[s-1][1])&&(r[s-1][0]=Math.min(r[s][0],r[s-1][0]),r[s-1][1]=Math.max(r[s][1],r[s-1][1]),r[s][2]!==void 0&&(r[s-1][0]>=r[s][0]||r[s-1][1]<=r[s][1])&&r[s-1][2]!==null&&(r[s][2]===null&&r[s-1][2]!==null?r[s-1][2]=null:r[s-1][2]!=null?+n.mergeType==2&&r[s-1][0]===r[s][0]?r[s-1][2]=r[s][2]:r[s-1][2]+=r[s][2]:r[s-1][2]=r[s][2]),r.splice(s,1),s=r.length);return r.length?r:null}var Te=!0,H="Invariant failed";function ee(e,t){if(!e){if(Te)throw new Error(H);var i=typeof t=="function"?t():t,n=i?"".concat(H,": ").concat(i):H;throw new Error(n)}}function re(e,t,i){let n=0,c=0;if(arguments.length===0)throw new Error("ranges-apply: [THROW_ID_01] inputs missing!");if(typeof e!="string")throw new TypeError(`ranges-apply: [THROW_ID_02] first input argument must be a string! Currently it's: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(t&&!Array.isArray(t))throw new TypeError(`ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ${typeof t}, equal to: ${JSON.stringify(t,null,4)}`);if(i&&typeof i!="function")throw new TypeError(`ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ${typeof i}, equal to: ${JSON.stringify(i,null,4)}`);if(!(t!=null&&t.filter(l=>l).length))return e;let r;Array.isArray(t)&&Number.isInteger(t[0])&&Number.isInteger(t[1])?r=[Array.from(t)]:r=Array.from(t);let o=r.length,u=0;r.filter(l=>l).forEach((l,f)=>{if(i&&(n=Math.floor(u/o*10),n!==c&&(c=n,i(n))),!Array.isArray(l))throw new TypeError(`ranges-apply: [THROW_ID_05] ranges array, second input arg., has ${f}th element not an array: ${JSON.stringify(l,null,4)}, which is ${typeof l}`);if(!Number.isInteger(l[0])){if(!Number.isInteger(+l[0])||+l[0]<0)throw new TypeError(`ranges-apply: [THROW_ID_06] ranges array, second input arg. has ${f}th element, array ${JSON.stringify(l,null,0)}. Its first element is not an integer, string index, but ${typeof l[0]}, equal to: ${JSON.stringify(l[0],null,4)}.`);r[f][0]=+r[f][0]}if(!Number.isInteger(l[1])){if(!Number.isInteger(+l[1])||+l[1]<0)throw new TypeError(`ranges-apply: [THROW_ID_07] ranges array, second input arg. has ${f}th element, array ${JSON.stringify(l,null,0)}. Its second element is not an integer, string index, but ${typeof l[1]}, equal to: ${JSON.stringify(l[1],null,4)}.`);r[f][1]=+r[f][1]}u+=1});let a=Y(r,{progressFn:l=>{i&&(n=10+Math.floor(l/10),n!==c&&(c=n,i(n)))}});ee(a);let s=a.length;if(s>0){let l=e.slice(a[s-1][1]);e=a.reduce((f,p,h,g)=>{i&&(n=20+Math.floor(h/s*80),n!==c&&(c=n,i(n)));let m=h===0?0:g[h-1][1],T=g[h][0];return`${f}${e.slice(m,T)}${g[h][2]||""}`},""),e+=l}return e}var we={classicTrim:!1,cr:!1,lf:!1,tab:!1,space:!0,nbsp:!1};function F(e,t){if(typeof e!="string")throw new Error(`string-trim-spaces-only: [THROW_ID_01] input must be string! It was given as ${typeof e}, equal to:
${JSON.stringify(e,null,4)}`);let i=$($({},we),t);function n(o){return i.classicTrim&&!o.trim()||!i.classicTrim&&(i.space&&o===" "||i.cr&&o==="\r"||i.lf&&o===`
`||i.tab&&o===" "||i.nbsp&&o==="\xA0")}let c,r;if(e.length){if(n(e[0]))for(let o=0,u=e.length;o<u;o++){if(!n(e[o])){c=o;break}if(o===e.length-1)return{res:"",ranges:[[0,e.length]]}}if(n(e[e.length-1])){for(let o=e.length;o--;)if(!n(e[o])){r=o+1;break}}return c?r?{res:e.slice(c,r),ranges:[[0,c],[r,e.length]]}:{res:e.slice(c),ranges:[[0,c]]}:r?{res:e.slice(0,r),ranges:[[r,e.length]]}:{res:e,ranges:[]}}return{res:"",ranges:[]}}var te="7.0.23";var Ae=te,ne={heads:["{{"],tails:["}}"]};function Ee(e,t){let i=Object.prototype.hasOwnProperty;if(e===void 0)throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_01] The input is missing!");if(typeof e!="string")return e;if(t&&!R(t))throw new Error(`string-remove-duplicate-heads-tails: [THROW_ID_03] The given options are not a plain object but ${typeof t}!`);let n=$({},t);if(n&&i.call(n,"heads"))if(S(n.heads).every(m=>typeof m=="string"||Array.isArray(m)))typeof n.heads=="string"&&(n.heads=S(n.heads));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_04] The resolvedOpts.heads contains elements which are not string-type!");if(n&&i.call(n,"tails"))if(S(n.tails).every(m=>typeof m=="string"||Array.isArray(m)))typeof n.tails=="string"&&(n.tails=S(n.tails));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_05] The resolvedOpts.tails contains elements which are not string-type!");let c=F(e).res;if(c.length===0)return e;e=c;let r=$($({},ne),n);r.heads=r.heads.map(m=>m.trim()),r.tails=r.tails.map(m=>m.trim());let o=!1,u=!1,a=new J({limitToBeAddedWhitespace:!0}),s=new J({limitToBeAddedWhitespace:!0}),l=!0,f=!0,p="";function h(m,T){let d;return I(m,0,T.heads,{trimBeforeMatching:!0,cb:(y,A,b)=>(d=b,!0)})&&I(m,d,T.tails,{trimBeforeMatching:!0,cb:(y,A,b)=>(d=b,!0)})?m.slice(d):m}for(;e!==h(e,r);)e=F(h(e,r)).res;function g(m,T){let d;return!x(m,m.length-1,T.tails,{trimBeforeMatching:!0,cb:(y,A,b)=>(d=b,!0)})||!d?m:x(m,d,T.heads,{trimBeforeMatching:!0,cb:(y,A,b)=>(d=b,!0)})?m.slice(0,d+1):m}for(;e!==g(e,r);)e=F(g(e,r)).res;if(!r.heads.length||!I(e,0,r.heads,{trimBeforeMatching:!0})||!r.tails.length||!x(e,e.length-1,r.tails,{trimBeforeMatching:!0}))return F(e).res;for(let m=0,T=e.length;m<T;m++)if(e[m].trim()!==""){let d;if(I(e,m,r.heads,{trimBeforeMatching:!0,cb:(y,A,b)=>(d=b,!0)})&&d){f=!0,l&&(l=!0);let y;I(e,d,r.tails,{trimBeforeMatching:!0,cb:(b,N,C)=>(y=C,!0)})&&a.push(m,y),s.current()&&o&&p!=="tails"&&a.push(s.current()),o||s.current()&&(a.push(s.current()),s.wipe()),s.push(m,d),p="heads",m=d-1;continue}if(I(e,m,r.tails,{trimBeforeMatching:!0,cb:(y,A,b)=>(d=Number.isInteger(b)?b:e.length,!0)})&&d){f=!0,l?(p==="heads"&&s.wipe(),l=!1):s.push(m,d),p="tails",m=d-1;continue}l&&(l=!0),f&&!o?(o=!0,f=!1):f&&!u?(u=!0,l=!0,f=!1,p==="heads"&&s.wipe()):f&&u&&s.wipe()}return s.current()&&a.push(s.current()),a.current()?re(e,a.current()).trim():e.trim()}return ge(De);})();
/**
* @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/}
*/
/**
* @name string-collapse-leading-whitespace
* @fileoverview Collapse the leading and trailing whitespace of a string
* @version 7.0.9
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-collapse-leading-whitespace/}
*/
/**
* @name ranges-sort
* @fileoverview Sort string index ranges
* @version 6.0.14
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-sort/}
*/
/**
* @name ranges-push
* @fileoverview Gather string index ranges
* @version 7.0.19
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-push/}
*/
/**
* @name ranges-merge
* @fileoverview Merge and sort string index ranges
* @version 9.0.19
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-merge/}
*/
/**
* @name ranges-apply
* @fileoverview Take an array of string index ranges, delete/replace the string according to them
* @version 7.0.20
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-apply/}
*/
/**
* @name string-trim-spaces-only
* @fileoverview Like String.trim() but you can choose granularly what to trim
* @version 5.0.13
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-trim-spaces-only/}
*/