-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9cee708
Showing
11 changed files
with
311 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_style = space | ||
indent_size = 2 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
bower_components | ||
coverage | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
dist: trusty | ||
git: | ||
depth: 1 | ||
branches: | ||
except: /^v\d/ | ||
language: node_js | ||
node_js: node | ||
after_script: | ||
- npm install istanbul-coveralls | ||
- node node_modules/.bin/istanbul-coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2016 Shinnosuke Watanabe | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# sequence-positions | ||
|
||
[![NPM version](https://img.shields.io/npm/v/sequence-positions.svg)](https://www.npmjs.com/package/sequence-positions) | ||
[![Bower version](https://img.shields.io/bower/v/sequence-positions.svg)](https://github.com/shinnn/sequence-positions/releases) | ||
[![Build Status](https://travis-ci.org/shinnn/sequence-positions.svg?branch=master)](https://travis-ci.org/shinnn/sequence-positions) | ||
[![Coverage Status](https://img.shields.io/coveralls/shinnn/sequence-positions.svg)](https://coveralls.io/r/shinnn/sequence-positions) | ||
|
||
Resolve all start/end positions at which a given sequence of elements can be found in the array | ||
|
||
```javascript | ||
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}] | ||
``` | ||
|
||
|index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | | ||
|:------|---|---|---|---|---|---|---|---|---| | ||
|array | a | b | a | b | c | d | a | b | c | | ||
|matched| | | ○ | ○ | ○ | | ○ | ○ | ○ | | ||
|
||
## Installation | ||
|
||
### [npm](https://www.npmjs.com/) | ||
|
||
``` | ||
npm install sequence-positions | ||
``` | ||
|
||
### [bower](https://bower.io/) | ||
|
||
``` | ||
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 | ||
|
||
```javascript | ||
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](https://github.com/shinnn) | ||
|
||
Licensed under [the MIT License](./LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{ | ||
"name": "sequence-positions", | ||
"description": "Resolve all start/end positions at which a given sequence of elements can be found in the array", | ||
"keywords": [ | ||
"array", | ||
"all", | ||
"index", | ||
"search", | ||
"find", | ||
"range", | ||
"position", | ||
"start", | ||
"end", | ||
"sequence", | ||
"series", | ||
"elements" | ||
], | ||
"main": "module.js", | ||
"moduleType": "es6", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/shinnn/sequence-positions.git" | ||
}, | ||
"authors": [ | ||
"Shinnosuke Watanabe (https://github.com/shinnn)" | ||
], | ||
"license": "MIT", | ||
"dependencies": { | ||
"indexes-of-sequence": "^1.0.3" | ||
}, | ||
"ignore": [ | ||
"**/.*", | ||
"*.js", | ||
"*.json", | ||
"*.yml", | ||
"coverage", | ||
"node_modules" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
var indexesOfSequence = require('indexes-of-sequence'); | ||
|
||
/*! | ||
* sequence-positions | MIT (c) Shinnosuke Watanabe | ||
* https://github.com/shinnn/sequence-positions | ||
*/ | ||
function sequencePositions(arr, searchValues, fromIndex) { | ||
var endPositionOffset = searchValues.length - 1; | ||
|
||
return indexesOfSequence(arr, searchValues, fromIndex).map(function(startPosition) { | ||
return { | ||
start: startPosition, | ||
end: startPosition + endPositionOffset | ||
}; | ||
}); | ||
} | ||
|
||
module.exports = sequencePositions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/*! | ||
* sequence-positions | MIT (c) Shinnosuke Watanabe | ||
* https://github.com/shinnn/sequence-positions | ||
*/ | ||
import indexesOfSequence from 'indexes-of-sequence'; | ||
|
||
export default function sequencePositions(arr, searchValues, fromIndex) { | ||
var endPositionOffset = searchValues.length - 1; | ||
|
||
return indexesOfSequence(arr, searchValues, fromIndex).map(function(startPosition) { | ||
return { | ||
start: startPosition, | ||
end: startPosition + endPositionOffset | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"name": "sequence-positions", | ||
"version": "1.0.0", | ||
"description": "Resolve all start/end positions at which a given sequence of elements can be found in the array", | ||
"repository": "shinnn/ranges-of-sequence", | ||
"author": "Shinnosuke Watanabe (https://github.com/shinnn)", | ||
"scripts": { | ||
"prebuild": "eslint --fix --format=codeframe module.js test.js", | ||
"build": "rollup --config=node:module", | ||
"pretest": "npm run-script build", | ||
"test": "istanbul cover test.js" | ||
}, | ||
"license": "MIT", | ||
"module": "module.js", | ||
"files": [ | ||
"index.js", | ||
"module.js" | ||
], | ||
"keywords": [ | ||
"array", | ||
"all", | ||
"index", | ||
"search", | ||
"find", | ||
"range", | ||
"position", | ||
"start", | ||
"end", | ||
"sequence", | ||
"series", | ||
"elements" | ||
], | ||
"dependencies": { | ||
"indexes-of-sequence": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@shinnn/eslint-config": "^3.3.5", | ||
"eslint": "^3.12.2", | ||
"istanbul": "^0.4.5", | ||
"rollup": "^0.37.2", | ||
"rollup-config-module": "^1.0.0", | ||
"tape": "^4.6.2" | ||
}, | ||
"eslintConfig": { | ||
"extends": "@shinnn" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict'; | ||
|
||
const sequencePositions = require('.'); | ||
const test = require('tape'); | ||
|
||
test('sequencePositions()', t => { | ||
const arr = ['a', 'b', 'c', 'b', 'c']; | ||
const searches = ['b', 'c']; | ||
|
||
t.deepEqual( | ||
sequencePositions(arr, searches), | ||
[{start: 1, end: 2}, {start: 3, end: 4}], | ||
'should return all start/end positions of the search sequence.' | ||
); | ||
|
||
t.deepEqual( | ||
arr, | ||
['a', 'b', 'c', 'b', 'c'], | ||
'should not mutate the array passed to the first parameter.' | ||
); | ||
|
||
t.deepEqual( | ||
searches, | ||
['b', 'c'], | ||
'should not mutate the array passed to the first parameter.' | ||
); | ||
|
||
t.deepEqual( | ||
sequencePositions([1, 3, '1', '3', 1, 1, 3], [1, 3], 1), | ||
[{start: 5, end: 6}], | ||
'should support the third `fromIndex` parameter.' | ||
); | ||
|
||
t.deepEqual( | ||
sequencePositions(['foo', 'bar'], ['bar'], -9999), | ||
[{start: 1, end: 1}], | ||
'should treat negative `fromIndex` as 0.' | ||
); | ||
|
||
t.deepEqual( | ||
sequencePositions(['Hello', 'world'], ['Hello', Buffer.from('world')]), | ||
[], | ||
'should return an empty array when the it cannot find the sequence anywhere.' | ||
); | ||
|
||
t.deepEqual( | ||
sequencePositions([], ['p']), | ||
[], | ||
'should return an empty array when the first argument is an empty array.' | ||
); | ||
|
||
t.deepEqual( | ||
sequencePositions(['q'], []), | ||
[], | ||
'should return an empty when the second argument is an empty array.' | ||
); | ||
|
||
t.throws( | ||
() => sequencePositions(1, ['a']), | ||
/^TypeError.*Expected an array, but got 1 \(number\)\./, | ||
'should throw a type error when the first argument is a non-array value.' | ||
); | ||
|
||
t.throws( | ||
() => sequencePositions([true], new Map()), | ||
/^TypeError.*Expected an array, but got \[object Map] \(object\)\./, | ||
'should throw a type error when the second argument is a non-array value.' | ||
); | ||
|
||
t.throws( | ||
() => sequencePositions(['a'], ['b'], 'c'), | ||
/^TypeError.*Expected an index where to start the searching forwards in the array, but got c \(string\)\./, | ||
'should throw a type error when the third argument is not a number.' | ||
); | ||
|
||
t.throws( | ||
() => sequencePositions(['a'], ['b'], ''), | ||
/^TypeError.*Expected an index where to start the searching forwards in the array, but got an empty string\./, | ||
'should throw a type error when the third argument is an empty string.' | ||
); | ||
|
||
t.end(); | ||
}); |