0.0.1 • Published 1 year ago

@cascadeos/dynamic-dynscript v0.0.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
1 year ago

Dynscript

Write computed properties in JSON Schema using JS expressions.

npm version

Installation

Install @cascadeos/dynamic-dynscript by running:

pnpm i -D @cascadeos/dynamic-dynscript

Usage

Dynscript looks for the keyword $$formula in your schema and evaluates the expression it finds there. The full object data being evaluated is provided to the script execution scope as contract and the current field being evaluated is provided as input. A $$formula field must be an expression, and aliases and variables are not supported. Dynscript supports all the functions provided by FormulaJS

Below is an example how to use this library:

import { Dynscript } from '@cascadeos/dynamic-dynscript';

const parser = new Dynscript();

const schema = {
    type: 'object',
    properties: {
        number: {
            type: 'string',
            $$formula: 'SUM(contract.input, 10)',
        },
        lucky: {
            type: 'boolean',
            $$formula: 'contract.number === 13 ? true: false',
        },
        input: {
            type: 'number',
        },
    },
};

const data = {
    input: 3,
};

const result = parser.evaluateObject(schema, data);

console.log(result); // --> { lucky: true, number: 13, input: 3, }