diff --git a/src/LazyList.js b/src/LazyList.js index 1b0cc44..c9c003d 100644 --- a/src/LazyList.js +++ b/src/LazyList.js @@ -56,6 +56,7 @@ export default class LazyList { const { list, operations } = this; const listLength = list.length; const operationsLength = operations.length; + const { type: lastOperationType } = operations[operationsLength - 1]; const result = []; let reducerAcc; @@ -90,12 +91,12 @@ export default class LazyList { } } - if (nextItem.valid) { + if (lastOperationType !== LIST_OPERATIONS.REDUCE && nextItem.valid) { result.push(nextItem.value); } } - if (reducerAcc) { + if (lastOperationType === LIST_OPERATIONS.REDUCE) { return reducerAcc; } diff --git a/src/LazyList.test.js b/src/LazyList.test.js index 7ab64ee..f48f4cc 100644 --- a/src/LazyList.test.js +++ b/src/LazyList.test.js @@ -86,6 +86,7 @@ describe('LazyList.map', () => { describe('LazyList.filter', () => { it('should filter value correctly', () => { expect(lazy([0, 1, 2]).filter(v => v > 0).value()).toEqual([1, 2]); + expect(lazy([0, true, false]).filter(v => !!v).value()).toEqual([true]); expect(lazy([ { name: 'Joe', age: 3 }, { name: 'Spike', age: 16 }, @@ -103,6 +104,7 @@ describe('LazyList.filter', () => { describe('LazyList.reduce', () => { it('should reduce to correctly value', () => { expect(lazy([1, 2, 3]).reduce((a, b) => a + b)).toEqual(6); + expect(lazy([0, 0]).reduce((a, b) => a + b)).toEqual(0); }); it('should reduce to correctly value when provide initial value', () => {