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

sigmatch

v0.9.0

Published

Yet another way to overload functions

Downloads

12

Readme

sigmatch

Yet another way to overload functions

Motivation

I very often find myself in situations where I want to create a function that supports several alternative call signatures. In javascript (or Coffeescript) you usually do this by looking at the arguments-Object to find out how many and what kind of arguments were given. This is typically followed by an awkward bit of shuffeling where you decide what the actual name and content of each parameter would be before dispatching to the code that does the actual work.

The sigmatch module allows you to do this in a more declarative way.

Install

npm install --save sigmatch

Usage

This example is based on a real use of this module in the tbob project.

Let's say you are writing a DSL describe json-like data structures. You want to create a directive for describing attributes. The attribute has a name and an associated value type. It is also possible to give a default value for that attribute or even a strategy that calculates that default value in dependency to the value of other attributes. So the most general signature would look something like this:

createAttribute = (name, type, dependencies, defaultStrategy)->
  # actual code to create the attribute

Now, you wouldn't want this in a DSL. So here is what you could do, using sigmatch:

sigmatch = require "sigmatch"
attributeDirective = sigmatch (match)->
  # the most "complete" case:
  match "s,o,a,f", createAttribute
  # use an optional static default value
  match "s,o,.?", (name, typeExpr, defaultValue)-> 
    createAttribute name, typeExpr, [], ->defaultValue
  # do not specify a type
  match "s,a,f", (name, deps, defaultStrategy)->
    createAttribute name, any(), deps, defaultStrategy
  # same, but with static default value
  match "s,.", (name, defaultValue)->
    createAttribute name, any(),[], ->defaultValue
  # catch-all rule to produce an error
  match ".*", -> 
    throw new Error("unsupported call signature")

Rules

As seen in the above example, sigmatch is taking a function as its single argument. It calls that function with a directive for creating rules. We called it match in the above example.

To create a rule, you call match with a pattern and an action. The pattern is a comma-separated list of attribute matchers. The following matchers are supported:

| Matcher | Meaning | |---------|-------------------------------------------------| | . | matches any argument (but not undefined) | | s | matches a string | | n | matches a number | | a | matches any array | | o | matches any object (but not functions or arrays)|

By default, a single matcher will match exactly one argument. This can be changed by appending multiplicity modifier:

| Modifier | Matches | Produces | |----------|------------------------------|-----------------------------------------| | (none) | match exactly one argument | a single value | | ? | match zero or one argument | a single value or null | | + | match one or more arguments | an array containing at least one element| | * | match zero or more arguments | an array |

When the pattern matches, the values produced by the matchers will be passed to the action. sigmatch will try the given rule one after another and stop after the first matching rule. It will return whatever the action of that rule returned.