/** * @name ranges-merge * @fileoverview Merge and sort string index ranges * @version 7.1.0 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/ranges-merge/} */ 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray'); var _objectSpread = require('@babel/runtime/helpers/objectSpread2'); var _typeof = require('@babel/runtime/helpers/typeof'); var rangesSort = require('ranges-sort'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray); var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread); var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof); var version$1 = "7.1.0"; var version = version$1; var defaults = { mergeType: 1, progressFn: null, joinRangesThatTouchEdges: true }; function rMerge(arrOfRanges, originalOpts) { function isObj(something) { return something && _typeof__default['default'](something) === "object" && !Array.isArray(something); } if (!Array.isArray(arrOfRanges) || !arrOfRanges.length) { return null; } var opts; if (originalOpts) { if (isObj(originalOpts)) { opts = _objectSpread__default['default'](_objectSpread__default['default']({}, defaults), originalOpts); if (opts.progressFn && isObj(opts.progressFn) && !Object.keys(opts.progressFn).length) { opts.progressFn = null; } else if (opts.progressFn && typeof opts.progressFn !== "function") { throw new Error("ranges-merge: [THROW_ID_01] opts.progressFn must be a function! It was given of a type: \"".concat(_typeof__default['default'](opts.progressFn), "\", equal to ").concat(JSON.stringify(opts.progressFn, null, 4))); } if (opts.mergeType && +opts.mergeType !== 1 && +opts.mergeType !== 2) { throw new Error("ranges-merge: [THROW_ID_02] opts.mergeType was customised to a wrong thing! It was given of a type: \"".concat(_typeof__default['default'](opts.mergeType), "\", equal to ").concat(JSON.stringify(opts.mergeType, null, 4))); } if (typeof opts.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__default['default'](opts.joinRangesThatTouchEdges), "\", equal to ").concat(JSON.stringify(opts.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(originalOpts, null, 4), " (type ").concat(_typeof__default['default'](originalOpts), ")")); } } else { opts = _objectSpread__default['default']({}, defaults); } var filtered = arrOfRanges .filter(function (range) { return range; }).map(function (subarr) { return _toConsumableArray__default['default'](subarr); }).filter( function (rangeArr) { return rangeArr[2] !== undefined || rangeArr[0] !== rangeArr[1]; }); var sortedRanges; var lastPercentageDone; var percentageDone; if (opts.progressFn) { sortedRanges = rangesSort.rSort(filtered, { progressFn: function progressFn(percentage) { percentageDone = Math.floor(percentage / 5); if (percentageDone !== lastPercentageDone) { lastPercentageDone = percentageDone; opts.progressFn(percentageDone); } } }); } else { sortedRanges = rangesSort.rSort(filtered); } if (!sortedRanges) { return null; } var len = sortedRanges.length - 1; for (var i = len; i > 0; i--) { if (opts.progressFn) { percentageDone = Math.floor((1 - i / len) * 78) + 21; if (percentageDone !== lastPercentageDone && percentageDone > lastPercentageDone) { lastPercentageDone = percentageDone; opts.progressFn(percentageDone); } } if (sortedRanges[i][0] <= sortedRanges[i - 1][0] || !opts.joinRangesThatTouchEdges && sortedRanges[i][0] < sortedRanges[i - 1][1] || opts.joinRangesThatTouchEdges && sortedRanges[i][0] <= sortedRanges[i - 1][1]) { sortedRanges[i - 1][0] = Math.min(sortedRanges[i][0], sortedRanges[i - 1][0]); sortedRanges[i - 1][1] = Math.max(sortedRanges[i][1], sortedRanges[i - 1][1]); if (sortedRanges[i][2] !== undefined && (sortedRanges[i - 1][0] >= sortedRanges[i][0] || sortedRanges[i - 1][1] <= sortedRanges[i][1])) { if (sortedRanges[i - 1][2] !== null) { if (sortedRanges[i][2] === null && sortedRanges[i - 1][2] !== null) { sortedRanges[i - 1][2] = null; } else if (sortedRanges[i - 1][2] != null) { if (+opts.mergeType === 2 && sortedRanges[i - 1][0] === sortedRanges[i][0]) { sortedRanges[i - 1][2] = sortedRanges[i][2]; } else { sortedRanges[i - 1][2] += sortedRanges[i][2]; } } else { sortedRanges[i - 1][2] = sortedRanges[i][2]; } } } sortedRanges.splice(i, 1); i = sortedRanges.length; } } return sortedRanges.length ? sortedRanges : null; } exports.defaults = defaults; exports.rMerge = rMerge; exports.version = version;