A JavaScript interpreter for Lisp-like forms written in JSON.
import evaluateForm from "@tmanderson/json-forms";
// A data model
const data = { users: [{ name: "lindsay" }, { name: "tom" }] };
// A JSON Form run against the data model
const form = { $concat: { users: { $map: { $get: "name" } } } };
// The result of that form
evaluateForm(form, data); // => ['lindsay', 'tom']
Or, using JSON Forms' DSL and the compileAndEvaluate
method
import { compileAndEvaluate } from "@tmandersno/json-forms";
// Our data model
const data = { users: [{ name: "lindsay" }, { name: "tom" }] };
// Our form against the model
const form = `
$concat
users
$map
$get 'name'
`;
// The result of that form
compileAndEvaluate(form, data); // => ['lindsay', 'tom']
JSON Forms ships with a parser (grammar can be viewed here) that
reduces a bit of the JSON noise when writing forms. JSON Forms exports both compileToJSON
and compileAndExec
methods that directly consume a JSON Form writting in the DSL.
You can append any command with a !
(eg. $get!
) to enable debug output on
a particular query.
$concat
users
$map
$get 'name'
{
$concat: {
users: {
$map: {
$get: "name";
}
}
}
}
people
$and!
$has 'lastName'
$has 'firstName'
$concat
'lastName'
','
'firstName'
{
people: {
$debug: true, // note: the `!` after `$and` above will debug a statement
$and: [
{ $has: "lastName" },
{ $has: "firstName" },
{
$concat: ["lastName", ",", "firstName"]
}
];
}
}