ranges-invert
Version:
Invert string index ranges
37 lines (35 loc) • 8.47 kB
JavaScript
/**
* @name ranges-invert
* @fileoverview Invert string index ranges
* @version 6.0.19
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-invert/}
*/
;var rangesInvert=(()=>{var m=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames,b=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable;var T=(e,n,l)=>n in e?m(e,n,{enumerable:!0,configurable:!0,writable:!0,value:l}):e[n]=l,g=(e,n)=>{for(var l in n||(n={}))w.call(n,l)&&T(e,l,n[l]);if(b)for(var l of b(n))N.call(n,l)&&T(e,l,n[l]);return e};var O=(e,n)=>{for(var l in n)m(e,l,{get:n[l],enumerable:!0})},_=(e,n,l,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let s of A(n))!w.call(e,s)&&s!==l&&m(e,s,{get:()=>n[s],enumerable:!(o=R(n,s))||o.enumerable});return e};var S=e=>_(m({},"__esModule",{value:!0}),e);var k={};O(k,{defaults:()=>I,rInvert:()=>H,version:()=>F});var x={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function d(e,n){if(!Array.isArray(e)||!e.length)return e;let l=g(g({},x),n),o,s;if(l.strictlyTwoElementsInRangeArrays&&!e.every((a,i)=>!Array.isArray(a)||a.length!==2?(o=i,s=a.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, ${o}th range (${JSON.stringify(e[o],null,4)}) has not two but ${s} elements!`);if(!e.every((a,i)=>!Array.isArray(a)||!Number.isInteger(a[0])||a[0]<0||!Number.isInteger(a[1])||a[1]<0?(o=i,!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, ${o}th range (${JSON.stringify(e[o],null,4)}) does not consist of only natural numbers!`);let t=e.length**2,u=0;return Array.from(e).sort((a,i)=>(l.progressFn&&(u+=1,l.progressFn(Math.floor(u*100/t))),a[0]===i[0]?a[1]<i[1]?-1:a[1]>i[1]?1:0:a[0]<i[0]?-1:1))}var $={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function c(e,n){function l(r){return!!r&&typeof r=="object"&&!Array.isArray(r)}if(!Array.isArray(e)||!e.length)return null;let o;if(n)if(l(n)){if(o=g(g({},$),n),o.progressFn&&l(o.progressFn)&&!Object.keys(o.progressFn).length)o.progressFn=null;else if(o.progressFn&&typeof o.progressFn!="function")throw new Error(`ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: "${typeof o.progressFn}", equal to ${JSON.stringify(o.progressFn,null,4)}`);if(![1,2,"1","2"].includes(o.mergeType))throw new Error(`ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: "${typeof o.mergeType}", equal to ${JSON.stringify(o.mergeType,null,4)}`);if(typeof o.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 o.joinRangesThatTouchEdges}", equal to ${JSON.stringify(o.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(n,null,4)} (type ${typeof n})`);else o=g({},$);let s=e.filter(r=>Array.isArray(r)).map(r=>[...r]).filter(r=>r[2]!==void 0||r[0]!==r[1]),t,u,a;o.progressFn?t=d(s,{progressFn:r=>{a=Math.floor(r/5),a!==u&&(u=a,o.progressFn(a))}}):t=d(s);let i=t.length-1;for(let r=i;r>0;r--)o.progressFn&&(a=Math.floor((1-r/i)*78)+21,a!==u&&a>u&&(u=a,o.progressFn(a))),(t[r][0]<=t[r-1][0]||!o.joinRangesThatTouchEdges&&t[r][0]<t[r-1][1]||o.joinRangesThatTouchEdges&&t[r][0]<=t[r-1][1])&&(t[r-1][0]=Math.min(t[r][0],t[r-1][0]),t[r-1][1]=Math.max(t[r][1],t[r-1][1]),t[r][2]!==void 0&&(t[r-1][0]>=t[r][0]||t[r-1][1]<=t[r][1])&&t[r-1][2]!==null&&(t[r][2]===null&&t[r-1][2]!==null?t[r-1][2]=null:t[r-1][2]!=null?+o.mergeType==2&&t[r-1][0]===t[r][0]?t[r-1][2]=t[r][2]:t[r-1][2]+=t[r][2]:t[r-1][2]=t[r][2]),t.splice(r,1),r=t.length);return t.length?t:null}function v(e,n){if(e===null)return null;if(!Array.isArray(e))throw new TypeError(`ranges-crop: [THROW_ID_01] The first input's argument must be an array, consisting of range arrays! Currently its type is: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(!Number.isInteger(n))throw new TypeError(`ranges-crop: [THROW_ID_02] The second input's argument must be a natural number or zero (coming from String.length)! Currently its type is: ${typeof n}, equal to: ${JSON.stringify(n,null,4)}`);if(!e.filter(s=>s).length)return e.filter(s=>s);let l=0;if(!e.filter(s=>s).every((s,t)=>!Number.isInteger(s[0])||!Number.isInteger(s[1])?(l=t,!1):!0))throw Array.isArray(e)&&typeof e[0]=="number"&&typeof e[1]=="number"?new TypeError(`ranges-crop: [THROW_ID_03] The first argument should be AN ARRAY OF RANGES, not a single range! Currently arrOfRanges = ${JSON.stringify(e,null,0)}!`):new TypeError(`ranges-crop: [THROW_ID_04] The first argument should be AN ARRAY OF ARRAYS! Each sub-array means string slice indexes. In our case, here ${l}th range (${JSON.stringify(e[l],null,0)}) does not consist of only natural numbers!`);if(!e.filter(s=>s).every((s,t)=>s[2]!=null&&typeof s[2]!="string"?(l=t,!1):!0))throw new TypeError(`ranges-crop: [THROW_ID_05] The third argument, if present at all, should be of a string-type or null. Currently the ${l}th range ${JSON.stringify(e[l],null,0)} has a argument in the range of a type ${typeof e[l][2]}`);let o=(c(e)||[]).filter(s=>s[0]<=n&&(s[2]!=null||s[0]<n)).map(s=>s[1]>n?s[2]!=null?[s[0],n,s[2]]:[s[0],n]:s);return o.length?o:null}var E="6.0.19";var F=E,I={strictlyTwoElementsInRangeArrays:!1,skipChecks:!1};function H(e,n,l){if(!Array.isArray(e)&&e!==null)throw new TypeError(`ranges-invert: [THROW_ID_01] Input's first argument must be an array, consisting of range arrays! Currently its type is: ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);if(!Number.isInteger(n)||n<0)throw new TypeError(`ranges-invert: [THROW_ID_02] Input's second argument must be a natural number or zero (coming from String.length)! Currently its type is: ${typeof n}, equal to: ${JSON.stringify(n,null,4)}`);if(Array.isArray(e)&&typeof e[0]=="number"&&typeof e[1]=="number")throw new TypeError(`ranges-invert: [THROW_ID_07] The first argument should be AN ARRAY OF RANGES, not a single range! Currently arrOfRanges = ${JSON.stringify(e,null,0)}!`);if(!Array.isArray(e)||!e.filter(i=>Array.isArray(i)&&i[0]!==i[1]).length||!n)return n?[[0,n]]:null;let o=g(g({},I),l),s=0,t;if(!o.skipChecks&&o.strictlyTwoElementsInRangeArrays&&!e.filter(i=>i).every((i,r)=>i.length!==2?(s=r,t=i.length,!1):!0))throw new TypeError(`ranges-invert: [THROW_ID_04] Because opts.strictlyTwoElementsInRangeArrays was enabled, all ranges must be strictly two-element-long. However, the ${s}th range (${JSON.stringify(e[s],null,0)}) has not two but ${t} elements!`);if(!o.skipChecks&&!e.every((i,r)=>!Number.isInteger(i[0])||i[0]<0||!Number.isInteger(i[1])||i[1]<0?(s=r,!1):!0))throw new TypeError(`ranges-invert: [THROW_ID_05] The first argument should be AN ARRAY OF ARRAYS! Each sub-array means string slice indexes. In our case, here ${s+1}th range (${JSON.stringify(e[s],null,0)}) does not consist of only natural numbers!`);let u;o.skipChecks?u=e.filter(i=>i[0]!==i[1]):u=c(e.filter(i=>i[0]!==i[1]));let a=u.reduce((i,r,f,p)=>{let h=[];f===0&&p[0][0]!==0&&h.push([0,p[0][0]]);let y=f<p.length-1?p[f+1][0]:n;if(r[1]!==y){if(o.skipChecks&&r[1]>y)throw new TypeError(`ranges-invert: [THROW_ID_08] The checking (opts.skipChecks) is off and input ranges were not sorted! We nearly wrote range [${r[1]}, ${y}] which is backwards. For investigation, whole ranges array is:
${JSON.stringify(p,null,0)}`);h.push([r[1],y])}return i.concat(h)},[]);return v(a,n)}return S(k);})();
/**
* @name ranges-sort
* @fileoverview Sort string index ranges
* @version 6.0.13
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-sort/}
*/
/**
* @name ranges-merge
* @fileoverview Merge and sort string index ranges
* @version 9.0.18
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-merge/}
*/
/**
* @name ranges-crop
* @fileoverview Crop array of ranges when they go beyond the reference string's length
* @version 6.0.19
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/ranges-crop/}
*/