-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathreduce.js
96 lines (84 loc) · 2.59 KB
/
reduce.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
var async = require('../lib');
var {expect} = require('chai');
var assert = require('assert');
describe('reduce', () => {
it('reduce', (done) => {
var call_order = [];
async.reduce([1,2,3], 0, (a, x, callback) => {
call_order.push(x);
callback(null, a + x);
}, (err, result) => {
assert(err === null, err + " passed instead of 'null'");
expect(result).to.equal(6);
expect(call_order).to.eql([1,2,3]);
done();
});
});
it('reduce async with non-reference memo', (done) => {
async.reduce([1,3,2], 0, (a, x, callback) => {
setTimeout(() => {callback(null, a + x);}, Math.random()*100);
}, (err, result) => {
expect(result).to.equal(6);
done();
});
});
it('reduce error', (done) => {
async.reduce([1,2,3], 0, (a, x, callback) => {
callback('error');
}, (err) => {
expect(err).to.equal('error');
});
setTimeout(done, 50);
});
it('reduce canceled', (done) => {
var call_order = [];
async.reduce([1,2,3], 0, (a, x, callback) => {
call_order.push(x);
callback(x === 2 ? false : null, a + x)
}, () => {
throw new Error('should not get here');
});
setTimeout(() => {
expect(call_order).to.eql([1, 2]);
done();
}, 25);
});
it('inject alias', (done) => {
expect(async.inject).to.equal(async.reduce);
done();
});
it('foldl alias', (done) => {
expect(async.foldl).to.equal(async.reduce);
done();
});
it('reduceRight', (done) => {
var call_order = [];
var arr = [1,2,3];
async.reduceRight(arr, 0, (a, x, callback) => {
call_order.push(x);
callback(null, a + x);
}, (err, result) => {
expect(result).to.equal(6);
expect(call_order).to.eql([3,2,1]);
expect(arr).to.eql([1,2,3]);
done();
});
});
it('reduceRight canceled', (done) => {
var call_order = [];
async.reduceRight([1,2,3], 0, (a, x, callback) => {
call_order.push(x);
callback(x === 2 ? false : null, a + x)
}, () => {
throw new Error('should not get here');
});
setTimeout(() => {
expect(call_order).to.eql([3, 2]);
done();
}, 25);
});
it('foldr alias', (done) => {
expect(async.foldr).to.equal(async.reduceRight);
done();
});
});