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

curryN.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { curryN } from './curryN.js'
function multiply( a, b, c, d, e, f, g, h, i, j, k, l){ if (l){ return a * b * c * d * e * f * g * h * i * j * k * l } if (k){ return a * b * c * d * e * f * g * h * i * j * k } if (j){ return a * b * c * d * e * f * g * h * i * j } if (i){ return a * b * c * d * e * f * g * h * i } if (h){ return a * b * c * d * e * f * g * h } if (g){ return a * b * c * d * e * f * g } if (f){ return a * b * c * d * e * f } if (e){ return a * b * c * d * e }
return a * b * c}
test('accepts an arity', () => { const curried = curryN(3, multiply) expect(curried(1)(2)(3)).toBe(6) expect(curried(1, 2)(3)).toBe(6) expect(curried(1)(2, 3)).toBe(6) expect(curried( 1, 2, 3 )).toBe(6)})
test('can be partially applied', () => { const curry3 = curryN(3) const curried = curry3(multiply) expect(curried).toHaveLength(3) expect(curried(1)(2)(3)).toBe(6) expect(curried(1, 2)(3)).toBe(6) expect(curried(1)(2, 3)).toBe(6) expect(curried( 1, 2, 3 )).toBe(6)})
test('preserves context', () => { const ctx = { x : 10 } const f = function (a, b){ return a + b * this.x } const g = curryN(2, f)
expect(g.call( ctx, 2, 4 )).toBe(42) expect(g.call(ctx, 2).call(ctx, 4)).toBe(42)})
test('number of arguments is 4', () => { const fn = curryN(4, multiply) expect(fn( 1, 2, 3, 4 )).toBe(6)})
test('number of arguments is 5', () => { const fn = curryN(5, multiply) expect(fn( 1, 2, 3, 4, 5 )).toBe(120)})
test('number of arguments is 6', () => { const fn = curryN(6, multiply) expect(fn( 1, 2, 3, 4, 5, 6 )).toBe(720)})
test('number of arguments is 7', () => { const fn = curryN(7, multiply) expect(fn( 1, 2, 3, 4, 5, 6, 7 )).toBe(5040)})
test('number of arguments is 8', () => { const fn = curryN(8, multiply) expect(fn( 1, 2, 3, 4, 5, 6, 7, 8 )).toBe(40320)})
test('number of arguments is 9', () => { const fn = curryN(9, multiply) expect(fn( 1, 2, 3, 4, 5, 6, 7, 8, 9 )).toBe(362880)})
test('number of arguments is 10', () => { const fn = curryN(10, multiply) expect(fn( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )).toBe(3628800)})
test('number of arguments is 11', () => { expect(() => { const fn = curryN(11, multiply) fn( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ) }).toThrowWithMessage(Error, 'First argument to _arity must be a non-negative integer no greater than ten')})
test('forwards extra arguments', () => { const createArray = function (){ return Array.prototype.slice.call(arguments) } const fn = curryN(3, createArray)
expect(fn( 1, 2, 3 )).toEqual([ 1, 2, 3 ]) expect(fn( 1, 2, 3, 4 )).toEqual([ 1, 2, 3, 4 ]) expect(fn(1, 2)(3, 4)).toEqual([ 1, 2, 3, 4 ]) expect(fn(1)( 2, 3, 4 )).toEqual([ 1, 2, 3, 4 ]) expect(fn(1)(2)(3, 4)).toEqual([ 1, 2, 3, 4 ])})
rambda

Version Info

Tagged at
2 months ago