Skip to content

Export LiquidExpression class or evalExpression to evaluate single expressions #527

@Prinzhorn

Description

@Prinzhorn

First of all thanks for creating and maintaining LiquidJS. I needed a white space sensitive generic templating language that is not tied to HTML and LiquidJS does exactly that.

Now I have a requirement that I think this library could very easily fulfill as well. You are probably aware how GitHub actions allows using templating and conditions inside YAML. E.g.

if: github.repository == 'octo-org/octo-repo-prod'

or

if: ${{ github.event_name == 'pull_request' && github.event.action == 'unassigned' }}

I have a very similar requirement (not sure yet if it will use YAML as well). Since I am already using LiquidJS I would love to use the same semantics here as well. But Instead of rendering a template and getting a string as a result I would need LiquidJS to evaluate a single expression and return the value as is (boolean, number, etc.). It would look like this:

import { LiquidExpression } from 'liquidjs';

const expressionEngine = new LiquidExpression();
const expression = expressionEngine.parse('a > b');
const result = expressionEngine.evaluate(expression, {a: 1, b: 2});

// In this specific case the expression yielded a boolean, but it could be any primitive.
assert(result === false);

I've been poking around the docs and code and I think what I want is a more ergonomic way of doing this?

const ctx = new Context({})
const trie = createTrie(defaultOperators)
const create = (str: string) => new Tokenizer(str, trie).readExpression()

Maybe add evalExpression analogous to evalValue?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions