-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
134 lines (118 loc) · 3.47 KB
/
test.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
'use strict'
/* global it, describe */
const assert = require('assert')
const fpArray = require('../lib/index')
const filter = fpArray.filter
const map = fpArray.map
const reduce = fpArray.reduce
describe('fp-array', function () {
describe('#filter()', function () {
it('should be defined', function () {
const actual = typeof filter
const expected = 'function'
assert.deepEqual(actual, expected)
})
it('should return an object', function () {
const actual = typeof filter([])
const expected = 'object'
assert.deepEqual(actual, expected)
})
it('should filter with one function', function () {
const arr = [0, 5, null]
const actual = filter(arr)
.is(Boolean)
.result()
const expected = [5]
assert.deepEqual(actual, expected)
})
it('should not mutate the original arr', function () {
const actual = [0, 5, null]
const boolean = n => typeof n === 'boolean'
filter(actual)
.is(boolean)
.result()
const expected = [0, 5, null]
assert.deepEqual(actual, expected)
})
it('should filter with multiple functions', function () {
const arr = [1, 'a', true, 6, 0, 4, undefined, 10]
// filters functions
const number = n => typeof n === 'number'
const greaterThan4 = n => n > 4
const even = n => n % 2 === 0
const actual = filter(arr)
.is(number)
.and(greaterThan4)
.and(even)
.result()
const expected = [6, 10]
assert.deepEqual(actual, expected)
})
})
describe('#map()', function () {
it('should be defined', function () {
const actual = typeof map
const expected = 'function'
assert.deepEqual(actual, expected)
})
it('should return an object', function () {
const actual = typeof map([])
const expected = 'object'
assert.deepEqual(actual, expected)
})
it('should map with one function', function () {
const arr = [0, true, null]
const actual = map(arr)
.with(Boolean)
.result()
const expected = [false, true, false]
assert.deepEqual(actual, expected)
})
it('should not mutate the original arr', function () {
const actual = [0, true, null]
map(actual)
.with(Boolean)
.result()
const expected = [0, true, null]
assert.deepEqual(actual, expected)
})
it('should map with multiple functions', function () {
const arr = [1, 2, 3]
const increment = n => n + 1
const double = n => n * 2
const actual = map(arr)
.with(increment)
.and(double)
.and(String)
.result()
const expected = ['4', '6', '8']
assert.deepStrictEqual(actual, expected)
})
})
describe('#reduce()', function () {
it('should be defined', function () {
const actual = typeof reduce
const expected = 'function'
assert.deepEqual(actual, expected)
})
it('should work without initial value', function () {
const arr = [1, 2, 3]
const actual = reduce(
arr,
(acc, currentValue) => acc + currentValue
)
const expected = 6
assert.strictEqual(actual, expected)
})
it('should allow an initial value', function () {
const arr = [1, 2, 3]
const actual = reduce(
arr,
(acc, currentValue) => acc + currentValue,
5
)
const expected = 11
assert.strictEqual(actual, expected)
})
})
})