/** * @name ranges-apply * @fileoverview Take an array of string index ranges, delete/replace the string according to them * @version 5.1.0 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/ranges-apply/} */ import { rMerge } from 'ranges-merge'; var version$1 = "5.1.0"; const version = version$1; function rApply(str, originalRangesArr, progressFn) { let percentageDone = 0; let lastPercentageDone = 0; if (arguments.length === 0) { throw new Error("ranges-apply: [THROW_ID_01] inputs missing!"); } if (typeof str !== "string") { throw new TypeError(`ranges-apply: [THROW_ID_02] first input argument must be a string! Currently it's: ${typeof str}, equal to: ${JSON.stringify(str, null, 4)}`); } if (originalRangesArr && !Array.isArray(originalRangesArr)) { throw new TypeError(`ranges-apply: [THROW_ID_03] second input argument must be an array (or null)! Currently it's: ${typeof originalRangesArr}, equal to: ${JSON.stringify(originalRangesArr, null, 4)}`); } if (progressFn && typeof progressFn !== "function") { throw new TypeError(`ranges-apply: [THROW_ID_04] the third input argument must be a function (or falsey)! Currently it's: ${typeof progressFn}, equal to: ${JSON.stringify(progressFn, null, 4)}`); } if (!originalRangesArr || !originalRangesArr.filter(range => range).length) { return str; } let rangesArr; if (Array.isArray(originalRangesArr) && Number.isInteger(originalRangesArr[0]) && Number.isInteger(originalRangesArr[1])) { rangesArr = [Array.from(originalRangesArr)]; } else { rangesArr = Array.from(originalRangesArr); } const len = rangesArr.length; let counter = 0; rangesArr.filter(range => range).forEach((el, i) => { if (progressFn) { percentageDone = Math.floor(counter / len * 10); /* istanbul ignore else */ if (percentageDone !== lastPercentageDone) { lastPercentageDone = percentageDone; progressFn(percentageDone); } } if (!Array.isArray(el)) { throw new TypeError(`ranges-apply: [THROW_ID_05] ranges array, second input arg., has ${i}th element not an array: ${JSON.stringify(el, null, 4)}, which is ${typeof el}`); } if (!Number.isInteger(el[0])) { if (!Number.isInteger(+el[0]) || +el[0] < 0) { throw new TypeError(`ranges-apply: [THROW_ID_06] ranges array, second input arg. has ${i}th element, array ${JSON.stringify(el, null, 0)}. Its first element is not an integer, string index, but ${typeof el[0]}, equal to: ${JSON.stringify(el[0], null, 4)}.`); } else { rangesArr[i][0] = +rangesArr[i][0]; } } if (!Number.isInteger(el[1])) { if (!Number.isInteger(+el[1]) || +el[1] < 0) { throw new TypeError(`ranges-apply: [THROW_ID_07] ranges array, second input arg. has ${i}th element, array ${JSON.stringify(el, null, 0)}. Its second element is not an integer, string index, but ${typeof el[1]}, equal to: ${JSON.stringify(el[1], null, 4)}.`); } else { rangesArr[i][1] = +rangesArr[i][1]; } } counter += 1; }); const workingRanges = rMerge(rangesArr, { progressFn: perc => { if (progressFn) { percentageDone = 10 + Math.floor(perc / 10); /* istanbul ignore else */ if (percentageDone !== lastPercentageDone) { lastPercentageDone = percentageDone; progressFn(percentageDone); } } } }); const len2 = Array.isArray(workingRanges) ? workingRanges.length : 0; /* istanbul ignore else */ if (len2 > 0) { const tails = str.slice(workingRanges[len2 - 1][1]); str = workingRanges.reduce((acc, _val, i, arr) => { if (progressFn) { percentageDone = 20 + Math.floor(i / len2 * 80); /* istanbul ignore else */ if (percentageDone !== lastPercentageDone) { lastPercentageDone = percentageDone; progressFn(percentageDone); } } const beginning = i === 0 ? 0 : arr[i - 1][1]; const ending = arr[i][0]; return acc + str.slice(beginning, ending) + (arr[i][2] || ""); }, ""); str += tails; } return str; } export { rApply, version };