ranges-crop
Version:
Crop array of ranges when they go beyond the reference string's length
28 lines (26 loc) • 6.09 kB
JavaScript
/**
* @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/}
*/
;var rangesCrop=(()=>{var p=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames,c=Object.getOwnPropertySymbols;var h=Object.prototype.hasOwnProperty,$=Object.prototype.propertyIsEnumerable;var m=(r,t,i)=>t in r?p(r,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):r[t]=i,g=(r,t)=>{for(var i in t||(t={}))h.call(t,i)&&m(r,i,t[i]);if(c)for(var i of c(t))$.call(t,i)&&m(r,i,t[i]);return r};var E=(r,t)=>{for(var i in t)p(r,i,{get:t[i],enumerable:!0})},v=(r,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of w(t))!h.call(r,o)&&o!==i&&p(r,o,{get:()=>t[o],enumerable:!(s=b(t,o))||s.enumerable});return r};var x=r=>v(p({},"__esModule",{value:!0}),r);var _={};E(_,{rCrop:()=>N,version:()=>R});var A={strictlyTwoElementsInRangeArrays:!1,progressFn:null};function y(r,t){if(!Array.isArray(r)||!r.length)return r;let i=g(g({},A),t),s,o;if(i.strictlyTwoElementsInRangeArrays&&!r.every((l,a)=>!Array.isArray(l)||l.length!==2?(s=a,o=l.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, ${s}th range (${JSON.stringify(r[s],null,4)}) has not two but ${o} elements!`);if(!r.every((l,a)=>!Array.isArray(l)||!Number.isInteger(l[0])||l[0]<0||!Number.isInteger(l[1])||l[1]<0?(s=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, ${s}th range (${JSON.stringify(r[s],null,4)}) does not consist of only natural numbers!`);let n=r.length**2,u=0;return Array.from(r).sort((l,a)=>(i.progressFn&&(u+=1,i.progressFn(Math.floor(u*100/n))),l[0]===a[0]?l[1]<a[1]?-1:l[1]>a[1]?1:0:l[0]<a[0]?-1:1))}var f={mergeType:1,progressFn:null,joinRangesThatTouchEdges:!0};function d(r,t){function i(e){return!!e&&typeof e=="object"&&!Array.isArray(e)}if(!Array.isArray(r)||!r.length)return null;let s;if(t)if(i(t)){if(s=g(g({},f),t),s.progressFn&&i(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: "${typeof s.progressFn}", equal to ${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: "${typeof s.mergeType}", equal to ${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: "${typeof s.joinRangesThatTouchEdges}", equal to ${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:
${JSON.stringify(t,null,4)} (type ${typeof t})`);else s=g({},f);let o=r.filter(e=>Array.isArray(e)).map(e=>[...e]).filter(e=>e[2]!==void 0||e[0]!==e[1]),n,u,l;s.progressFn?n=y(o,{progressFn:e=>{l=Math.floor(e/5),l!==u&&(u=l,s.progressFn(l))}}):n=y(o);let a=n.length-1;for(let e=a;e>0;e--)s.progressFn&&(l=Math.floor((1-e/a)*78)+21,l!==u&&l>u&&(u=l,s.progressFn(l))),(n[e][0]<=n[e-1][0]||!s.joinRangesThatTouchEdges&&n[e][0]<n[e-1][1]||s.joinRangesThatTouchEdges&&n[e][0]<=n[e-1][1])&&(n[e-1][0]=Math.min(n[e][0],n[e-1][0]),n[e-1][1]=Math.max(n[e][1],n[e-1][1]),n[e][2]!==void 0&&(n[e-1][0]>=n[e][0]||n[e-1][1]<=n[e][1])&&n[e-1][2]!==null&&(n[e][2]===null&&n[e-1][2]!==null?n[e-1][2]=null:n[e-1][2]!=null?+s.mergeType==2&&n[e-1][0]===n[e][0]?n[e-1][2]=n[e][2]:n[e-1][2]+=n[e][2]:n[e-1][2]=n[e][2]),n.splice(e,1),e=n.length);return n.length?n:null}var T="6.0.19";var R=T;function N(r,t){if(r===null)return null;if(!Array.isArray(r))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 r}, equal to: ${JSON.stringify(r,null,4)}`);if(!Number.isInteger(t))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 t}, equal to: ${JSON.stringify(t,null,4)}`);if(!r.filter(o=>o).length)return r.filter(o=>o);let i=0;if(!r.filter(o=>o).every((o,n)=>!Number.isInteger(o[0])||!Number.isInteger(o[1])?(i=n,!1):!0))throw Array.isArray(r)&&typeof r[0]=="number"&&typeof r[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(r,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 ${i}th range (${JSON.stringify(r[i],null,0)}) does not consist of only natural numbers!`);if(!r.filter(o=>o).every((o,n)=>o[2]!=null&&typeof o[2]!="string"?(i=n,!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 ${i}th range ${JSON.stringify(r[i],null,0)} has a argument in the range of a type ${typeof r[i][2]}`);let s=(d(r)||[]).filter(o=>o[0]<=t&&(o[2]!=null||o[0]<t)).map(o=>o[1]>t?o[2]!=null?[o[0],t,o[2]]:[o[0],t]:o);return s.length?s:null}return x(_);})();
/**
* @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/}
*/