npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

treebranch

v0.3.0

Published

Create domain specific languages. Execute them as code. Serialize them as JSON. Build your own functionality around them.

Downloads

5

Readme

Tree Branch

Create domain specific languages. Evaluate them as code. Serialize them as JSON. Build your own functionality around them.

Overview

Programmers spend a lot of time trying to squeeze a domain language into a programming language. Sometimes it doesn't fit and they have to start over. Sometimes the domain language gets popular and finds it way into a standardized JSON or YAML format. Sometimes—more often than not—the domain language isn't considered before writing code and is figured out along the way, leading to inconsistencies in the domain language and implementation.

Tree Branch is an idea for building domain specific languages in JavaScript that can be evaluated or serialized. Programmers can design the semantics of a language before writing code and later evaluate the code around it or define serializers and deserializers. Because these DSLs can be serialized to something like JSON, they can be shared across the web and across programming languages. Even logic can be shared and evaluated at runtime across networks and platforms given the right design.

Usage

npm install treebranch

Create a Language

const treebranch = require('treebranch');

// Create a language with a name and semantics
const l = treebranch.createLanguage('math', ['add', 'subtract']);

// Build a tree from our DSL
const tree = l.add(l.subtract(4, 2), 8);

// Serialize the tree
const treeList = treebranch.serializers.toList(tree);

The tree value will contain the tree of nodes built from the language. That value will be the equivalent to the following.

const tree = new ExprNode('add',
  new ExprNode('subtract', new NumNode(4), new NumNode(2)),
  new NumNode(8));

This abstract tree provides the power of this method, because it allows developers to write code that represents the abstract tree one-to-one. Trees can be built on the fly or wrapped with additional functionality.

In the example above, the treeList constant will be the serialized version of the defined tree, with each language node prefixed with the name of the language like below.

['math/add',
  ['math/subtract', ['number', 4], ['number', 2]],
  ['number' 8]]]

Running as Code

New languages can be created from existing objects, where the property name is the function name and the value is the function to use. After you have built your tree, you can then pass it back in to be evaluated using the defined functions.

const treebranch = require('treebranch');

const runtime = treebranch.TreeBranch();

// Pass in a name and functions, which will register your code and create a language for you.
const l = runtime.register('math', {
  add: (a, b) => a + b,
  subtract: (a, b) => a - b,
});

const tree = l.add(l.subtract(4, 2), 8);

const result = runtime.eval(tree); // will be 10

License

This code is licensed under the MIT license.