1.0.0 • Published 7 years ago

sequence-positions v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

sequence-positions

NPM version Bower version Build Status Coverage Status

Resolve all start/end positions at which a given sequence of elements can be found in the array

import sequencePositions from 'sequence-positions';

sequencePositions(['a', 'b', 'a', 'b', 'c', 'd', 'a', 'b', 'c'], ['a', 'b', 'c']);
//=> [{start: 2, end: 4}, {start: 6, end: 8}]
index012345678
arrayababcdabc
matched

Installation

npm

npm install sequence-positions

bower

bower install sequence-positions

API

sequencePositions(array, searchArray , fromIndex)

array: Array
searchArray: Array (the sequence of values to search for)
fromIndex: Number (index in the array where to begin searching)
Return: Array of objects with start and end properties

indexOfSequence([1, '1', true, '1', 1, Buffer.from('1'), '1', 1, '1'], [1, '1']);
//=> [{start: 0, end: 1}, {start: 7, end: 8}]
indexOfSequence([1, '1', true, '1', 1, Buffer.from('1'), '1', 1, '1'], [1, '1'], 1);
//=> [{start: 7, end: 8}]

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

License

Copyright (c) 2016 Shinnosuke Watanabe

Licensed under the MIT License.