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

bloomrun

v4.1.1

Published

JS object pattern matching

Downloads

20,098

Readme

logo

bloomrun

npm travis coveralls david

A js pattern matcher with results that can be returned in insertion order or depth order. 2.5KB minified and gzipped, runs in the browser. Inspired by bloom filters.

Install

To install bloomrun, simply use npm:

npm install bloomrun --save

Example

The example below can be found here and ran using node example.js. It demonstrates how to use bloomrun for pattern matching with a payload.

'use strict'

var bloomrun = require('bloomrun')()

bloomrun.add({say: 'hello'}, 'Hello World!')
bloomrun.add({say: 'goodbye'}, function () {
  console.log('Goodbye World!')
})
bloomrun.add({say: 'something', to: /.*/}, 'Matched with a regexp and a prop')
bloomrun.add({say: /.*/}, 'Matched with a regexp!')

var hello = bloomrun.lookup({say: 'hello'})
console.log(hello)

var goodbye = bloomrun.lookup({say: 'goodbye'})
goodbye()

var anything = bloomrun.lookup({say: 'anything'})
console.log(anything)

console.log(bloomrun.lookup({say: 'something', to: 'Matteo'}))

API

  • bloomrun()
  • instance.add()
  • instance.remove()
  • instance.lookup()
  • instance.iterator()
  • instance.list()
  • instance.default()

bloomrun([opts])

Creates a new instance of bloomrun.

Options are:

  • indexing: it can be either insertion (default) or depth; if set to insertion, it will try to match entries in insertion order; if set to depth, it will try to match entries with the most properties first. Depth indexing is guaranteed if the patterns overlaps. If multiple matching patterns overlaps it checks on the first overlapping group of patterns that matches. The insertion order of pattern with the same depth is not guaranteed.

for..of construct:

The bloomrun instance implements iterable protocol, all added patterns can be looped over in a for..of construct.


instance.add(pattern [,payload])

Adds a pattern to the bloomrun instance. You can also provide an alternative payload to return instead of the pattern itself. This allows pattern based retrieval of objects. If no payload is provided the pattern itself will be returned.


instance.remove(pattern [,payload])

Removes a pattern from the bloomrun instance. Filters are rebuilt after each removal which may mean the same pattern is matched by another filter. In cases where two patterns differ only by payload, the supplied payload can be used to determine the correct match. If no payload is supplied any matched pattern will be removed regardless of it's own payload.


instance.lookup(obj [, opts])

Looks up the first entry that matches the given obj. A match happens when all properties of the added pattern matches with the one in the passed obj. If a payload was provided it will be returned instead of the pattern.

Options:

  • patterns: true, if you want to retrieve only patterns, not payloads

instance.iterator(obj [, opts])

Returns an iterator, which is an object with a next method. next will return the next pattern that matches the object or null if there are no more. If obj is null, all patterns/payload will be returned.

Options:

  • patterns: true, if you want to retrieve patterns, defaults to false
  • payloads: true, if you want to retrieve payloads, defaults to true

If both patterns and payloads are true, the data will be in the form:

{
  pattern,
  payload
}

for..of construct:

The iterator also implements iterable protocol, matched patterns can be looped over in a for..of construct.


instance.list(obj [, opts])

Returns all patterns that matches the object. If a payload was provided this will be returned instead of the pattern. If obj is null, all patterns/payload will be returned.

Options:

  • patterns: true, if you want to retrieve patterns, defaults to false
  • payloads: true, if you want to retrieve payloads, defaults to true

If both patterns and payloads are true, the data will be in the form:

{
  pattern,
  payload
}

If a default is set, it will be returned if obj is falsy. In case the opts.patterns is true, the element in the list will have the form:

{
  default: true,
  payload
}

instance.default(payload)

Sets a default payload to be returned when no pattern is matched. This allows a single 'catch all' to be defined. By default, null is returned when a pattern is not matched.


Acknowledgements

This project was kindly sponsored by nearForm.

This library is heavily inspired by Richard Rodger's patrun and Seneca.

The bloomrun logo was created, with thanks, by Dean McDonnell

License

Copyright Matteo Collina 2015-2017, Licensed under MIT.