UNPKG

string-collapse-white-space

Version:

Replace chunks of whitespace with a single spaces

67 lines (65 loc) 21.6 kB
/** * @name string-collapse-white-space * @fileoverview Replace chunks of whitespace with a single spaces * @version 11.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-collapse-white-space/} */ "use strict";var stringCollapseWhiteSpace=(()=>{var Y=Object.create;var x=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var ee=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols,te=Object.getPrototypeOf,W=Object.prototype.hasOwnProperty,re=Object.prototype.propertyIsEnumerable;var R=(e,r,a)=>r in e?x(e,r,{enumerable:!0,configurable:!0,writable:!0,value:a}):e[r]=a,h=(e,r)=>{for(var a in r||(r={}))W.call(r,a)&&R(e,a,r[a]);if(C)for(var a of C(r))re.call(r,a)&&R(e,a,r[a]);return e};var ne=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports),se=(e,r)=>{for(var a in r)x(e,a,{get:r[a],enumerable:!0})},J=(e,r,a,s)=>{if(r&&typeof r=="object"||typeof r=="function")for(let u of ee(r))!W.call(e,u)&&u!==a&&x(e,u,{get:()=>r[u],enumerable:!(s=X(r,u))||s.enumerable});return e};var F=(e,r,a)=>(a=e!=null?Y(te(e)):{},J(r||!e||!e.__esModule?x(a,"default",{value:e,enumerable:!0}):a,e)),ie=e=>J(x({},"__esModule",{value:!0}),e);var I=(e,r,a)=>R(e,typeof r!="symbol"?r+"":r,a);var V=ne((Ne,q)=>{"use strict";q.exports=ae;function O(e){return e instanceof Buffer?Buffer.from(e):new e.constructor(e.buffer.slice(),e.byteOffset,e.length)}function ae(e){if(e=e||{},e.circles)return ue(e);let r=new Map;if(r.set(Date,l=>new Date(l)),r.set(Map,(l,c)=>new Map(s(Array.from(l),c))),r.set(Set,(l,c)=>new Set(s(Array.from(l),c))),e.constructorHandlers)for(let l of e.constructorHandlers)r.set(l[0],l[1]);let a=null;return e.proto?t:u;function s(l,c){let o=Object.keys(l),n=new Array(o.length);for(let i=0;i<o.length;i++){let f=o[i],m=l[f];typeof m!="object"||m===null?n[f]=m:m.constructor!==Object&&(a=r.get(m.constructor))?n[f]=a(m,c):ArrayBuffer.isView(m)?n[f]=O(m):n[f]=c(m)}return n}function u(l){if(typeof l!="object"||l===null)return l;if(Array.isArray(l))return s(l,u);if(l.constructor!==Object&&(a=r.get(l.constructor)))return a(l,u);let c={};for(let o in l){if(Object.hasOwnProperty.call(l,o)===!1)continue;let n=l[o];typeof n!="object"||n===null?c[o]=n:n.constructor!==Object&&(a=r.get(n.constructor))?c[o]=a(n,u):ArrayBuffer.isView(n)?c[o]=O(n):c[o]=u(n)}return c}function t(l){if(typeof l!="object"||l===null)return l;if(Array.isArray(l))return s(l,t);if(l.constructor!==Object&&(a=r.get(l.constructor)))return a(l,t);let c={};for(let o in l){let n=l[o];typeof n!="object"||n===null?c[o]=n:n.constructor!==Object&&(a=r.get(n.constructor))?c[o]=a(n,t):ArrayBuffer.isView(n)?c[o]=O(n):c[o]=t(n)}return c}}function ue(e){let r=[],a=[],s=new Map;if(s.set(Date,o=>new Date(o)),s.set(Map,(o,n)=>new Map(t(Array.from(o),n))),s.set(Set,(o,n)=>new Set(t(Array.from(o),n))),e.constructorHandlers)for(let o of e.constructorHandlers)s.set(o[0],o[1]);let u=null;return e.proto?c:l;function t(o,n){let i=Object.keys(o),f=new Array(i.length);for(let m=0;m<i.length;m++){let p=i[m],g=o[p];if(typeof g!="object"||g===null)f[p]=g;else if(g.constructor!==Object&&(u=s.get(g.constructor)))f[p]=u(g,n);else if(ArrayBuffer.isView(g))f[p]=O(g);else{let A=r.indexOf(g);A!==-1?f[p]=a[A]:f[p]=n(g)}}return f}function l(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return t(o,l);if(o.constructor!==Object&&(u=s.get(o.constructor)))return u(o,l);let n={};r.push(o),a.push(n);for(let i in o){if(Object.hasOwnProperty.call(o,i)===!1)continue;let f=o[i];if(typeof f!="object"||f===null)n[i]=f;else if(f.constructor!==Object&&(u=s.get(f.constructor)))n[i]=u(f,l);else if(ArrayBuffer.isView(f))n[i]=O(f);else{let m=r.indexOf(f);m!==-1?n[i]=a[m]:n[i]=l(f)}}return r.pop(),a.pop(),n}function c(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return t(o,c);if(o.constructor!==Object&&(u=s.get(o.constructor)))return u(o,c);let n={};r.push(o),a.push(n);for(let i in o){let f=o[i];if(typeof f!="object"||f===null)n[i]=f;else if(f.constructor!==Object&&(u=s.get(f.constructor)))n[i]=u(f,c);else if(ArrayBuffer.isView(f))n[i]=O(f);else{let m=r.indexOf(f);m!==-1?n[i]=a[m]:n[i]=c(f)}}return r.pop(),a.pop(),n}}});var ye={};se(ye,{cbSchema:()=>he,collapse:()=>de,defaults:()=>G,version:()=>me});var oe={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function T(e,r){if(!Array.isArray(e)||!e.length)return e;let a=h(h({},oe),r),s,u;if(a.strictlyTwoElementsInRangeArrays&&!e.every((c,o)=>!Array.isArray(c)||c.length!==2?(s=o,u=c.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, ".concat(s,"th range (").concat(JSON.stringify(e[s],null,4),") has not two but ").concat(u," elements!"));if(!e.every((c,o)=>!Array.isArray(c)||!Number.isInteger(c[0])||c[0]<0||!Number.isInteger(c[1])||c[1]<0?(s=o,!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, ".concat(s,"th range (").concat(JSON.stringify(e[s],null,4),") does not consist of only natural numbers!"));let t=e.length**2,l=0;return Array.from(e).sort((c,o)=>(a.progressFn&&(l+=1,a.progressFn(Math.floor(l*100/t))),c[0]===o[0]?c[1]<o[1]?-1:c[1]>o[1]?1:0:c[0]<o[0]?-1:1))}var L={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function H(e,r){function a(n){return!!n&&typeof n=="object"&&!Array.isArray(n)}if(!Array.isArray(e)||!e.length)return null;let s;if(r)if(a(r)){if(s=h(h({},L),r),s.progressFn&&a(s.progressFn)&&!Object.keys(s.progressFn).length)s.progressFn=null;else if(s.progressFn&&typeof s.progressFn!="function")throw new Error('ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "'.concat(typeof s.progressFn,'", equal to ').concat(JSON.stringify(s.progressFn,null,4)));if(![1,2,"1","2"].includes(s.mergeType))throw new Error('ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "'.concat(typeof s.mergeType,'", equal to ').concat(JSON.stringify(s.mergeType,null,4)));if(typeof s.joinRangesThatTouchEdges!="boolean")throw new Error('ranges-merge: [THROW_ID_04] opts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "'.concat(typeof s.joinRangesThatTouchEdges,'", equal to ').concat(JSON.stringify(s.joinRangesThatTouchEdges,null,4)))}else throw new Error("emlint: [THROW_ID_03] the second input argument must be a plain object. It was given as:\n".concat(JSON.stringify(r,null,4)," (type ").concat(typeof r,")"));else s=h({},L);let u=e.filter(n=>Array.isArray(n)).map(n=>[...n]).filter(n=>n[2]!==void 0||n[0]!==n[1]),t,l,c;s.progressFn?t=T(u,{progressFn:n=>{c=Math.floor(n/5),c!==l&&(l=c,s.progressFn(c))}}):t=T(u);let o=t.length-1;for(let n=o;n>0;n--)s.progressFn&&(c=Math.floor((1-n/o)*78)+21,c!==l&&c>l&&(l=c,s.progressFn(c))),(t[n][0]<=t[n-1][0]||!s.joinRangesThatTouchEdges&&t[n][0]<t[n-1][1]||s.joinRangesThatTouchEdges&&t[n][0]<=t[n-1][1])&&(t[n-1][0]=Math.min(t[n][0],t[n-1][0]),t[n-1][1]=Math.max(t[n][1],t[n-1][1]),t[n][2]!==void 0&&(t[n-1][0]>=t[n][0]||t[n-1][1]<=t[n][1])&&t[n-1][2]!==null&&(t[n][2]===null&&t[n-1][2]!==null?t[n-1][2]=null:t[n-1][2]!=null?+s.mergeType==2&&t[n-1][0]===t[n][0]?t[n-1][2]=t[n][2]:t[n-1][2]+=t[n][2]:t[n-1][2]=t[n][2]),t.splice(n,1),n=t.length);return t.length?t:null}var le=!0,j="Invariant failed";function B(e,r){if(!e){if(le)throw new Error(j);var a=typeof r=="function"?r():r,s=a?"".concat(j,": ").concat(a):j;throw new Error(s)}}function M(e,r,a){let s=0,u=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: ".concat(typeof e,", equal to: ").concat(JSON.stringify(e,null,4)));if(r&&!Array.isArray(r))throw new TypeError("ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ".concat(typeof r,", equal to: ").concat(JSON.stringify(r,null,4)));if(a&&typeof a!="function")throw new TypeError("ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ".concat(typeof a,", equal to: ").concat(JSON.stringify(a,null,4)));if(!(r!=null&&r.filter(i=>i).length))return e;let t;Array.isArray(r)&&Number.isInteger(r[0])&&Number.isInteger(r[1])?t=[Array.from(r)]:t=Array.from(r);let l=t.length,c=0;t.filter(i=>i).forEach((i,f)=>{if(a&&(s=Math.floor(c/l*10),s!==u&&(u=s,a(s))),!Array.isArray(i))throw new TypeError("ranges-apply: [THROW_ID_05] ranges array, second input arg., has ".concat(f,"th element not an array: ").concat(JSON.stringify(i,null,4),", which is ").concat(typeof i));if(!Number.isInteger(i[0])){if(!Number.isInteger(+i[0])||+i[0]<0)throw new TypeError("ranges-apply: [THROW_ID_06] ranges array, second input arg. has ".concat(f,"th element, array ").concat(JSON.stringify(i,null,0),". Its first element is not an integer, string index, but ").concat(typeof i[0],", equal to: ").concat(JSON.stringify(i[0],null,4),"."));t[f][0]=+t[f][0]}if(!Number.isInteger(i[1])){if(!Number.isInteger(+i[1])||+i[1]<0)throw new TypeError("ranges-apply: [THROW_ID_07] ranges array, second input arg. has ".concat(f,"th element, array ").concat(JSON.stringify(i,null,0),". Its second element is not an integer, string index, but ").concat(typeof i[1],", equal to: ").concat(JSON.stringify(i[1],null,4),"."));t[f][1]=+t[f][1]}c+=1});let o=H(t,{progressFn:i=>{a&&(s=10+Math.floor(i/10),s!==u&&(u=s,a(s)))}});B(o);let n=o.length;if(n>0){let i=e.slice(o[n-1][1]);e=o.reduce((f,m,p,g)=>{a&&(s=20+Math.floor(p/n*80),s!==u&&(u=s,a(s)));let A=p===0?0:g[p-1][1],d=g[p][0];return"".concat(f).concat(e.slice(A,d)).concat(g[p][2]||"")},""),e+=i}return e}function N(e,r=1){let a="\xA0";function s(t){return Array.from(t).reverse().join("")}function u(t,l,c){let o=c?"\n":"\r",n=c?"\r":"\n";if(!t)return t;let i=0,f=0,m="";for(let p=0,g=t.length;p<g;p++)(t[p]===o||t[p]===n&&t[p-1]!==o)&&f++,"\r\n".includes(t[p])||t[p]===a?(i=0,t[p]===a?m+=t[p]:t[p]===o?f<=l&&(m+=t[p],t[p+1]===n&&(m+=t[p+1],p++)):t[p]===n&&(t==null?void 0:t[p-1])!==o&&f<=l&&(m+=t[p])):(i++,!t[p+1]&&!f&&(m+=" "));return m}if(typeof e=="string"&&e.length){let t=1;typeof+r=="number"&&Number.isInteger(+r)&&+r>=0&&(t=+r);let l="",c="";if(!e.trim())l=e;else if(!e[0].trim()){for(let o=0,n=e.length;o<n;o++)if(e[o].trim()){l=e.slice(0,o);break}}if(e.trim()&&(e.slice(-1).trim()===""||e.slice(-1)===a)){for(let o=e.length;o--;)if(e[o].trim()){c=e.slice(o+1);break}}return"".concat(u(l,t,!1)).concat(e.trim()).concat(s(u(s(c),t,!0)))}return e}var P=F(V(),1);var Re=(0,P.default)();function E(e){return typeof e=="string"}function Q(e){return Number.isFinite(e)}function S(e){return Number.isSafeInteger(e)&&e>=0}function w(e){return e!=null}var U={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function ce(e,r){var n;function a(i){return!!i&&typeof i=="object"&&!Array.isArray(i)}if(!Array.isArray(e)||!e.length)return null;let s;if(r)if(a(r)){if(s=h(h({},U),r),s.progressFn&&a(s.progressFn)&&!Object.keys(s.progressFn).length)s.progressFn=null;else if(s.progressFn&&typeof s.progressFn!="function")throw new Error('ranges-merge: [THROW_ID_01] resolvedOpts.progressFn must be a function! It was given of a type: "'.concat(typeof s.progressFn,'", equal to ').concat(JSON.stringify(s.progressFn,null,4)));if(![1,2,"1","2"].includes(s.mergeType))throw new Error('ranges-merge: [THROW_ID_02] resolvedOpts.mergeType was customised to a wrong thing! It was given of a type: "'.concat(typeof s.mergeType,'", equal to ').concat(JSON.stringify(s.mergeType,null,4)));if(typeof s.joinRangesThatTouchEdges!="boolean")throw new Error('ranges-merge: [THROW_ID_04] resolvedOpts.joinRangesThatTouchEdges was customised to a wrong thing! It was given of a type: "'.concat(typeof s.joinRangesThatTouchEdges,'", equal to ').concat(JSON.stringify(s.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:\n".concat(JSON.stringify(r,null,4)," (type ").concat(typeof r,")"));else s=h({},U);let u=e.filter(i=>Array.isArray(i)).map(i=>[...i]).filter(i=>i[2]!==void 0||i[0]!==i[1]),t,l,c;s.progressFn?t=T(u,{progressFn:i=>{c=Math.floor(i/5),c!==l&&(l=c,s.progressFn!=null&&s.progressFn(c))}}):t=T(u);let o=t.length-1;for(let i=o;i>0;i--)s.progressFn&&(c=Math.floor((1-i/o)*78)+21,c!==l&&c>l&&(l=c,s.progressFn(c))),(t[i][0]<=t[i-1][0]||!s.joinRangesThatTouchEdges&&t[i][0]<t[i-1][1]||s.joinRangesThatTouchEdges&&t[i][0]<=t[i-1][1])&&(t[i-1][0]=Math.min(t[i][0],t[i-1][0]),t[i-1][1]=Math.max(t[i][1],t[i-1][1]),t[i][2]!==void 0&&(t[i-1][0]>=t[i][0]||t[i-1][1]<=t[i][1])&&t[i-1][2]!==null&&(t[i][2]===null&&t[i-1][2]!==null?t[i-1][2]=null:t[i-1][2]!=null?+((n=s||{})==null?void 0:n.mergeType)==2&&t[i-1][0]===t[i][0]?t[i-1][2]=t[i][2]:t[i-1][2]+=t[i][2]:t[i-1][2]=t[i][2]),t.splice(i,1),i=t.length);return t.length?t:null}var ge={limitToBeAddedWhitespace:!1,limitLinebreaksCount:1,mergeType:1},z=class{constructor(e){I(this,"ranges");I(this,"opts");let r=h(h({},ge),e);if(r.mergeType&&r.mergeType!==1&&r.mergeType!==2)if(E(r.mergeType)&&r.mergeType.trim()==="1")r.mergeType=1;else if(E(r.mergeType)&&r.mergeType.trim()==="2")r.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: "'.concat(typeof r.mergeType,'", equal to ').concat(JSON.stringify(r.mergeType,null,4)));this.opts=r,this.ranges=[]}add(e,r,a){var t;if(e==null&&r==null)return;if(w(e)&&!w(r)){if(Array.isArray(e)){if(e.length){if(e.some(l=>Array.isArray(l))){e.forEach(l=>{Array.isArray(l)&&this.add(...l)});return}e.length&&S(+e[0])&&S(+e[1])&&this.add(...e)}return}throw new TypeError('ranges-push/Ranges/add(): [THROW_ID_12] the first input argument, "from" is set ('.concat(JSON.stringify(e,null,0),') but second-one, "to" is not (').concat(JSON.stringify(r,null,0),")"))}else if(!w(e)&&w(r))throw new TypeError('ranges-push/Ranges/add(): [THROW_ID_13] the second input argument, "to" is set ('.concat(JSON.stringify(r,null,0),') but first-one, "from" is not (').concat(JSON.stringify(e,null,0),")"));let s=+e,u=+r;if(S(s)&&S(u)){if(w(a)&&!E(a)&&!Q(a))throw new TypeError("ranges-push/Ranges/add(): [THROW_ID_08] The third argument, the value to add, was given not as string but ".concat(typeof a,", equal to:\n").concat(JSON.stringify(a,null,4)));if(w(this.ranges)&&Array.isArray(this.last())&&s===this.last()[1]){if(this.last()[1]=u,this.last()[2],this.last()[2]!==null&&w(a)){let l=this.last()[2]&&this.last()[2].length&&(!((t=this.opts)!=null&&t.mergeType)||this.opts.mergeType===1)?"".concat(this.last()[2]).concat(a):a;this.opts.limitToBeAddedWhitespace&&(l=N(l,this.opts.limitLinebreaksCount)),E(l)&&!l.length||(this.last()[2]=l)}}else{this.ranges||(this.ranges=[]);let l=a!==void 0&&!(E(a)&&!a.length)?[s,u,a&&this.opts.limitToBeAddedWhitespace?N(a,this.opts.limitLinebreaksCount):a]:[s,u];this.ranges.push(l)}}else throw S(s)&&s>=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 "'.concat(typeof u,'" equal to: ').concat(JSON.stringify(u,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 "'.concat(typeof s,'" equal to: ').concat(JSON.stringify(s,null,4)))}push(e,r,a){this.add(e,r,a)}current(){return Array.isArray(this.ranges)&&this.ranges.length?(this.ranges=ce(this.ranges,{mergeType:this.opts.mergeType}),this.ranges&&this.opts.limitToBeAddedWhitespace?this.ranges.map(e=>w(e[2])?[e[0],e[1],N(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])&&S(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, ".concat(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 K=F(V(),1);var Le=(0,K.default)();var k="\xA0";function fe({str:e,idx:r=0,stopAtNewlines:a=!1,stopAtRawNbsp:s=!1}){if(typeof e!="string"||!e.length||((!r||typeof r!="number")&&(r=0),!e[r+1]))return null;if(e[r+1]&&(e[r+1].trim()||a&&"\n\r".includes(e[r+1])||s&&e[r+1]===k))return r+1;if(e[r+2]&&(e[r+2].trim()||a&&"\n\r".includes(e[r+2])||s&&e[r+2]===k))return r+2;for(let u=r+1,t=e.length;u<t;u++)if(e[u].trim()||a&&"\n\r".includes(e[u])||s&&e[u]===k)return u;return null}function _(e,r=0){return fe({str:e,idx:r,stopAtNewlines:!1,stopAtRawNbsp:!1})}var Z="11.1.0";var me=Z,G={trimStart:!0,trimEnd:!0,trimLines:!1,trimnbsp:!1,removeEmptyLines:!1,limitConsecutiveEmptyLinesTo:0,enforceSpacesOnly:!1,cb:({suggested:e})=>e},he=["suggested","whiteSpaceStartsAt","whiteSpaceEndsAt","str"];function de(e,r){if(typeof e!="string")throw new Error("string-collapse-white-space/collapse(): [THROW_ID_01] The input is not string but ".concat(typeof e,", equal to: ").concat(JSON.stringify(e,null,4)));if(r&&typeof r!="object")throw new Error("string-collapse-white-space/collapse(): [THROW_ID_02] The resolvedOpts is not a plain object but ".concat(typeof r,", equal to:\n").concat(JSON.stringify(r,null,4)));if(!e.length)return{result:"",ranges:null};let a=new z,s="\xA0",u=h(h({},G),r);function t(g,A){if(typeof u.cb=="function"){let d=u.cb(h({suggested:g},A));Array.isArray(d)&&a.push(...d)}else g&&a.push(...g)}let l=null,c=null,o=null,n=null,i=null,f=!1,m=[],p=0;for(let g=0,A=e.length;g<=A;g++){if((e[g]==="\r"||e[g]==="\n"&&e[g-1]!=="\r")&&(p+=1,n===null&&(n=g),i=e[g]==="\r"&&e[g+1]==="\n"?g+2:g+1),!u.trimnbsp&&e[g]===s&&!f&&(f=!0),l!==null&&e[g]!==" "){let d=l&&c||!c&&(!u.trimStart||!u.trimnbsp&&(e[g]===s||e[l-1]===s)),b=e[g]||!u.trimEnd||!u.trimnbsp&&(e[g]===s||e[l-1]===s),y=!u.enforceSpacesOnly||(!e[l-1]||e[l-1].trim())&&(!e[g]||e[g].trim());if(l<g-1&&d&&b&&y){let $=l,v=g,D=" ";u.trimLines&&(!l||!e[g]||e[l-1]&&"\r\n".includes(e[l-1])||e[g]&&"\r\n".includes(e[g]))&&(D=null),D&&e[l]===" "&&(v-=1,D=null),(!l&&u.trimStart||!e[g]&&u.trimEnd)&&(v=g),m.push([D?[$,v,D]:[$,v],{whiteSpaceStartsAt:c,whiteSpaceEndsAt:_(e,g-1)||g,str:e}])}}if(l===null&&e[g]===" "&&(l=g),c===null&&e[g]&&!e[g].trim()&&(c=g),o!==null&&("\n\r".includes(e[g])||!e[g]||e[g].trim()||!(u.trimnbsp||u.enforceSpacesOnly)&&e[g]===s)&&(o||!u.trimStart||u.enforceSpacesOnly&&f)&&(e[g]||!u.trimEnd||u.enforceSpacesOnly&&f)){if(u.enforceSpacesOnly&&(g>o+1||e[o]!==" ")){let d=o,b=g,y=" ";e[b-1]===" "?(b-=1,y=null):e[o]===" "&&(d+=1,y=null),((u.trimStart||u.trimLines)&&!o||(u.trimEnd||u.trimLines)&&!e[g])&&(y=null),t(y?[d,b,y]:[d,b],{whiteSpaceStartsAt:c,whiteSpaceEndsAt:g,str:e})}u.trimLines&&(!o||"\r\n".includes(e[o-1])||!e[g]||"\r\n".includes(e[g]))&&(u.trimnbsp||!f)&&t([o,g],{whiteSpaceStartsAt:c,whiteSpaceEndsAt:_(e,g-1)||g,str:e}),o=null}if(o===null&&!"\r\n".includes(e[g])&&e[g]&&!e[g].trim()&&(u.trimnbsp||e[g]!==s||u.enforceSpacesOnly)&&(o=g),c!==null&&(!e[g]||e[g].trim())){if((!c&&(u.trimStart||u.trimLines&&n===null)||!e[g]&&(u.trimEnd||u.trimLines&&n===null))&&(u.trimnbsp||!f||u.enforceSpacesOnly))t([c,g],{whiteSpaceStartsAt:c,whiteSpaceEndsAt:g,str:e});else{let d=!1;if(u.removeEmptyLines&&n!==null&&p>(u.limitConsecutiveEmptyLinesTo||0)+1){d=!0;let b=n,y=i||e.length,$="".concat(e[n]==="\r"&&e[n+1]==="\n"?"\r\n":e[n]).repeat((u.limitConsecutiveEmptyLinesTo||0)+1);e.endsWith($,i)?(y-=$.length||0,$=null):e.startsWith($,n)&&(b+=$.length,$=null),t($?[b,y,$]:[b,y],{whiteSpaceStartsAt:c,whiteSpaceEndsAt:g,str:e})}if(m.length){for(;m.length;)t(...m.shift());d=!0}d||t(null,{whiteSpaceStartsAt:c,whiteSpaceEndsAt:g,str:e})}c=null,o=null,f=!1,p&&(p=0,n=null,i=null)}l!==null&&e[g]!==" "&&(l=null)}return{result:M(e,a.current()),ranges:a.current()}}return ie(ye);})(); /** * @name ranges-sort * @fileoverview Sort string index ranges * @version 6.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/ranges-sort/} */ /** * @name ranges-merge * @fileoverview Merge and sort string index ranges * @version 9.1.0 * @author Roy Revelt * @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.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/ranges-apply/} */ /** * @name string-collapse-leading-whitespace * @fileoverview Collapse the leading and trailing whitespace of a string * @version 7.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-collapse-leading-whitespace/} */ /** * @name codsen-utils * @fileoverview Various utility functions * @version 1.7.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/codsen-utils/} */ /** * @name ranges-push * @fileoverview Gather string index ranges * @version 7.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/ranges-push/} */ /** * @name string-left-right * @fileoverview Looks up the first non-whitespace character to the left/right of a given index * @version 6.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-left-right/} */