marko/runtime/helper-forEachWithStatusVar.js
2017-01-02 15:53:38 -07:00

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);
}
};