Skip to content

Lisp-like forms written as JSON and evaluated with JavaScript

Notifications You must be signed in to change notification settings

tmanderson/json-forms

Repository files navigation

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"]
      }
    ];
  }
}

About

Lisp-like forms written as JSON and evaluated with JavaScript

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published