silphius/app/astride/evaluators/array.js
2024-06-30 14:29:10 -05:00

42 lines
1.1 KiB
JavaScript

export default function(node, {evaluate, scope}) {
const elements = [];
const asyncSpread = Object.create(null);
let isAsync = false;
for (const index in node.elements) {
const element = node.elements[index];
if ('SpreadElement' === element.type) {
const {async, value} = evaluate(element.argument, {scope});
isAsync = isAsync || async;
if (async) {
elements.push(value);
asyncSpread[elements.length - 1] = true;
}
else {
elements.push(...value);
}
}
else {
const {async, value} = evaluate(element, {scope});
isAsync = isAsync || async;
elements.push(value);
}
}
return {
async: !!isAsync,
value: !isAsync
? elements
: Promise.all(elements).then((elementsAndOrSpreads) => {
const elements = [];
for (let i = 0; i < elementsAndOrSpreads.length; ++i) {
if (asyncSpread[i]) {
elements.push(...elementsAndOrSpreads[i]);
}
else {
elements.push(elementsAndOrSpreads[i]);
}
}
return elements;
}),
};
}