Add eachUpTo function (#690)

This commit is contained in:
toasted-nutbread 2020-07-26 16:50:56 -04:00 committed by GitHub
parent 2ed2b22d49
commit e153971cd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -69,7 +69,8 @@ class TemplateRenderer {
['sanitizeCssClass', this._sanitizeCssClass.bind(this)], ['sanitizeCssClass', this._sanitizeCssClass.bind(this)],
['regexReplace', this._regexReplace.bind(this)], ['regexReplace', this._regexReplace.bind(this)],
['regexMatch', this._regexMatch.bind(this)], ['regexMatch', this._regexMatch.bind(this)],
['mergeTags', this._mergeTags.bind(this)] ['mergeTags', this._mergeTags.bind(this)],
['eachUpTo', this._eachUpTo.bind(this)]
]; ];
for (const [name, helper] of helpers) { for (const [name, helper] of helpers) {
@ -206,4 +207,21 @@ class TemplateRenderer {
return [...tags].join(', '); return [...tags].join(', ');
} }
_eachUpTo(context, iterable, maxCount, options) {
if (iterable) {
const results = [];
let any = false;
for (const entry of iterable) {
any = true;
if (results.length >= maxCount) { break; }
const processedEntry = options.fn(entry);
results.push(processedEntry);
}
if (any) {
return results.join('');
}
}
return options.inverse(context);
}
} }