-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
193 lines (169 loc) · 5.2 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
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
var test = require('tape')
var reduce = require('universal-reduce')
var pc = require('./index')
var done = require('./done')
function incKey (next) {
var inc = 0
return function (acc, value, key) {
if (done(acc, value, key)) {
return next(acc)
}
inc += 1
return next(acc, value, 'a' + inc)
}
}
function pair (next) {
var isFirst = true
var first
return function (acc, value, key) {
if (reduce.isReduced(acc)) {
return next.apply(null, arguments)
}
if (done(acc, value, key)) {
if (!isFirst) {
acc = next(acc, [first, value], key)
}
return next(acc)
}
if (!isFirst) {
isFirst = true
return next(acc, [first, value], key)
} else {
isFirst = false
first = value
return acc
}
}
}
test('iterate-all', function (t) {
var a = pc([1, 2, 3], pc.map(function (i) { return i }))
t.equal(a[1], 2, 'map identity over array')
var b = pc([1, 2, 3])
t.equal(b[0], 1, 'map identity over array by default')
t.deepEqual(pc([1, 2, 3, 4], pair), [[1, 2], [3, 4]], 'even pair')
// also tests single item compose
t.deepEqual(pc([1, 2, 3], pc.comp(pair)), [[1, 2], [3, undefined]], 'uneven pair')
function all (next) {
return function (acc, val, key) {
if (done(acc, val, key)) {
return next(acc)
}
if (!val) return reduce.reduced(false)
return next(acc, val, key)
}
}
t.equal(pc([true, false, true], all, true), false, 'early reduce')
var symbols = {}
var aaa = Symbol('aaa')
symbols[aaa] = 'a'
symbols[Symbol('bbb')] = 'b'
symbols[Symbol('ccc')] = 'c'
symbols.foo = 'bar'
symbols[1] = 'baz'
t.deepEqual(pc(symbols)[aaa], 'a', 'Copies symbol')
t.deepEqual(pc(symbols, pc.filter(function (v, k) { return typeof k === 'symbol' }), []),
['a', 'b', 'c'], 'filters by key type')
t.deepEqual(pc(symbols, pc.identity, []), ['baz', 'bar', 'a', 'b', 'c'], 'in order')
var obj = {a: 1, b: 2}
// also tests single item compose
var c = pc(obj, pc.comp(pc.map(function (v, k) { return k + v })))
t.equal(c.b, 'b2', 'modify object while copying')
obj.b = false
t.equal(c.b, 'b2', 'confirm copy')
var deep = new Set()
deep.add([1, 2, 3])
var tmp = new Map()
tmp.set(1, 'a')
tmp.set(2, 'b')
tmp.set(3, 'c')
deep.add(tmp)
function notA4 (value, key) {
return key !== 'a4'
}
var flat = pc(deep, pc.comp(pc.cat, incKey, pc.filter(notA4)), {})
t.deepEqual(flat, {a1: 1, a2: 2, a3: 3, a5: 'b', a6: 'c'}, 'weeeee')
t.deepEqual(pc(flat, pc.keyMap(() => 'a'), {}, pc.byKey),
{a: [1, 2, 3, 'b', 'c']},
'Partition by key'
)
tmp.set(4, [ 'd' ])
deep.add('e')
t.deepEqual(pc(deep, pc.steamroll, []), [ 1, 2, 3, 'a', 'b', 'c', 'd', 'e' ], 'fully steamrolled')
;(function () {
t.equal(pc(arguments)[2], 3, 'arguments object')
t.ok(Array.isArray(pc(arguments, pc.identity, [])), 'arguments to array')
}(1, 2, 3))
var d = { foo: 'bar' }
d.constructor = null
t.equal(pc(d).foo, 'bar', 'no constructor')
var add = function (a, b) { return a + b }
add.foo = 'bar'
function wrap (fn) { return function () { return fn.apply(this, arguments) } }
var wrappedAdd = pc(add, null, wrap(add))
t.notEqual(add, wrappedAdd, 'wrapped function not the same ref')
t.equal(add.foo, wrappedAdd.foo, 'props transfered')
t.equal(add(1, 2), wrappedAdd(1, 2), 'work the same')
var take1 = pc.take(1)
t.deepEqual(pc([1, 2, 3], take1), [ 1 ], 'take')
t.deepEqual(pc(tmp, take1, {}), { 1: 'a' }, 'take obj')
var skip2 = pc.skip(2)
t.deepEqual(pc([1, 2, 3], skip2), [ 3 ], 'skip')
t.deepEqual(pc(tmp, skip2, {}), { 3: 'c', 4: [ 'd' ] }, 'skip obj')
var numSort = pc.sort(function (a, b) {
return a - b
})
t.deepEqual(pc(new Set([3, 2, 5, 1, 4]), numSort, []), [1, 2, 3, 4, 5], 'numeric sort')
t.deepEqual(pc([1, 2, 3, 2, 1, 2, 2, 2, 4, 1, 2, 2], pc.comp(
numSort,
numSort,
numSort,
numSort,
pc.filter(function (val) { return val === 2 }),
pc.map(function (val, key) { return +key })
)), [ 1, 3, 5, 6, 7, 10, 11 ], 'really stable')
t.deepEqual(
pc(
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
pc.cond(
(val, key) => !(val % 3 === 0 || key === '9'),
pc.identity,
pc.filter(function () { return false })
)
),
[1, 2, 4, 5, 7, 8],
'conditional transform'
)
t.deepEqual(
pc(
[1, 3, 1, 8, 2, 2, 4, 1, 6], pc.comp(
pc.cond(
(val, key) => val % 2 === 0,
numSort,
pc.comp(pc.skip(1), pair)
),
function (next) {
return function (acc, val, key) {
if (reduce.isReduced(acc)) {
return next.apply(null, arguments)
}
if (done(acc, val, key)) {
return next(acc, 'doh', 'only_once')
}
return next(acc, val, key)
}
}
)),
[[3, 1], 2, 2, 4, 6, 8, [1, undefined], 'doh'],
'Stateful transforms in both branches of cond.'
)
var j = 30000
var lots = new Array(j)
var lotsQ = new Array(j)
for (var i = 0; i < j; i++) {
lotsQ[i] = lots[i] = Math.random() * j
}
lotsQ.sort(function (a, b) { return a - b })
lots = pc(lots, pc.comp(numSort, numSort), [])
t.same(lots, lotsQ, 'Sorts the same as native sort')
t.end()
})