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

@ursamu/parser

v1.4.9

Published

install the parser with your favorite package manager:

Downloads

426

Readme

The Javascript powered, pluggable Muschcode engine.

Installation

install the parser with your favorite package manager:

npm install @ursamu/parser
yarn add @ursamu/parser

Basic Usage

import { Parser } from "@ursamu/parser";

// Create a new parser.
const parser = new Parser();

// add a function
parser.add("div", (args) => parseInt(args[0]) / parseInt(args[1]));

// Add some substitutions
parser.addSub(
  "telnet",
  { before: /%r/g, after: "\n" },
  { before: /%b/g, after: " " },
  { before: /%t/g, after: "\t" }
);

// run the parser!
parser
  .string("telnet", {
    msg: "Hello %0!! [div(6,2)]",
    scope: { "%0": "World!" },
    data: {},
  })
  .then((results) => console.log(results)); // => Hello World!!! 3

string(lists: string, ctx: Context) => Promise<string>

Functions

Functions are the basis of mushcode, and through the parser you are able to represent whatever codebase you wish!

add(name: string, (args: string[], data = {}) => {})

Add a new function to the parser.

parser.add("hello", (args) => `hello ${args[0]}!`);
// hello(Dave) => hello Dave!

parser.add("add", (args) => args.reduce((a, b) => a + parseInt(b), 0));
// add(2,3) => 5
  • args The following args are available to the add method:
    • args: any[] An array of arguments given the function in a comma seperated list.
    • data: {} An object where any data needed by the run of the parser is kept. enactor, target, location, etc.

Substitutions

The parser comes with a customisable substitution system that can take stand in expressions. In a Mux-like example %crWARNING!!%cn. To customize what substitutisns are available through the parser:

addSubs(list: string, ...subs: Sub[])

Add a new subsitution to the parser. There are two lists that are pre-determined. pre and post. pre substitutions happen before the code is run through the eval process. post happens after all of the other substitutions have been run.

  • list is the list of substitutions that this range should be added too.
  • subs A comma seperated list of substitutions to be added to the particular label.
    • before The starting string or regular expression to search for.
    • after What the matched regular expression or text should be substituted for. If you've ised a regi;ar expression for your before, then you can use wildcard matches $1.
    • strip? When a function strips the substitutions from your string, what characters should it be substituted with? With most you can get away with a space, or leaving the field blank.
parser.addSubs("telnet", [
  {
    before: /%r/g,
    after: "\n",
  }.
  {
    before: /%t/g,
    after: "\t",
    strip: "    "
  }
]);

substitute(lists: string, msg: string)

Make substitutions from the provided apace seperated list string omto the message.

const results = parser.substitute("telnet", "This is a %rtest.");

Plugins

The parser is customizable through a rogbust plugin system.

parser.plugin(myPlugin)

The syntax for creating a plugin is simple:

export const plugin = (parser) => {
  parser.add("add", (args) => args.reduce((a, b) => a + b, 0));
};

Development