UNPKG

@1771technologies/lytenyte-pro

Version:

Blazingly fast headless React data grid with 100s of features.

71 lines (70 loc) 2.55 kB
import { current, advance } from "../parser/parser-context.js"; import { parseArrayLiteral } from "../parser/parse-array-literal.js"; import { parseObjectLiteral } from "../parser/parse-object-literal.js"; import { parseExpression } from "../parser/parse-expression.js"; export const collectionsPlugin = { name: "collections", parsePrefix: (ctx) => { const tok = current(ctx); // Array literal: [elements] if (tok.type === "Punctuation" && tok.value === "[") { return parseArrayLiteral(ctx); } // Object literal: { props } if (tok.type === "Punctuation" && tok.value === "{") { return parseObjectLiteral(ctx); } // Spread element: ...expr if (tok.type === "Spread") { advance(ctx); const argument = parseExpression(ctx, 0); return { type: "SpreadElement", argument, start: tok.start, end: argument.end }; } return null; }, optimize: (node, opt) => { if (node.type === "ArrayLiteral") { return { ...node, elements: node.elements.map((e) => opt(e)) }; } if (node.type === "ObjectLiteral") { return { ...node, properties: node.properties.map((p) => ({ ...p, key: p.computed ? opt(p.key) : p.key, value: opt(p.value), })), }; } return null; }, evaluate: (node, context, evalFn) => { if (node.type === "ArrayLiteral") { const result = []; for (const element of node.elements) { if (element.type === "SpreadElement") { const spread = evalFn(element.argument, context); result.push(...spread); } else { result.push(evalFn(element, context)); } } return { value: result }; } if (node.type === "ObjectLiteral") { const result = {}; for (const prop of node.properties) { const key = prop.computed ? evalFn(prop.key, context) : prop.key.type === "Identifier" ? prop.key.name : prop.key.value; const value = evalFn(prop.value, context); result[key] = value; } return { value: result }; } return null; }, };