fc_json
v1.1.2
Published
This README would normally document whatever steps are necessary to get your application up and running.
Downloads
10
Maintainers
Readme
fc_json version 1.1.2
A lightweight TypeScript library that allows adding function calls to a JSON object.
These function calls are created using the functionCall
builder, see example bellow.
Objects with functionCall members can be serialized and parsed using the standard JSON functionality.
This library provide and extension of the eval function evaluate
, that receive an extended JSON object
with one or more function implementation and evaluate this function call.
Installation
npm install fc_json
Usage
A simple example of how you can use fc_json
import { functionCall, evaluate} from 'fc_json';
const obj = {
q: "The meaning of Life?",
a: functionCall("computeTheMeaingOfLife(2, 21)")
};
// Parsing and serializing a function call using JSON standard functionality.
// obj and objCopy have the same content.
const objCopy = JSON.parse(JSON.stringify(obj));
// Evaluating the object, using a function implementation.
function computeTheMeaingOfLife(x: number, y: number) {
return x * y;
};
// Outputs "{ q: 'The meaning of Life?', a: 42 }"
console.log(evaluate(objCopy, [computeTheMeaingOfLife]));
Example with two nested function calls
import { functionCall, evaluate} from 'fc_json';
const obj = {
q: "The meaning of Life?",
a: functionCall("computeTheMeaingOfLife(2, multiplyby3(7))")
};
function computeTheMeaingOfLife(x: number, y: number) {
return x * y;
};
function multiplyby3(x: number) {
return 3 * x;
};
// Outputs "{ q: 'The meaning of Life?', a: 42 }"
console.log(evaluate(obj, [computeTheMeaingOfLife, multiplyby3]));
Contribution guidelines
If you plan to enhance the library, make sure you add test cases and all the previous tests are passing.
You can test the library with:
npm test
Support
- Questions / suggestions / ideas and code contributions are welcome at [email protected]