@nil-/jwalk
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

@nil-/jwalk

A library that walks through JSON data following schema provided. It is a play of words with "jaywalk" but for JSON.

A "jaywalker" is described as a pedestrian that violates the "rules".

In contrary, "jwalk" expects users to provide the "rules" to follow how to traverse the graph.

The only responsibility of jwalk is to execute the nodes by traversing the graph you setup yourself.

Anything you can do inside the nodes is up to your imagination.

Example

import { jwalker } from "@nil-/jwalk";

const j = jwalker()
    .node("Boolean", "boolean", {
        action: ({ value }) => {
            console.log("[BOOL]", "INIT", value);
            return {
                update: (value) => console.log("[BOOL]", "UPDATE", value),
                destroy: () => console.log("[BOOL]", "DESTROY", "-")
            };
        }
    })
    .node("Number", "number", {
        action: ({ value }) => {
            console.log("[Number]", "INIT", value);
            return {
                update: (value) => console.log("[Number]", "UPDATE", value),
                destroy: () => console.log("[Number]", "DESTROY", "-")
            };
        }
    })
    .node("ROOT", "tuple", {
        content: ["Boolean", "Number"],
        action: ({ value, auto }) => {
            console.log("[ROOT]", "INIT", value);
            const { update, destroy } = auto(
                () => {},
                () => {},
                value
            );
            return {
                update: (value) => {
                    console.log("[ROOT]", "UPDATE", value);
                    update(value);
                },
                destroy: () => {
                    destroy();
                    console.log("[ROOT]", "DESTROY", "-");
                }
            };
        }
    })
    .build(null, [true, 100]);

j.update([false, 200]);

j.destroy();

Result

[GROUP]  INIT    [true, 100]
[BOOL]   INIT    true
[Number] INIT    100
[GROUP]  UPDATE  [false, 200]
[BOOL]   UPDATE  false
[Number] UPDATE  200
[Number] DESTROY
[BOOL]   DESTROY
[GROUP]  DESTROY

Limitations

  • Currently, schema validation is expected to be done by the user before build/update.
    Some considerations is to add zod json validation before execution of each node.

For more information see link

Package Sidebar

Install

npm i @nil-/jwalk

Weekly Downloads

1

Version

1.0.1

License

ISC

Unpacked Size

35.3 kB

Total Files

17

Last publish

Collaborators

  • nil-