├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Dominic Tarr 2 | 3 | Permission is hereby granted, free of charge, 4 | to any person obtaining a copy of this software and 5 | associated documentation files (the "Software"), to 6 | deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom 10 | the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 20 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # indexes-of 2 | 3 | like Array/String#indexOf but return all the indexes in an array. 4 | 5 | ``` js 6 | var indexesOf = require('indexes-of') 7 | 8 | var twosIndexes = indexesOf([1, 2, 3, 4, 5, 4, 3, 2, 1], 2) 9 | 10 | console.log(twosIndexes) 11 | 12 | // [1, 7] 13 | 14 | ``` 15 | 16 | # Haiku 17 | 18 | * A 5 line module. 19 | * But the tests are 40 lines. 20 | * npm publish. 21 | 22 | ## License 23 | 24 | MIT 25 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (ary, item) { 2 | var i = -1, indexes = [] 3 | while((i = ary.indexOf(item, i + 1)) !== -1) 4 | indexes.push(i) 5 | return indexes 6 | } 7 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "indexes-of", 3 | "description": "line String/Array#indexOf but return all the indexes in an array", 4 | "version": "1.0.1", 5 | "homepage": "https://github.com/dominictarr/indexes-of", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/dominictarr/indexes-of.git" 9 | }, 10 | "scripts": { 11 | "test": "node test.js" 12 | }, 13 | "author": "Dominic Tarr (dominictarr.com)", 14 | "license": "MIT", 15 | "devDependencies": { 16 | "tape": "~2.1.0" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var tape = require('tape') 2 | 3 | var indexes = require('./') 4 | 5 | tape('indexes of - 2 matches', function (t) { 6 | var x = indexes([1,2,3,2,4,5,9,8,0], 2) 7 | t.deepEqual(x, [1,3]) 8 | t.end() 9 | }) 10 | 11 | 12 | tape('indexes of - 1 match', function (t) { 13 | var x = indexes([1,2,3,2,4,5,9,8,0], 3) 14 | t.deepEqual(x, [2]) 15 | t.end() 16 | }) 17 | 18 | 19 | tape('indexes of - empty', function (t) { 20 | var x = indexes([1,2,3, 2,4,5,9,8,0], 24) 21 | t.deepEqual(x, []) 22 | t.end() 23 | }) 24 | 25 | 26 | tape('indexes of - all matches', function (t) { 27 | var x = indexes([8,8,8,8,8,8,8], 8) 28 | t.deepEqual(x, [0,1,2,3,4,5,6]) 29 | t.end() 30 | }) 31 | 32 | 33 | tape('indexes of - string', function (t) { 34 | var x = indexes('foo bar baz foo', 'foo') 35 | t.deepEqual(x, [0, 12]) 36 | t.end() 37 | }) 38 | --------------------------------------------------------------------------------