An open API service indexing awesome lists of open source software.

https://github.com/adbayb/esvisitor

An ECMAScript module to visit any hashmap-like structure (AST, custom tree, ...) and apply some algorithms
https://github.com/adbayb/esvisitor

Last synced: 3 months ago
JSON representation

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

Awesome Lists containing this project

README

        




ESVisitor


An ECMAScript 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:

```bash
# Npm
npm install esvisitor
# Pnpm
pnpm add esvisitor
# Yarn
yarn add esvisitor
```

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

```ts
import { visit } from "esvisitor";

visit(
{
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: Type;
value: Value;
};
```

_You can check the [examples](https://github.com/adbayb/esvisitor/tree/main/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](https://github.com/adbayb/esvisitor/blob/main/CONTRIBUTING.md).


## 📖 License

[MIT](https://github.com/adbayb/esvisitor/blob/main/LICENSE "License MIT")