@open-vanilla/visitor

1.0.0 • Public • Published

@open-vanilla/visitor

A vanilla module to visit any hashmap-like structure (AST, custom tree, ...) and apply some algorithms


✨ Features

  • Tiny module with zero dependencies (less than 300B 🚀)
  • Apply the visitor design pattern in a functional way with ease
  • Data structure agnostic (a custom tree, a parser-specific abstract syntax tree, ...).
    Any hashmap-like structure can be visited as long as each node exposes a type field

🚀 Quickstart

1️⃣ Install the library:

# Npm
npm install @open-vanilla/visitor
# Pnpm
pnpm add @open-vanilla/visitor
# Yarn
yarn add @open-vanilla/visitor

2️⃣ Once you're done, you can play with the API:

import { visit } from "@open-vanilla/visitor";

visit<Nodes>(
	{
		type: "Root",
		value: {
			type: "Array",
			value: [
				{
					type: "Variable",
					value: "Hello",
				},
			],
		},
	},
	{
		Array(node) {
			console.log("Array node: ", node);
		},
		Root(node) {
			console.log("Root node: ", node);
		},
		Variable(node) {
			console.log("Variable node: ", node);
		},
	},
);

type Nodes = {
	Array: Node<"Array", Nodes["Variable"][]>;
	Root: Node<"Root", Nodes["Array"][]>;
	Variable: Node<"Variable", string>;
};

type Node<Type, Value> = {
	type: Type;
	value: Value;
};

You can check the examples folder for different hashmap structure processing (including parser-specific abstract syntax trees).


✍️ Contribution

We're open to new contributions, you can find more details here.


📖 License

MIT


Readme

Keywords

none

Package Sidebar

Install

npm i @open-vanilla/visitor

Weekly Downloads

25

Version

1.0.0

License

MIT

Unpacked Size

6.46 kB

Total Files

6

Last publish

Collaborators

  • adbayb