forked from nan-ci/beta-01-E01
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbonus.js
249 lines (214 loc) · 7.56 KB
/
bonus.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
*/
const ref = { lol: {} }
const rng = Array(100000).fill().map(Math.random)
const total = rng.reduce((a, b) => a + b)
module.exports = ({ describe, test, $, exports }) => [
describe('BONUS', [
/*
#### bonus `makeGet`
export a function `makeGet` that takes one argument and return a function that
takes no arguments but return the previously given argument
*/
test.defined('makeGet'),
test.type('makeGet', Function),
test('makeGet should take 1 argument')
.value(exports.makeGet)
.map('length')
.equal(1),
test('makeGet should return a function')
.value(exports.makeGet)
.map(fn => typeof fn('5'))
.equal('function'),
test('the returned function by makeGet should take no arguments')
.value(exports.makeGet)
.map(fn => fn('5').length)
.equal(0),
test('the returned function should by makeGet return the given argument')
.value(exports.makeGet)
.map(fn => fn(ref)())
.equal(ref),
/*
#### bonus `hideMe`
export a function `hideMe` that takes one object argument
and return a function that takes a string arguments and return the corresponding
values from the previously given object
*/
test.defined('hideMe'),
test.type('hideMe', Function),
test('hideMe should take 1 argument')
.value(exports.hideMe)
.map('length')
.equal(1),
test('hideMe should return a function')
.value(exports.hideMe)
.map(fn => typeof fn({}))
.equal('function'),
test('the returned function by hideMe should take one argument')
.value(exports.hideMe)
.map(fn => fn({}).length)
.equal(1),
test('the returned function should by hideMe return the value at the given '
+'key in the previously given object')
.value(exports.hideMe)
.map(fn => fn(ref)('lol'))
.equal(ref.lol),
/*
#### bonus `safe`
export a function `safe` that takes no arguments
and return an object that contain two functions, `set`, and `get`.
The function `set` take any value argument, store it and return generated key
If a second argument is given to set, it will be used as a key
and update the value at the given key
The function `get` takes one of the generated key returned from `set`
and return the corresponding value
The generated keys must be empty object references generated by `Math.random()`
*/
test.defined('safe'),
test.type('safe', Function),
test('safe should take no arguments')
.value(exports.safe)
.map('length')
.equal(0),
test('safe should return an object')
.value(exports.safe)
.map(fn => fn() && typeof fn())
.equal('object'),
test('the returned object by safe should contain a function set')
.value(exports.safe)
.map(fn => typeof fn().set)
.equal('function'),
test('the returned object by safe should contain a function get')
.value(exports.safe)
.map(fn => typeof fn().get)
.equal('function'),
test('safe() should be able to store a few different values')
.value(exports.safe)
.map(fn => (({ get, set }) => rng
.map(n => set(n))
.map(get)
.reduce((a, b) => Number(a) + Number(b)))(fn()))
.equal(total),
test('safe() should always use a new object')
.value(exports.safe)
.map(fn => fn().get(fn().set(ref)))
.equal(undefined),
test('safe() same values should return differents key every set')
.value(exports.safe)
.map(fn => (({ set }) => set(ref) === set(ref))(fn()))
.equal(false),
test('safe() should always return a different key')
.value(exports.safe)
.map(fn => fn().set(ref) === fn().set(ref))
.equal(false),
test('safe().set should return a key, calling get with this key should'
+'return the first arguement of set')
.value(exports.safe)
.map(fn => {
const { set, get } = fn()
const key = set(ref)
return get(key)
})
.equal(ref),
test('the second argument of safe().set should be use to set a specific key'
+'instead of creating a new one')
.value(exports.safe)
.map(fn => {
const { set, get } = fn()
const key = set('lol')
set(ref, key)
return get(key)
})
.equal(ref),
test('the type of the key should be \'number\'')
.value(exports.safe)
.map(fn => fn().set() && typeof fn().set())
.equal('number'),
/*
#### bonus `superSafe`
export a function `superSafe` that work's exactly like `safe` but the generated
keys must be empty object references generated by `Object.create(null)`
*/
test.defined('superSafe'),
test.type('superSafe', Function),
test('superSafe should take no arguments')
.value(exports.superSafe)
.map('length')
.equal(0),
test('superSafe should return an object')
.value(exports.superSafe)
.map(fn => fn() && typeof fn())
.equal('object'),
test('the returned object by superSafe should contain a function set')
.value(exports.superSafe)
.map(fn => typeof fn().set)
.equal('function'),
test('the returned object by superSafe should contain a function get')
.value(exports.superSafe)
.map(fn => typeof fn().get)
.equal('function'),
test('superSafe() should be able to store a few different values')
.value(exports.superSafe)
.map(fn => (({ get, set }) => rng
.map(n => set(n))
.map(get)
.reduce((a, b) => Number(a) + Number(b)))(fn()))
.equal(total),
test('superSafe() should always use a new object')
.value(exports.superSafe)
.map(fn => fn().get(fn().set(ref)))
.equal(undefined),
test('superSafe() same values should return differents key every set')
.value(exports.superSafe)
.map(fn => (({ set }) => set(ref) === set(ref))(fn()))
.equal(false),
test('superSafe() should always return a different key')
.value(exports.superSafe)
.map(fn => fn().set(ref) === fn().set(ref))
.equal(false),
test('superSafe().set should return a key, calling get with this key should'
+'return the first arguement of set')
.value(exports.superSafe)
.map(fn => {
const { set, get } = fn()
const key = set(ref)
return get(key)
})
.equal(ref),
test('the second argument of superSafe().set should be use to set a specific key'
+'instead of creating a new one')
.value(exports.superSafe)
.map(fn => {
const { set, get } = fn()
const key = set('lol')
set(ref, key)
return get(key)
})
.equal(ref),
test('the type of the key should be \'object\'')
.value(exports.superSafe)
.map(fn => fn().set() && typeof fn().set())
.equal('object'),
test('the object key should have no prototype')
.value(exports.superSafe)
.map(fn => fn().set().prototype)
.equal(undefined),
].concat($('arrow').map(def =>
test(`function line ${def.loc.start.line} column ${
def.loc.start.column} is a single expression`)
.value(def.body.type)
.notEqual('BlockStatement')))
.concat([
test('no variable declaration')
.value($('VariableDeclarator').length)
.equal(0, 'Variables declaration count should be 0'),
test(`Don't Repeat Yourself`)
.value($('ExpressionStatement Identifier'))
.map(nodes => nodes
.filter(n => !/function/i.test(n.parent.type))
.map(n => n.name)
.filter(name => name !== 'get' && Object.keys(exports).includes(name))
.length)
.equal(Object.keys(exports).length - 1, 'You have repeated keys'),
])),
]