-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
68 lines (55 loc) · 1.57 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
var reduce = require('./index.js')
var test = require('tape')
test('reduce', function (t) {
// sum some things, no initial value.
sum([1,2,3])
sum(new Map([['0', 1], ['1', 2], ['2', 3]]))
sum(new Set([1, 2, 3]))
sum({'0': 1, '1': 2, '2': 3})
var complex = new ComplexObject()
sum(complex)
// generators get exausted between the tests
t.same(reduce(foo(), function (acc, val) {
return acc + val
}, 0), 6, 'sum generator')
t.same(reduce(foo(), function (acc, val, key) {
if (key === '2') return reduce.reduced(acc)
return acc + val
}, 0), 3, 'early reduced generator')
t.ok(reduce.isReduced(reduce._reduce([ 1, 2], reduce.reduced, 4)), "doesn't unwrap")
t.equal(reduce._reduce([ 1, 2], reduce.reduced, 4)['@@transducer/value'], 4, 'manual unwraped')
;(function () {
sum(arguments)
}(1,2,3))
t.end()
function sum (stuff) {
t.same(reduce(stuff, function (acc, val) {
return acc + val
}, 0), 6, 'sum ' + stuff.constructor.name)
t.same(reduce(stuff, function (acc, val, key) {
if (key === '2') return reduce.reduced(acc)
return acc + val
}, 0), 3, 'early reduced ' + stuff.constructor.name)
}
})
function* foo () {
var count = 0
while (count++ < 3) {
yield count
}
}
function ComplexObject () {
this[0] = 3
this[2] = 0
this[Symbol('foo')] = 3
Object.defineProperty(this, Symbol('baz'), {
value: 1000,
enumerable: false
})
Object.defineProperty(this, 'baz', {
value: 1000,
enumerable: false
})
}
ComplexObject.prototype[Symbol('bar')] = 100
ComplexObject.prototype.bar = 100