/
index.js
91 lines (81 loc) · 2.17 KB
/
index.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
var each = require('..');
describe('each(arr, fn)', function(){
it('should iterate the values', function(){
var vals = [];
each([1,2,3], function(val){
vals.push(val);
});
vals.should.eql([1,2,3]);
})
it('should pass the index', function(){
var vals = [];
each([1,2,3], function(val, i){
vals.push(i);
});
vals.should.eql([0,1,2]);
})
describe('when passed a context', function(){
it('should iterate in context', function(){
var vals = [];
each([1,2,3], function(val, i){
this.push(val);
}, vals);
vals.should.eql([1,2,3]);
})
})
})
describe('each(obj, fn)', function(){
it('should iterate key / value pairs', function(){
var user = { name: 'Tobi', age: 2 };
var vals = [];
each(user, function(key, val){
vals.push([key, val]);
});
vals.should.eql([['name', 'Tobi'], ['age', 2]]);
})
describe('when .length is present', function(){
it('should iterate as if it were an array', function(){
var arr = { length: 2, 0: 'foo', 1: 'bar' };
var vals = [];
each(arr, function(val, i){
vals.push(val, i);
});
vals.should.eql(['foo', 0, 'bar', 1]);
})
})
describe('when passed a context', function(){
it('should iterate in context', function(){
var user = { name: 'Tobi', age: 2 };
var vals = [];
each(user, function(key, val){
this.push([key, val]);
}, vals);
vals.should.eql([['name', 'Tobi'], ['age', 2]]);
})
})
})
describe('each(str, fn)', function(){
it('should iterate characters', function(){
var vals = [];
each('hey', function(c, i){
vals.push(c, i);
});
vals.should.eql(['h', 0, 'e', 1, 'y', 2]);
})
describe('when passed a context', function(){
it('should iterate in context', function(){
var vals = [];
each('hey', function(c, i){
this.push(c, i);
}, vals);
vals.should.eql(['h', 0, 'e', 1, 'y', 2]);
})
})
})
describe('each(users, fn)', function(){
it('should use toFunction', function(){
var users = [{ name: 'john' }];
each(users, '.name = "baz"');
users[0].name.should.equal('baz');
})
})