deno.land / x / rambda@v9.1.1 / source / reduceBy.spec.js

reduceBy.spec.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { reduceBy } from './reduceBy.js'import { prop } from './prop.js'
const byType = prop('type')const sumValues = function (acc, obj){ return acc + obj.val}
const grade = function (score){ return score < 65 ? 'F' : score < 70 ? 'D' : score < 80 ? 'C' : score < 90 ? 'B' : 'A'}
const byGrade = function (student){ return grade(student.score || 0)}
test('splits the list into groups according to the grouping function', () => { const collectNames = function (acc, student){ return acc.concat(student.name) } expect(reduceBy( collectNames, [], byGrade, getStudents() )).toEqual({ A : [ 'Dianne', 'Gillian' ], B : [ 'Abby', 'Chris', 'Irene' ], C : [ 'Brad', 'Hannah' ], D : [ 'Fred', 'Jack' ], F : [ 'Eddy' ], })})
test('splits the list into mutation-free groups', () => { const collectNames = function (acc, student){ acc.push(student.name)
return acc } expect(reduceBy( collectNames, [], byGrade, getStudents() )).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', () => { expect(reduceBy( sumValues, 0, byType, [] )).toEqual({})})
function getStudents(){ return [ { 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, }, ]}
rambda

Version Info

Tagged at
2 months ago