patterns | , walk tree | select tree | Search

This code appears to be a function that evaluates templates or expressions and returns an array of results, which can be either plain results or objects with an evaluate method, depending on the input settings. The function takes an array of strings or an object with settings as input and returns an array of results based on the provided settings.

Cell 1


/*

input:
[
'Item > * > Expression',
]

outut: an array of results

input:
[
'Item > * > Expression',
{
    title: '@title'
}
]

output: an array of results assign to an object with evaluate called on each
result using the result as the new context for the object evaluation

*/

What the code could have been:

/**
 * @typedef {Object} ASTNode
 * @property {string} type
 * @property {string} value
 * @property {ASTNode[]} children
 */

/**
 * @typedef {Object} NodeResult
 * @property {any} evaluatedExpression
 * @property {Object} evaluatedAST
 */

/**
 * Parse a string into an abstract syntax tree (AST) node.
 *
 * @param {string} str - The string to parse.
 * @returns {ASTNode} The parsed AST node.
 */
function parseAST(str) {
    // For simplicity, assume the string is already parsed into an AST node
    return { type: 'Item', value: str };
}

/**
 * Evaluate an AST node.
 *
 * @param {ASTNode} node - The AST node to evaluate.
 * @returns {any} The result of evaluating the AST node.
 */
function evaluateNode(node) {
    // For simplicity, assume the node's value is a simple expression
    return eval(node.value);
}

/**
 * Evaluate an AST node with the given context.
 *
 * @param {ASTNode} node - The AST node to evaluate.
 * @param {Object} context - The context in which to evaluate the node.
 * @returns {any} The result of evaluating the AST node with the given context.
 */
function evaluateNodeWithContext(node, context) {
    return evaluateNode(node);
}

/**
 * Evaluate a list of AST nodes.
 *
 * @param {ASTNode[]} nodes - The list of AST nodes to evaluate.
 * @returns {any[]} The results of evaluating the AST nodes.
 */
function evaluateNodes(nodes) {
    return nodes.map((node) => evaluateNode(node));
}

/**
 * Evaluate a list of AST nodes with the given context.
 *
 * @param {ASTNode[]} nodes - The list of AST nodes to evaluate.
 * @param {Object} context - The context in which to evaluate the nodes.
 * @returns {NodeResult[]} The results of evaluating the AST nodes with the given context.
 */
function evaluateNodesWithContext(nodes, context) {
    return nodes.map((node) => ({
        evaluatedExpression: evaluateNodeWithContext(node, context),
        evaluatedAST: node,
    }));
}

/**
 * Main function to evaluate a list of strings.
 *
 * @param {string[]} strings - The list of strings to evaluate.
 * @param {Object} [options] - Optional configuration.
 * @param {Object} [options.context] - The context in which to evaluate the expressions.
 * @returns {any[]} The results of evaluating the expressions.
 */
function evaluateExpressions(strings, options = {}) {
    const context = options.context || {};
    const nodes = strings.map((str) => parseAST(str));
    return evaluateNodesWithContext(nodes, context);
}

// Example usage:
const strings = ['Item > * > Expression'];
const results = evaluateExpressions(strings);

// With context:
const stringsWithContext = ['Item > * > Expression', { title: '@title' }];
const resultsWithContext = evaluateExpressions(stringsWithContext);

// Use the results with the context
resultsWithContext.forEach((result) => {
    const evaluatedExpression = evaluateNodeWithContext(result.evaluatedAST, result.evaluatedAST.value);
    //... do something with the result...
});

Code Breakdown

Functionality

This code appears to be a function or module that takes one or more inputs:

The function returns an array of results, which can be either plain results or objects with an evaluate method called on each result.

Input Parameters

Output

Example Inputs and Outputs