Add reduce
iterables utility
This commit is contained in:
parent
7cf7b1fca5
commit
4495a1ac67
3 changed files with 33 additions and 2 deletions
|
@ -11,6 +11,7 @@ import {
|
|||
groupBy,
|
||||
isIterable,
|
||||
map,
|
||||
reduce,
|
||||
size,
|
||||
take,
|
||||
} from '../../util/iterables';
|
||||
|
@ -311,6 +312,23 @@ describe('iterable utilities', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('reduce', () => {
|
||||
it('returns the accumulator when passed an empty iterable', () => {
|
||||
const fn = sinon.fake();
|
||||
|
||||
assert.strictEqual(reduce([], fn, 123), 123);
|
||||
|
||||
sinon.assert.notCalled(fn);
|
||||
});
|
||||
|
||||
it('iterates over the iterable, ultimately returning a result', () => {
|
||||
assert.strictEqual(
|
||||
reduce(new Set([1, 2, 3, 4]), (result, n) => result + n, 89),
|
||||
99
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('take', () => {
|
||||
it('returns the first n elements from an iterable', () => {
|
||||
const everyNumber = {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue