npm.io
7.11.5 • Published 2 years ago

@balena/jellyfish-jellyscript

Licence
Apache-2.0
Version
7.11.5
Deps
7
Size
35 kB
Vulns
0
Weekly
0
Stars
1
DeprecatedThis package is deprecated


npm version

Jellyscript

Write computed properties in JSON Schema using javascript expressions.
https://product-os.github.io/jellyfish-jellyscript



Installation

Install @balena/jellyfish-jellyscript by running:

npm install --save @balena/jellyfish-jellyscript

Usage

Jellyscript 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. Jellyscript supports all the functions provided by FormulaJS

Below is an example how to use this library:

import { Jellyscript } from '@balena/jellyfish-jellyscript';

const parser = new Jellyscript();

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, }