Skip to content

Commit

Permalink
Merge branch 'master' of github.com:mout/mout
Browse files Browse the repository at this point in the history
  • Loading branch information
roboshoes committed Mar 3, 2016
2 parents 47ac9cc + 3f8ef06 commit 0349567
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
21 changes: 21 additions & 0 deletions doc/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,27 @@ more info at [MDN Array#indexOf](https://developer.mozilla.org/en/JavaScript/Ref



## indicesOf(arr, item, [fromIndex]):Number

Returns an array of indices where `item` is found in the array.

Like `array/indexOf` it does loop over sparse items in the array. The optional
`fromIndex` parameter can limit the scope, the same way as it does in indexOf.

```js
var items = ['lorem', 'ipsum', 'foo', 'ipsum', 'ipsum'];

indicesOf(items, 'ipsum');
// > [1, 3, 4]

indicesOf(items, 'ipsum', 1);
// > [3, 4]
```





## insert(arr, ...items):Number

Push items into array only if they aren't contained by it. Returns the new
Expand Down
1 change: 1 addition & 0 deletions src/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ return {
'forEach' : require('./array/forEach'),
'groupBy' : require('./array/groupBy'),
'indexOf' : require('./array/indexOf'),
'indicesOf' : require('./array/indicesOf'),
'insert' : require('./array/insert'),
'intersection' : require('./array/intersection'),
'invoke' : require('./array/invoke'),
Expand Down
28 changes: 28 additions & 0 deletions src/array/indicesOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
define(function () {

/**
* Array indicesOf
*/
function indicesOf(arr, item, fromIndex) {
var results = [];
if (arr == null) {
return results;
}

fromIndex = typeof fromIndex === 'number' ? fromIndex : 0;

var length = arr.length;
var cursor = fromIndex >= 0 ? fromIndex : length + fromIndex;

while (cursor < length) {
if (arr[cursor] === item) {
results.push(cursor);
}
cursor++;
}

return results;
}

return indicesOf;
});
85 changes: 85 additions & 0 deletions tests/spec/array/spec-indicesOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
define(['mout/array/indicesOf'], function (indicesOf) {

describe('array/indicesOf()', function() {

it('should work in regular arrays', function() {
var arr = [1, 'a', 'a', 4, 'a', 4];
expect( indicesOf(arr, 1) ).toEqual( [0] );
expect( indicesOf(arr, 'a') ).toEqual( [1, 2, 4] );
expect( indicesOf(arr, 4) ).toEqual( [3, 5] );
expect( indicesOf(arr, 'foo') ).toEqual( [] );
});

it('should iterate over sparse items. see #64', function() {
var arr = [];
arr[1] = 1;
arr[3] = 'a';
arr[4] = undefined; // it's a trap!
arr[6] = 1;

expect( indicesOf(arr, 1) ).toEqual( [1, 6] );
expect( indicesOf(arr, 'a') ).toEqual( [3] );
expect( indicesOf(arr, undefined) ).toEqual( [0, 2, 4, 5] );

expect( indicesOf(arr, 'foo') ).toEqual( [] );
});

it('should handle fromIndex', function() {
var arr = [1, 'a', 2, 'b', 1];

expect( indicesOf(arr, 1, 2) ).toEqual( [4] );
expect( indicesOf(arr, 1, -1) ).toEqual( [4] );
expect( indicesOf(arr, 1, 4) ).toEqual( [4] );
expect( indicesOf(arr, 1, 5) ).toEqual( [] );

expect( indicesOf(arr, 'a', 0) ).toEqual( [1] );
expect( indicesOf(arr, 'a', 1) ).toEqual( [1] );
expect( indicesOf(arr, 'a', 2) ).toEqual( [] );

expect( indicesOf(arr, 2, 2) ).toEqual( [2] );
expect( indicesOf(arr, 2, 3) ).toEqual( [] );

expect( indicesOf(arr, 'b', 2) ).toEqual( [3] );
expect( indicesOf(arr, 'b', 4) ).toEqual( [] );

expect( indicesOf(arr, 'foo', 2) ).toEqual( [] );
});

it('should handle negative fromIndex', function() {
var arr = [1, 'a', 2, 'b', 1];

expect( indicesOf(arr, 1, -2) ).toEqual( [4] );
expect( indicesOf(arr, 1, -12) ).toEqual( [0, 4] );

expect( indicesOf(arr, 'a', -3) ).toEqual( [] );
expect( indicesOf(arr, 'a', -4) ).toEqual( [1] );

expect( indicesOf(arr, 'b', -1) ).toEqual( [] );
expect( indicesOf(arr, 'b', -2) ).toEqual( [3] );
expect( indicesOf(arr, 'b', -3) ).toEqual( [3] );

expect( indicesOf(arr, 'foo', -2) ).toEqual( [] );
});

it('should handle fromIndex greater than length', function() {
var arr = [1, 'a', 2, 'b', 1];

expect( indicesOf(arr, 1, 10) ).toEqual( [] );
expect( indicesOf(arr, 'a', 100) ).toEqual( [] );
expect( indicesOf(arr, 2, 1000) ).toEqual( [] );
expect( indicesOf(arr, 'b', 15) ).toEqual( [] );

expect( indicesOf(arr, 'foo', 15) ).toEqual( [] );
});

it('should return an empty array if array is null/undefined', function() {
expect( indicesOf(null, 1) ).toEqual( [] );
expect( indicesOf(undefined, 1) ).toEqual( [] );

expect( indicesOf(null, 1, 15) ).toEqual( [] );
expect( indicesOf(undefined, 1, 15) ).toEqual( [] );
});

});

});
1 change: 1 addition & 0 deletions tests/spec/spec-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ define([
'./array/spec-forEach',
'./array/spec-groupBy',
'./array/spec-indexOf',
'./array/spec-indicesOf',
'./array/spec-insert',
'./array/spec-intersection',
'./array/spec-invoke',
Expand Down

0 comments on commit 0349567

Please sign in to comment.