43 lines
942 B
JavaScript
43 lines
942 B
JavaScript
'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
|
|
}
|
|
}
|
|
}
|