-
Notifications
You must be signed in to change notification settings - Fork 86
/
mapToObject.spec.js
59 lines (50 loc) · 1.27 KB
/
mapToObject.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
import { mapToObject } from './mapToObject.js'
const list = [ 1, 2, 3 ]
const fn = x => x % 2 ? { [ x ] : x + 1 } : { [ x ] : x + 10 }
const expected = {
1 : 2,
2 : 12,
3 : 4,
}
test('happy', () => {
const result = mapToObject(fn, list)
expect(result).toEqual(expected)
})
test('curried', () => {
const result = mapToObject(fn)(list)
expect(result).toEqual(expected)
})
test('string.fn test', () => {
const list = [ 'auto', 'bar=false', 'foo', 'baz=1.5', 's=more', 'k=2' ]
const fn = x => {
const [ key, value ] = x.split('=')
if (value === undefined || value === 'true'){
return { [ key ] : true }
}
if (value === 'false'){
return { [ key ] : false }
}
if (Number.isNaN(Number(value))){
return { [ key ] : value }
}
return { [ key ] : Number(value) }
}
const expectedResult = {
auto : true,
foo : true,
bar : false,
baz : 1.5,
s : 'more',
k : 2,
}
const result = mapToObject(fn, list)
expect(result).toEqual(expectedResult)
})
test('bad path', () => {
expect(() => mapToObject(1, null)).toThrowErrorMatchingInlineSnapshot(`
"Failed R.ok -
reason: {"input":"Number","schema":"Function"}
all inputs: ["Number","Null"]
all schemas: ["Function","Array"]"
`)
})