Skip to content

Commit

Permalink
feat(Modules): Add reduce and filter
Browse files Browse the repository at this point in the history
Use native if present
  • Loading branch information
beardedtim committed Sep 22, 2017
1 parent d934ac4 commit 5b7aeb3
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 7 deletions.
29 changes: 29 additions & 0 deletions __tests__/modules/filter.test.js
Original file line number Diff line number Diff line change
@@ -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
})
})
})
30 changes: 30 additions & 0 deletions __tests__/modules/reduce.test.js
Original file line number Diff line number Diff line change
@@ -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])
})
})
21 changes: 21 additions & 0 deletions modules/filter.js
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 5 additions & 7 deletions modules/map.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions modules/reduce.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 5b7aeb3

Please sign in to comment.