This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
each.js
72 lines (58 loc) · 1.7 KB
/
each.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
var expect = require('must')
, each = require('../lib/each')
, src = require('../lib/src')
, seq = require('../lib/seq')
describe('each', function() {
describe('when given a seq', function() {
it('should call `fn` once for each item', function() {
var l = [1, 2, 3]
, s = seq(l)
var i = 0
var fn = function(x) {
expect(x).to.equal(l[i++])
}
each(s, fn)
expect(i).to.equal(l.length)
})
})
each(
[ [1, 2, 3, 4, 5]
, [[1, 2], [4, 5]]
]
, function(test) {
describe('when given `' + src(test).slice(1, -1) + '`', function() {
describe('and the function `fn`', function() {
var i = 0
var fn = function(x) {
expect(x).to.equal(test[i++])
}
var times = test.length + ' time' + (test.length > 1? 's' : '')
it('should call `fn` ' + times +'; once for each item', function() {
each(test, fn)
expect(i).to.equal(test.length)
})
})
})
}
)
var obj = { foo: 1, bar: true, baz: 'wibble' }
describe('when given `' + src(obj) + '`', function() {
describe('and the function `fn`', function() {
var prop = [['foo', 1], ['bar', true], ['baz', 'wibble']]
var i = 0
var fn = function(x) {
expect(x).to.eql(prop[i++])
}
it('should call `fn` 3 times; once for each enumerable property', function() {
each(obj, fn)
expect(i).to.equal(3)
})
})
})
describe('when given an empty sequence', function() {
it('should do nothing', function() {
var fn = function(x) { throw new Error("shouldn't get here!") }
each(null, fn)
})
})
})