diff --git a/__tests__/modules/filter.test.js b/__tests__/modules/filter.test.js new file mode 100644 index 0000000..6229678 --- /dev/null +++ b/__tests__/modules/filter.test.js @@ -0,0 +1,29 @@ +import filter from '../../modules/filter' + +describe('filter', () => { + test('calls the iterator filter if present', () => { + const iterator = { + filter: jest.fn() + } + + const fn = () => {} + filter(fn, iterator) + + expect(iterator.filter).toHaveBeenCalled() + }) + + test('imitates the array.filter on objects', () => { + const obj = { + a: 1, + b: 2, + c: 4 + } + + const fn = n => n % 2 === 0 + const result = filter(fn, obj) + + expect(result).toEqual({ + b: 2, c: 4 + }) + }) +}) \ No newline at end of file diff --git a/__tests__/modules/reduce.test.js b/__tests__/modules/reduce.test.js new file mode 100644 index 0000000..429e24f --- /dev/null +++ b/__tests__/modules/reduce.test.js @@ -0,0 +1,30 @@ +import reduce from '../../modules/reduce' + +describe('identity', () => { + test('calls reduce method if located', () => { + const it = { + reduce: jest.fn() + } + + const fn = () => {} + const start = {} + + reduce(fn, start, it) + + expect(it.reduce).toHaveBeenCalled() + }) + + test('mirrors array reduce with objects', () => { + const obj = { + a: 1, + b: 2, + c: 3 + } + + const fn = (acc, curr) => [...acc, curr * 2] + const start = [] + const result = reduce(fn, start, obj) + + expect(result).toEqual([2, 4, 6]) + }) +}) \ No newline at end of file diff --git a/modules/filter.js b/modules/filter.js new file mode 100644 index 0000000..aa012e3 --- /dev/null +++ b/modules/filter.js @@ -0,0 +1,21 @@ +import curry from './curry' +import prop from './prop' + +const filter = (fn, iterator) => { + if ('filter' in iterator) { + return iterator.filter(fn) + } + + const result = {} + + for (let key in iterator) { + const value = prop(key, iterator) + if (fn(value)) { + result[key] = value + } + } + + return result +} + +export default curry(filter) \ No newline at end of file diff --git a/modules/map.js b/modules/map.js index 5305037..63629d2 100644 --- a/modules/map.js +++ b/modules/map.js @@ -1,16 +1,14 @@ import curry from './curry' -const mapArray = (fn, list) => list.map(fn) - -const map = (fn, list) => { - if (Array.isArray(list)) { - return mapArray(fn, list) +const map = (fn, iterator) => { + if ('map' in iterator) { + return iterator.map(fn) } const result = {} - for(let k in list) { - result[k] = fn(list[k]) + for(let k in iterator) { + result[k] = fn(iterator[k]) } return result diff --git a/modules/reduce.js b/modules/reduce.js new file mode 100644 index 0000000..9879b9e --- /dev/null +++ b/modules/reduce.js @@ -0,0 +1,18 @@ +import curry from './curry' +import prop from './prop' + +const reduce = (fn, start, iterator) => { + if ('reduce' in iterator) { + return iterator.reduce(fn, start) + } + + let result = start + + for(let k in iterator) { + result = fn(result, prop(k, iterator)) + } + + return result +} + +export default curry(reduce) \ No newline at end of file