Skip to content

Commit

Permalink
[ADD] any
Browse files Browse the repository at this point in the history
  • Loading branch information
HubCodes committed Sep 17, 2018
1 parent 409ecb1 commit 52c289f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions any/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function any(iterable, predicator) {
for (const element of iterable) {
if (predicator(element)) {
return true;
}
}
return false;
}
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const all = require('./all');
const andThen = require('./andThen');
const any = require('./any');
const compose = require('./compose');
const filter = require('./filter');
const foldLeft = require('./foldLeft');
Expand All @@ -12,6 +13,7 @@ const zip = require('./zip');
module.exports = {
all,
andThen,
any,
compose,
filter,
foldLeft,
Expand Down
24 changes: 24 additions & 0 deletions test/any.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const assert = require('assert');
const $ = require('../');

describe('any test', () => {
const array = [2, 4, 5, 8, 10];
it('work with array', () => {
const result = $.any(array, (x) => x % 2 == 1);
assert.equal(result, true);
});
it('work with Set', () => {
const set = new Set(array);
set.delete(5);
const result = $.any(set, (x) => x % 2 == 1);
assert.equal(result, false);
});
it('work with Map', () => {
const map = new Map();
for (let i = 3; i <= 9; i++) {
map.set(i, i * 2);
}
const result = $.any(map, (x) => x[0] % 2 == 1);
assert.deepEqual(result, true);
});
});

0 comments on commit 52c289f

Please sign in to comment.