cream-and-sugar
Version:
A deliciously functional syntax for JavaScript with native support for JSX
31 lines (29 loc) • 826 B
JavaScript
import { compile, nodes } from '../utils';
/*
* Turn comprehensions into functions as needed.
*/
compile(nodes.CompNode, function () {
const action = this.action.compile(true);
const params = `(${this.params.map(item => item.compile(true)).join(', ')})`;
const list = this.list.compile(true);
const caveat = this.caveat ? this.caveat.compile(true) : null;
if (!caveat) {
return `${list}.map(function ${params} {
return ${action};
}.bind(this))
`.replace(/\s+$/, '')
.replace(/\s+/g, ' ');
} else {
return `
(function () {
const acc_ = [];
${list}.forEach(function ${params} {
if (${caveat}) {
acc_.push(${action});
}
}.bind(this));
return acc_;
}.bind(this)())
`.replace(/^\s+|\s+$/g, '');
}
});