This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
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.
- Loading branch information
Showing
4 changed files
with
63 additions
and
13 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,42 @@ | ||
/* eslint-disable no-nested-ternary */ | ||
import reduceBy from '../reduceBy'; | ||
|
||
describe('Core.reduceBy', () => { | ||
test('splits the list into groups according to the grouping function', () => { | ||
const grade = score => | ||
score < 65 | ||
? 'F' | ||
: score < 70 ? 'D' : score < 80 ? 'C' : score < 90 ? 'B' : 'A'; | ||
|
||
const students = [ | ||
{ name: 'Abby', score: 84 }, | ||
{ name: 'Brad', score: 73 }, | ||
{ name: 'Chris', score: 89 }, | ||
{ name: 'Dianne', score: 99 }, | ||
{ name: 'Eddy', score: 58 }, | ||
{ name: 'Fred', score: 67 }, | ||
{ name: 'Gillian', score: 91 }, | ||
{ name: 'Hannah', score: 78 }, | ||
{ name: 'Irene', score: 85 }, | ||
{ name: 'Jack', score: 69 }, | ||
]; | ||
|
||
const byGrade = student => grade(student.score || 0); | ||
const collectNames = (acc, student) => acc.concat(student.name); | ||
|
||
expect(reduceBy(collectNames, [], byGrade, students)).toEqual({ | ||
A: ['Dianne', 'Gillian'], | ||
B: ['Abby', 'Chris', 'Irene'], | ||
C: ['Brad', 'Hannah'], | ||
D: ['Fred', 'Jack'], | ||
F: ['Eddy'], | ||
}); | ||
}); | ||
|
||
test('returns an empty object if given an empty array', () => { | ||
const byType = obj => obj.type; | ||
const sumValues = (acc, obj) => acc + obj.val; | ||
|
||
expect(reduceBy(sumValues, 0, byType, [])).toEqual({}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,16 +1,6 @@ | ||
import reduce from './reduce'; | ||
import prop from './prop'; | ||
import assoc from './assoc'; | ||
import reduceBy from './reduceBy'; | ||
import inc from './inc'; | ||
|
||
const countBy = (fn, list) => | ||
reduce( | ||
(acc, item) => { | ||
const key = fn(item); | ||
const existingKey = prop(key, acc); | ||
return assoc(key, existingKey ? existingKey + 1 : 1, acc); | ||
}, | ||
{}, | ||
list, | ||
); | ||
const countBy = (keyFn, list) => reduceBy(inc, 0, keyFn, list); | ||
|
||
export { countBy as default }; |
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,16 @@ | ||
import reduce from './reduce'; | ||
import has from './has'; | ||
import assoc from './assoc'; | ||
|
||
const reduceBy = (valFn, valAcc, keyFn, list) => | ||
reduce( | ||
(acc, item) => { | ||
const key = keyFn(item); | ||
const val = valFn(has(key, acc) ? acc[key] : valAcc, item); | ||
return assoc(key, val, acc); | ||
}, | ||
{}, | ||
list, | ||
); | ||
|
||
export { reduceBy as default }; |