marko/components/nextRepeatedId.js
2017-02-20 16:01:02 -07:00

16 lines
517 B
JavaScript

var REPEATED_ID_KEY = '$rep';
module.exports = function nextRepeatedId(out, parentId, id) {
var nextIdLookup = out.global[REPEATED_ID_KEY] || (out.global[REPEATED_ID_KEY] = {});
var indexLookupKey = parentId + '-' + id;
var currentIndex = nextIdLookup[indexLookupKey];
if (currentIndex == null) {
currentIndex = nextIdLookup[indexLookupKey] = 0;
} else {
currentIndex = ++nextIdLookup[indexLookupKey];
}
return indexLookupKey.slice(0, -2) + '[' + currentIndex + ']';
};