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

erm-js

v0.3.0

Published

The Esoteric Reactive Machine

Downloads

22

Readme

erm.js - The Esoteric Reducing Machine

erm.js creates composable machines for pattern matching.

let monkeyKeystrokes =
    infiniteTypewriters(infiniteMonkeys).readToEnd()

// raw (data + location) with closures
match(...monkeyKeystrokes)(
  make(macbeth)(book => worksOfShakespeare.push(book)),
  make(twogentlemenofverona)(book => worksOfShakespeare.push(book)),
  make(ayorkshiretragedy)(haltAndCatchFire),
  _
)

// values with closures
match(...monkeyKeystrokes)(
  make(macbeth).stream(book => worksOfShakespeare.push(book)),
  make(twogentlemenofverona).stream(book => worksOfShakespeare.push(book)),
  make(ayorkshiretragedy)(haltAndCatchFire),
  _
)

// values to arrays
match(...monkeyKeystrokes)(
  make(macbeth).push(worksOfShakespeare),
  make(twogentlemenofverona).push(worksOfShakespeare),
  make(ayorkshiretragedy)(haltAndCatchFire),
  _
)

If like me you've got data, you've tried pattern matching with rxjs, you've tried finding arrays in arrays with the Knuth-Morris-Pratt algorithm, but it's all too much and not quite what you need, then maybe erm is the javascript pattern matching library you're looking for!

note: this project is (pre) alpha right now and discussed APIs are subject to change

Introduction

erm.js will work with arrays of anything, including strings.

Principle of Operation

  • A match$machine has one iterable input and has many make$machines.
  • A match$machine sends its iterable input in slices of n(1,∞) items to a make$machine accepting n arguments.
  • When a make$machine predicate returns false, the match$machine restarts the slicing cycle sending input to the next make$machine in the chain.
  • A match$machine will terminate when all input is accepted by the make$machines.
  • A _ machine is a make$machine that accepts any input and always advances match$machine 1 position.

The static Match API

match(...input)(make(...predicate:unary|...value)[.until(haltpredicate:unary|value)](output, [error])[,...])

match accepts ...input and returns a match$machine - a callable object that accepts one or more make$machines

make accepts ...predicate|...value and return a make$machine- a callable object that accepts an output callback and optionally and error callback.

Fixed Size Patterns

A make$machine will be activated with the same number of items as predicate has parameters -or- the same number of ...values provided; which is to say the make$machine has the same arity; so a predicate p => ... will produce a machine that activates with 1 parameter, in this instance p; and the values ...['4', '2'] will produce a machine that activates with 2 parameters. 4 and 2. Because of this, predicates with a ...rest parameter are not compatible-. Variable length patterns can be matched using make$machine.until - à la Kleene star...

Variable Size Patterns

The make$machine also exposes an optional until method which causes the machine to run again until the haltpredicate signals true. To illustrate this with an albeit contrived example, compare it to the Regex [^] and * operator:

// regex
let username = /[^@]*/.match(emailaddress)
saveUsername(username)

// erm with predicates
match(emailaddress)(
  make(c => true).until(make(c => c == '@'))(output => saveUsername(output)),
  _
)

// erm with literals
match(emailaddress)(
  make(_).until('@')(output => saveUsername(output)),
  _
)

Predicates, Values and Callbacks

predicate and haltpredicate are as you would expect, p => true|false.

If a value value is supplied to make it is automatically converted to p => p == value i.e. make(3.14) == make(p => p == 3.14)

output is your supplied callback function that is invoked with a single object { value, signal, location: { start, length } }

error is your optional callback function that is invoked with a single object { error, location: { start, length } }

Utility Functions and Constants

Match.not() will invert a predicate while preserving arity e.g. let TRUE = make(p => true); let FALSE = make(not(TRUE))

Match._ is the 'unit' value symbol and acts as a wildcard when used in place of a make$machine. Like the default: label in a switch statement, _ catches anything that your make$machines don't. Unlike the default: label in a switch statement, a match$machine without a _ will not be able to read unmatched data and may not terminate.

Quick Start

Install

npm install erm-js

Usage

The Match class exposes the basic building blocks { match, make, not, _ } for composing machines:

const { match, make, not, _  } = require('erm-js').Match

match(..."my input data") (
  make((i, n) => i == "i" && n == "n")(_in => console.log(_in)),
  _
)

Examples

const { match, make, not, _  } = require('erm-js').Match

// simple predicates
let h = p => p == 'h'
let e = p => p == 'e'
let l = p => p == 'l'
let o = p => p == 'o'
let z = p => p == 'z'

// still simple but more useful
let hello = (ph,pe,pl,pL,po) => h(ph) && e(pe) && l(pl) && l(pL) && o(po)

// tada!
match(...'hello world')(
  make(h)(x => console.log('h found:', x)),
  make(e)(x => console.log('e found:', x)),
  make(l)(x => console.log('l found:', x)),
  _
)

// kapow!
match(...'hello world')(
  make(hello)(x => console.log('hello found:', x)),
  _
)

// zorb!
match(...'hello world')(
  make(l).until(not(o))(x => console.log('ll found:', x)),
  _
)

// implementing a take-while function
let takewhile = input => predicate => {
  let items = []
  match(...input)(
    make(predicate).until(p => !predicate(p))(result => items.push(...result.value)).break(),
    _
  )
  return items
}

// and using it
let taken = takewhile(dumbpasswords)(p => p != "111111")

// implementing a partition by function
let partition = input => predicate => {
  let left = []
  let right = []
  match(...input)(
    make(predicate)(q => left.push(q.value)),
    make(p => !predicate(p))(q => right.push(q.value)),
    _
  )
  return [left, right]
}

// and using it
let [lefthandside, righthandside] = partition(dumbpasswords)(p => p.match(/^\d*$/))