UNPKG

ranges-crop

Version:

Crop array of ranges when they go beyond the reference string's length

11 lines (9 loc) 1.9 kB
/** * @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/} */ import{rMerge as u}from"ranges-merge";var i="6.0.19";var g=i;function m(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(e=>e).length)return r.filter(e=>e);let n=0;if(!r.filter(e=>e).every((e,s)=>!Number.isInteger(e[0])||!Number.isInteger(e[1])?(n=s,!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 ${n}th range (${JSON.stringify(r[n],null,0)}) does not consist of only natural numbers!`);if(!r.filter(e=>e).every((e,s)=>e[2]!=null&&typeof e[2]!="string"?(n=s,!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 ${n}th range ${JSON.stringify(r[n],null,0)} has a argument in the range of a type ${typeof r[n][2]}`);let o=(u(r)||[]).filter(e=>e[0]<=t&&(e[2]!=null||e[0]<t)).map(e=>e[1]>t?e[2]!=null?[e[0],t,e[2]]:[e[0],t]:e);return o.length?o:null}export{m as rCrop,g as version};