Skip to content

Commit

Permalink
[ADD] all
Browse files Browse the repository at this point in the history
  • Loading branch information
HubCodes committed Sep 16, 2018
1 parent 457ec30 commit 409ecb1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions all/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = function all(iterable, predicator) {
for (const element of iterable) {
if (!predicator(element)) {
return false;
}
}
return true;
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const all = require('./all');
const andThen = require('./andThen');
const compose = require('./compose');
const filter = require('./filter');
Expand All @@ -9,8 +10,9 @@ const partition = require('./partition');
const zip = require('./zip');

module.exports = {
compose,
all,
andThen,
compose,
filter,
foldLeft,
foldRight,
Expand Down
23 changes: 23 additions & 0 deletions test/all.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const assert = require('assert');
const $ = require('../');

describe('all test', () => {
const array = [1, 3, 5, 7, 9];
it('work with array', () => {
const result = $.all(array, (x) => x % 2 == 1);
assert.equal(result, true);
});
it('work with Set', () => {
const set = new Set(array);
const result = $.all(set, (x) => x % 2 == 0);
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 = $.all(map, (x) => x[0] % 2 == 0);
assert.deepEqual(result, false);
});
});

0 comments on commit 409ecb1

Please sign in to comment.