Skip to content

Add constants #38

@cshaa

Description

@cshaa

Sometimes, it is necessary to provide various constants to the user. Right now, the only way to achieve this is to modify the data, which is rather unfortunate – it would be much better to specify the constants only once at compile time. Example usage:

const options = {
  constants: { pi: Math.PI }
}
const fn = compileExpression(`2 * pi * radius`, options)

fn({ radius: 1/2 })
// → Math.PI

It should be possible to access data fields shadowed by constants using single-quoted symbols:

const options = { constants: { a: "a_const " } }
const data = { a: "a_data ", b: "b_data " }

// single-quotes give access to data
const expr = `'a' + a + 'b' + b`

compileExpression(expr, options)(data)
// → "a_data a_const b_data b_data"

In order to keep the API simple, constants will bypass a customProp function – if a developer wants total control over the identifiers, they shouldn't define any constants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestimplementedThis feature is implemented in `develop` and ready for testing

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions