-
Notifications
You must be signed in to change notification settings - Fork 8
/
forEach.js
42 lines (36 loc) · 1.05 KB
/
forEach.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
module.exports = forEach
var vec = require('./create')()
/**
* Perform some operation over an array of vec2s.
*
* @param {Array} a the array of vectors to iterate over
* @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
* @param {Number} offset Number of elements to skip at the beginning of the array
* @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
* @param {Function} fn Function to call for each vector in the array
* @param {Object} [arg] additional argument to pass to fn
* @returns {Array} a
* @function
*/
function forEach(a, stride, offset, count, fn, arg) {
var i, l
if(!stride) {
stride = 2
}
if(!offset) {
offset = 0
}
if(count) {
l = Math.min((count * stride) + offset, a.length)
} else {
l = a.length
}
for(i = offset; i < l; i += stride) {
vec[0] = a[i]
vec[1] = a[i+1]
fn(vec, vec, arg)
a[i] = vec[0]
a[i+1] = vec[1]
}
return a
}