-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathloop.js
More file actions
53 lines (45 loc) · 937 Bytes
/
loop.js
File metadata and controls
53 lines (45 loc) · 937 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
43
44
45
46
47
48
49
50
51
52
53
// loop.js
// author jensdpersson@gmail.com
function loop(){
var obj = null;
var defaultResult = null;
var callback = null;
if(arguments.length == 3){
callback = arguments[2];
defaultResult = arguments[1];
obj = arguments[0];
} else if(arguments.length == 2){
callback = arguments[1];
defaultResult = arguments[0];
obj = this;
} else if(arguments.length == 1){
callback = arguments[0];
defaultResult = null;
obj = this;
}
if(!obj.length && obj.length != 0){
throw 'Cannot loop over ' + obj;
}
var leng = obj.length;
var index = 0;
var lo = {
result: defaultResult,
stop: function(){
index = leng;
},
index: function(){
return index;
},
isLast: function(){
return index >= leng;
}
};
while(index < leng){
lo.item = obj[index++];
callback(lo);
}
return lo.result;
};
if(exports){
exports.loop = loop;
}