@tmanderson/json-forms

0.1.0 • Public • Published

JSON Forms

Lisp-like forms written in JSON and evaluated with JavaScript

A JavaScript interpreter for Lisp-like forms written in JSON.

Example

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 DSL

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.

For example, this:

$concat
  users
    $map
      $get 'name'

Would convert to this:

{
  $concat: {
    users: {
      $map: {
        $get: "name";
      }
    }
  }
}

As another example, this:

people
  $and!
    $has 'lastName'
    $has 'firstName'
    $concat
      'lastName'
      ','
      'firstName'

Would convert to:

{
  people: {
    $debug: true, // note: the `!` after `$and` above will debug a statement
    $and: [
      { $has: "lastName" },
      { $has: "firstName" },
      {
        $concat: ["lastName", ",", "firstName"]
      }
    ];
  }
}

Readme

Keywords

none

Package Sidebar

Install

npm i @tmanderson/json-forms

Weekly Downloads

0

Version

0.1.0

License

ISC

Unpacked Size

365 kB

Total Files

54

Last publish

Collaborators

  • tmanderson