-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmap.js
More file actions
70 lines (64 loc) · 2.71 KB
/
map.js
File metadata and controls
70 lines (64 loc) · 2.71 KB
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
var expect = require('chai').expect;
var asyncChainable = require('../index');
describe('async-chainable.map() (forEach as an alias)', function() {
it('should correctly map a simple array', function(done) {
asyncChainable()
.map('output', [1, 2, 3, 4, 5], function(next, value) { setTimeout(function() { next(null, value * 2) }, 200 - (value * 10)) })
.end(function(err) {
expect(err).to.be.not.ok;
expect(this.output).to.be.deep.equal([2, 4, 6, 8, 10]);
done();
});
});
it('should correctly map a simple object', function(done) {
asyncChainable()
.map('output', {foo: 'foo', bar: 'bar', baz: 'baz'}, function(next, value, key) { next(null, value.toUpperCase() + '!', key.toUpperCase()) })
.end(function(err) {
expect(err).to.be.not.ok;
expect(this.output).to.be.deep.equal({FOO: 'FOO!', BAR: 'BAR!', BAZ: 'BAZ!'});
done();
});
});
it('should correctly map a late bound array', function(done) {
asyncChainable()
.set('myArray', [10, 9, 8, 7, 6])
.map('output', 'myArray', function(next, value) { setTimeout(function() { next(null, value - 2) }, value * 10) })
.end(function(err) {
expect(err).to.be.not.ok;
expect(this.myArray).to.be.deep.equal([10, 9, 8, 7, 6]);
expect(this.output).to.be.deep.equal([8, 7, 6, 5, 4]);
done();
});
});
it('should correctly map a late bound array (overwriting itself)', function(done) {
asyncChainable()
.set('myArray', [10, 9, 8, 7, 6])
.map('myArray', 'myArray', function(next, value) { setTimeout(function() { next(null, value - 2) }, value * 10) })
.end(function(err) {
expect(err).to.be.not.ok;
expect(this.myArray).to.be.deep.equal([8, 7, 6, 5, 4]);
done();
});
});
it('should correctly map a late bound object', function(done) {
asyncChainable()
.set('myObject', {foo: 'foo', quz: 'quz', floop: 'floop', fleem: 'fleem'})
.map('output', 'myObject', function(next, value, key) { next(null, value.toUpperCase() + '!', key.toUpperCase())} )
.end(function(err) {
expect(err).to.be.not.ok;
expect(this.myObject).to.be.deep.equal({foo: 'foo', quz: 'quz', floop: 'floop', fleem: 'fleem'});
expect(this.output).to.be.deep.equal({FOO: 'FOO!', QUZ: 'QUZ!', FLOOP: 'FLOOP!', FLEEM: 'FLEEM!'});
done();
});
});
it('should correctly map a late bound object (overwriting itself)', function(done) {
asyncChainable()
.set('myObject', {foo: 'foo', quz: 'quz', floop: 'floop', fleem: 'fleem'})
.map('myObject', 'myObject', function(next, value, key) { next(null, value.toUpperCase() + '!', key.toUpperCase())} )
.end(function(err) {
expect(err).to.be.not.ok;
expect(this.myObject).to.be.deep.equal({FOO: 'FOO!', QUZ: 'QUZ!', FLOOP: 'FLOOP!', FLEEM: 'FLEEM!'});
done();
});
});
});