jsof-parser
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

JSOF Parser

Snippet to easily parse JSON + FUNCTIONS into simple JSON ✌️💜.

Installation

Use the package manager npm to install JSOF.

npm i jsof-parser

Usage

We can start by converting a functional JSON to standard JSON

import JSOF from "jsof-parser";

const code = `{
  "label": "My functional json 😍",
  "animals": concat(
    getAnimal(types.unicorn, "🦄"),
    getAnimal(types.dog, "🐕"),
  ),
}`;

const myObject = JSOF.parse(code);

const jsonData = JSON.stringify(myObject);

That will be printed as follow:

{
  "label": "My functional json 😍",
  "animals": {
    "_type": "function",
    "path": "concat",
    "props": [
      {
        "_type": "function",
        "path": "getAnimal",
        "props": [
          {
            "_type": "prop",
            "path": "types.unicorn"
          },
          "🦄"
        ]
      },
      {
        "_type": "function",
        "path": "getAnimal",
        "props": [
          {
            "_type": "prop",
            "path": "types.dog"
          },
          "🐕"
        ]
      }
    ]
  }
}

Make the reverse process

Create a JSON data as fallow

const data = {
  "_type": "function",
  path: "pipe",
  props: [
    {
      map: {
        user: [
          {
            "_type": "function",
            path: "get",
            props: ["user"],
          },
          {
            "_type": "function",
            path: "pick",
            props: ["name", "nick", "email"],
          },
        ],
      },
      test: [
        {
          "user.nick": {
            "_type": "function",
            path: "match",
            props: [
              {
                "_type": "prop",
                path: "is.req",
              },
              {
                "_type": "prop",
                path: "is.slug",
              },
            ],
          },
          "user.email": {
            "_type": "function",
            path: "match",
            props: [
              {
                "_type": "prop",
                path: "is.email",
              },
            ],
          },
        },
        {
          "_type": "function",
          path: "get",
          props: ["test"],
        },
      ],
    },
  ],
};

const functionBody = JSOF.stringify(data);

const dependencies = ["pipe", "get", "pick", "match", "is"];

const params = `{ ${dependencies.join(", ")} }`;

const myFunction = eval(`(${params}) => ${functionBody}`);

console.log(myFunction.toString());

And the result will be one anonymous function like this

({ pipe, get, pick, match, is }) =>
  pipe({
    map: { user: [get("user"), pick("name", "nick", "email")] },
    test: [
      { "user.nick": match(is.req, is.slug), "user.email": match(is.email) },
      get("test"),
    ],
  });

Contribuindo com o projeto

Solicitações pull são bem-vindas. Para mudanças importantes, abra um problema primeiro para discutir o que você gostaria de mudar.

License

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i jsof-parser

Weekly Downloads

5

Version

2.0.1

License

MIT

Unpacked Size

62.6 kB

Total Files

22

Last publish

Collaborators

  • andersondrosa