Package Exports
- ranges-merge
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ranges-merge) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ranges-merge
Merge and sort arrays which mean string slice ranges
Table of Contents
Install
npm i ranges-merge
// consume as a CommonJS require:
const mergeRanges = require("ranges-merge");
// or as native ES Module:
import mergeRanges from "ranges-merge";
Here's what you'll get:
Type | Key in package.json |
Path | Size |
---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports |
main |
dist/ranges-merge.cjs.js |
3 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . |
module |
dist/ranges-merge.esm.js |
3 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in |
browser |
dist/ranges-merge.umd.js |
29 KB |
The Idea
If, after sorting, two ranges in the vicinity have the same edge value (like 2
below), merge those ranges:
const rangesMerge = require('ranges-merge')
rangesMerge([
[1, 2], [2, 3], [9, 10]
])
// => [
// [1, 3], [9, 10]
// ]
}
If ranges overlap, merge them too:
const rangesMerge = require('ranges-merge')
rangesMerge([
[1, 5], [2, 10]
])
// => [
// [1, 10]
// ]
}
API
rangesMerge(arrOfRanges[, progressFn]) — in other words, this library gives you a function and you must feed an array into its first argument and also if you wish, you can feed a second argument, a function (bracket in [, progressFn]
means "optional").
It returns a new array of zero or more arrays, with ranges merged (where applicable). Original input is not mutated.
Input argument | Type | Obligatory? | Description |
---|---|---|---|
arrOfRanges |
Array | yes | Array of zero or more arrays meaning natural number ranges (2 elements each) |
progressFn |
Function | no | If you provide a function, it will be fed a natural number many times, for each percentage (mostly) of the work done. It's handy in worker scenarios. |
progressFn
- the 2nd input argument
Consider this example (notice an arrow function in the second input argument):
console.log(
mergeRanges([[1, 5], [11, 15], [6, 10], [16, 20], [10, 30]], perc => {
console.log(`done: ${perc}`);
})
);
//
// done: 0
// done: 1
// done: 2
// done: 3
// done: 4
// done: 4
// done: 5
// done: 21
// done: 40
// done: 60
// done: 79
// done: 99
// [[1, 5], [6, 30]]
Imagine, instead of console.log
, this function could sit in a worker and report its progress, then, finally, ping the last value - result.
Whatever function you give in second argument, it will be called with percentage done so far given as the first argument. Grab that argument and do whatever you want with it in your function.
Contributing
- If you see an error, raise an issue.
- If you want a new feature but can't code it up yourself, also raise an issue. Let's discuss it.
- If you tried to use this package, but something didn't work out, also raise an issue. We'll try to help.
- If you want to contribute some code, fork the monorepo via BitBucket, then write code, then file a pull request via BitBucket. We'll merge it in and release.
In monorepo, npm libraries are located in packages/
folder. Inside, the source code is located either in src/
folder (normal npm library) or in the root, cli.js
(if it's a command line application).
The npm script "dev
", the "dev": "rollup -c --dev --silent"
builds the development version retaining all console.log
s with row numbers. It's handy to have js-row-num-cli installed globally so you can automatically update the row numbers on all console.log
s.
Licence
MIT License
Copyright (c) 2015-2019 Roy Revelt and other contributors