mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 12:50:38 -07:00
coverage
This commit is contained in:
+31
-57
@@ -4,7 +4,7 @@ const Promise = require('any-promise');
|
||||
var render = {
|
||||
|
||||
renderTemplates: function(templates, scope, opts) {
|
||||
if(!scope) throw new Error('unable to evalTemplates: scope undefined');
|
||||
if (!scope) throw new Error('unable to evalTemplates: scope undefined');
|
||||
opts = opts || {};
|
||||
opts.strict_filters = opts.strict_filters || false;
|
||||
|
||||
@@ -15,80 +15,54 @@ var render = {
|
||||
// emptyPromise.then(renderTag(template0).then(renderTag(template1).then(renderTag(template2)...
|
||||
var lastPromise = templates.reduce((promise, template) => {
|
||||
return promise.then(() => {
|
||||
if (scope.safeGet('forloop.skip')) {
|
||||
return Promise.resolve('');
|
||||
}
|
||||
if (scope.safeGet('forloop.stop')) {
|
||||
throw new Error('forloop.stop'); // this will stop/break the sequential promise chain and go to the catch
|
||||
}
|
||||
var promiseLink = Promise.resolve('');
|
||||
switch (template.type) {
|
||||
case 'tag':
|
||||
// Add Promises to the chain
|
||||
promiseLink = this.renderTag(template, scope, this.register)
|
||||
.then((partial) => {
|
||||
if (partial === undefined) {
|
||||
return true; // basically a noop (do nothing)
|
||||
}
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'html':
|
||||
promiseLink = Promise.resolve(template.value)
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'output':
|
||||
var val = this.evalOutput(template, scope, opts);
|
||||
promiseLink = Promise.resolve(val === undefined ? '' : stringify(val))
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
var promiseLink = Promise.resolve('');
|
||||
switch (template.type) {
|
||||
case 'tag':
|
||||
// Add Promises to the chain
|
||||
promiseLink = this.renderTag(template, scope, this.register)
|
||||
.then((partial) => {
|
||||
if (partial === undefined) {
|
||||
return true; // basically a noop (do nothing)
|
||||
}
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'html':
|
||||
promiseLink = Promise.resolve(template.value)
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
case 'output':
|
||||
var val = this.evalOutput(template, scope, opts);
|
||||
promiseLink = Promise.resolve(val === undefined ? '' : stringify(val))
|
||||
.then((partial) => {
|
||||
return html += partial;
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
return promiseLink;
|
||||
})
|
||||
.catch((error) => {
|
||||
if (error.message === 'forloop.skip') {
|
||||
// the error is a controlled, purposeful stop. so just return the html that we have up to this point
|
||||
return html;
|
||||
} else {
|
||||
// rethrow actual error
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
return promiseLink;
|
||||
});
|
||||
}, Promise.resolve('')); // start the reduce chain with a resolved Promise. After first run, the "promise" argument
|
||||
// in our reduce callback will be the returned promise from our "then" above. In this
|
||||
// case, that's the promise returned from this.renderTag or a resolved promise with raw html.
|
||||
|
||||
return lastPromise
|
||||
.then((renderedHtml) => {
|
||||
return renderedHtml;
|
||||
})
|
||||
.catch((error) => {
|
||||
throw error;
|
||||
});
|
||||
|
||||
return lastPromise;
|
||||
},
|
||||
|
||||
renderTag: function(template, scope, register) {
|
||||
if (template.name === 'continue') {
|
||||
scope.set('forloop.skip', true);
|
||||
return Promise.resolve('');
|
||||
}
|
||||
if (template.name === 'break') {
|
||||
scope.set('forloop.stop', true);
|
||||
scope.set('forloop.skip', true);
|
||||
return Promise.reject(new Error('forloop.stop')); // this will stop the sequential promise chain
|
||||
}
|
||||
return template.render(scope, register);
|
||||
},
|
||||
|
||||
evalOutput: function(template, scope, opts) {
|
||||
if(!scope) throw new Error('unable to evalOutput: scope undefined');
|
||||
if (!scope) throw new Error('unable to evalOutput: scope undefined');
|
||||
var val = Syntax.evalExp(template.initial, scope);
|
||||
template.filters.some(filter => {
|
||||
if (filter.error) {
|
||||
|
||||
Reference in New Issue
Block a user