-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
42 lines (32 loc) · 942 Bytes
/
index.js
File metadata and controls
42 lines (32 loc) · 942 Bytes
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
'use strict'
module.exports = iterate
var own = {}.hasOwnProperty
function iterate(values, callback, context) {
var index = -1
var result
if (!values) {
throw new Error('Iterate requires that |this| not be ' + values)
}
if (!own.call(values, 'length')) {
throw new Error('Iterate requires that |this| has a `length`')
}
if (typeof callback !== 'function') {
throw new Error('`callback` must be a function')
}
// The length might change, so we do not cache it.
while (++index < values.length) {
// Skip missing values.
if (!(index in values)) {
continue
}
result = callback.call(context, values[index], index, values)
// If `callback` returns a `number`, move `index` over to `number`.
if (typeof result === 'number') {
// Make sure that negative numbers do not break the loop.
if (result < 0) {
index = 0
}
index = result - 1
}
}
}