Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
shinnn committed Dec 14, 2016
0 parents commit 543035a
Show file tree
Hide file tree
Showing 11 changed files with 312 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bower_components
coverage
node_modules
10 changes: 10 additions & 0 deletions .travis.yml
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
20 changes: 20 additions & 0 deletions LICENSE
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.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# indexes-of-sequence

[![NPM version](https://img.shields.io/npm/v/indexes-of-sequence.svg)](https://www.npmjs.com/package/indexes-of-sequence)
[![Bower version](https://img.shields.io/bower/v/indexes-of-sequence.svg)](https://github.com/shinnn/indexes-of-sequence/releases)
[![Build Status](https://travis-ci.org/shinnn/indexes-of-sequence.svg?branch=master)](https://travis-ci.org/shinnn/indexes-of-sequence)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/indexes-of-sequence.svg)](https://coveralls.io/r/shinnn/indexes-of-sequence)
[![dependencies Status](https://david-dm.org/shinnn/indexes-of-sequence/status.svg)](https://david-dm.org/shinnn/indexes-of-sequence)
[![devDependencies Status](https://david-dm.org/shinnn/indexes-of-sequence/dev-status.svg)](https://david-dm.org/shinnn/indexes-of-sequence?type=dev)

Find all indexes at which a given sequence of elements can be found in the array

```javascript
import indexesOfSequence from 'indexes-of-sequence';

indexOfSequence(['a', 'b', 'a', 'b', 'c', 'd', 'a', 'b', 'c'], ['a', 'b', 'c']); //=> [2, 6]
```

## Installation

### [npm](https://www.npmjs.com/)

```
npm install indexes-of-sequence
```

### [bower](https://bower.io/)

```
bower install indexes-of-sequence
```

## API

### indexesOfSequence(*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 `Number` (indexes of the sequence of values)

```javascript
indexOfSequence([1, '1', true, '1', 1, Buffer.from('1'), '1', 1, '1'], [1, '1']); //=> [0, 7]
indexOfSequence([1, '1', true, '1', 1, Buffer.from('1'), '1', 1, '1'], [1, '1'], 1); //=> [7]

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

## Related project

[index-of-sequence](https://github.com/shinnn/index-of-sequence) - Find the first index instead

## License

Copyright (c) 2016 [Shinnosuke Watanabe](https://github.com/shinnn)

Licensed under [the MIT License](./LICENSE).
36 changes: 36 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "indexes-of-sequence",
"description": "Find all indexes at which a given sequence of elements can be found in the array",
"keywords": [
"array",
"all",
"index",
"search",
"find",
"sequence",
"series",
"elements"
],
"main": "module.js",
"moduleType": "es6",
"repository": {
"type": "git",
"url": "git://github.com/shinnn/indexes-of-sequence.git"
},
"authors": [
"Shinnosuke Watanabe (https://github.com/shinnn)"
],
"license": "MIT",
"dependencies": {
"append-type": "^1.0.0",
"arr-indexes-of": "^1.0.1"
},
"ignore": [
"**/.*",
"*.js",
"*.json",
"*.yml",
"coverage",
"node_modules"
]
}
32 changes: 32 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

var appendType = require('append-type');
var arrIndexesOf = require('arr-indexes-of');

/*!
* indexes-of-sequence | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/indexes-of-sequence
*/
function indexesOfSequence(arr, searchValues, fromIndex) {
[arr, searchValues].forEach(function(arg) {
if (!Array.isArray(arg)) {
throw new TypeError('Expected an array, but got ' + appendType(arg) + '.');
}
});

var firstValueIndexes = arrIndexesOf(arr, searchValues[0], fromIndex);

if (firstValueIndexes.length === 0 || searchValues.length === 0) {
return [];
}

var restSearchValues = searchValues.splice(1);

return firstValueIndexes.filter(function(index) {
return restSearchValues.every(function(val, i) {
return arr[index + i + 1] === val;
});
});
}

module.exports = indexesOfSequence;
28 changes: 28 additions & 0 deletions module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*!
* indexes-of-sequence | MIT (c) Shinnosuke Watanabe
* https://github.com/shinnn/indexes-of-sequence
*/
import appendType from 'append-type';
import arrIndexesOf from 'arr-indexes-of';

export default function indexesOfSequence(arr, searchValues, fromIndex) {
[arr, searchValues].forEach(function(arg) {
if (!Array.isArray(arg)) {
throw new TypeError('Expected an array, but got ' + appendType(arg) + '.');
}
});

var firstValueIndexes = arrIndexesOf(arr, searchValues[0], fromIndex);

if (firstValueIndexes.length === 0 || searchValues.length === 0) {
return [];
}

var restSearchValues = searchValues.splice(1);

return firstValueIndexes.filter(function(index) {
return restSearchValues.every(function(val, i) {
return arr[index + i + 1] === val;
});
});
}
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "indexes-of-sequence",
"version": "1.0.0",
"description": "Find all indexes at which a given sequence of elements can be found in the array",
"repository": "shinnn/index-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",
"sequence",
"series",
"elements"
],
"dependencies": {
"append-type": "^1.0.0",
"arr-indexes-of": "^1.0.1"
},
"devDependencies": {
"@shinnn/eslint-config": "^3.3.5",
"eslint": "^3.12.1",
"istanbul": "^0.4.5",
"rollup": "^0.37.0",
"rollup-config-module": "^1.0.0",
"tape": "^4.6.2"
},
"eslintConfig": {
"extends": "@shinnn"
}
}
68 changes: 68 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const indexOfSequence = require('.');
const test = require('tape');

test('indexOfSequence()', t => {
t.deepEqual(
indexOfSequence(['a', 'b', 'c', 'b', 'c'], ['b', 'c']),
[1, 3],
'should return all indexes of the search sequence.'
);

t.deepEqual(
indexOfSequence([1, 3, '1', '3', 1, 1, 3], [1, 3], 1),
[5],
'should support the third `fromIndex` parameter.'
);

t.deepEqual(
indexOfSequence(['foo', 'bar'], ['bar'], -9999),
[1],
'should treat negative `fromIndex` as 0.'
);

t.deepEqual(
indexOfSequence(['Hello', 'world'], ['Hello', Buffer.from('world')]),
[],
'should return an empty array when the it cannot find the sequence anywhere.'
);

t.deepEqual(
indexOfSequence([], ['p']),
[],
'should return an empty array when the first argument is an empty array.'
);

t.deepEqual(
indexOfSequence(['q'], []),
[],
'should return an empty when the second argument is an empty array.'
);

t.throws(
() => indexOfSequence(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(
() => indexOfSequence([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(
() => indexOfSequence(['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(
() => indexOfSequence(['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();
});

0 comments on commit 543035a

Please sign in to comment.