-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Modules): Add reduce and filter
Use native if present
- Loading branch information
1 parent
d934ac4
commit 5b7aeb3
Showing
5 changed files
with
103 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |