mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
40 lines
799 B
JavaScript
40 lines
799 B
JavaScript
function LoopStatus(len) {
|
|
this.i = 0;
|
|
this.len = len;
|
|
}
|
|
|
|
LoopStatus.prototype = {
|
|
getLength: function() {
|
|
return this.len;
|
|
},
|
|
isLast: function() {
|
|
return this.i === this.len - 1;
|
|
},
|
|
isFirst: function() {
|
|
return this.i === 0;
|
|
},
|
|
getIndex: function() {
|
|
return this.i;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Internal helper method to handle loops with a status variable
|
|
* @private
|
|
*/
|
|
module.exports = function forEachStatusVariableHelper(array, callback) {
|
|
if (!array) {
|
|
return;
|
|
}
|
|
if (!array.forEach) {
|
|
array = [array];
|
|
}
|
|
|
|
var len = array.length;
|
|
var loopStatus = new LoopStatus(len);
|
|
|
|
for (; loopStatus.i < len; loopStatus.i++) {
|
|
var o = array[loopStatus.i];
|
|
callback(o, loopStatus);
|
|
}
|
|
}; |