1.0.5 • Published 6 years ago

repetition-ranges v1.0.5

Weekly downloads
5
License
ISC
Repository
github
Last release
6 years ago

repetition-ranges

npm version Build Status Coverage Status

Get start/end positions where a given value consecutively appears in the array

import repetitionRanges from 'repetition-ranges';

repetitionRanges(['a', 'b', 'a', 'a', 'b', 'a', 'a', 'a'], 'a');
//=> [{start: 2, end: 3}, {start: 5, end: 7}]
index01234567
arrayabaabaaa
(a appeared)
a consecutively appeared

Installation

Use npm.

npm install repetition-ranges

API

import repetitionRanges from 'repetition-ranges';

repetitionRanges(array, searchValue)

array: Array
searchValue: any type (the value to resolve its repetition ranges)
Return: Array of objects with start and end properties

repetitionRanges([1, 1, 1], 1); //=> [{start: 0, end: 2}]
repetitionRanges([1, 1, '1', 1, 1, Symbol('1')], 1); //=> [{start: 0, end: 1}, {start: 3, end: 4}]

repetitionRanges(['a', 'b'], 'c'); //=> []
repetitionRanges(['a', 'b', 'a'], 'a'); //=> []
repetitionRanges([], 'a']); //=> []

License

ISC License © 2018 Shinnosuke Watanabe