JSPM

  • Created
  • Published
  • Downloads 244240
  • Score
    100M100P100Q163626F
  • License MIT

Merge and sort arrays which mean string slice ranges

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

Repository is on BitBucket Coverage View dependencies as 2D chart Downloads/Month Test in browser Code style: prettier MIT License

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 2 KB
UMD build for browsers, transpiled, minified, containing iife's and has all dependencies baked-in browser dist/ranges-merge.umd.js 10 KB

⬆ back to top

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]
// ]
}

⬆ back to top

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 want a new feature in this package or you would like us to change some of its functionality, raise an issue on this repo.

  • If you tried to use this library but it misbehaves, or you need advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo.

  • If you would like to add or change some features, just fork it, hack away, and file a pull request. We'll do our best to merge it quickly. Prettier is enabled, so you don't need to worry about the code style.

⬆ back to top

Licence

MIT License (MIT)

Copyright © 2018 Codsen Ltd, Roy Revelt