• Stars
    star
    120
  • Rank 286,652 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A js pattern matcher

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([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.

More Repositories

1

autocannon

fast HTTP/1.1 benchmarking tool written in Node.js
JavaScript
7,545
star
2

fastq

Fast, in memory work queue
JavaScript
771
star
3

make-promises-safe

A node.js module to make the use of promises safe
JavaScript
670
star
4

hyperid

Uber-fast unique id generation, for Node.js and the browser
JavaScript
659
star
5

msgpack5

A msgpack v5 implementation for node.js, with extension points / msgpack.org[Node]
JavaScript
484
star
6

async-cache-dedupe

Async cache with dedupe support
JavaScript
370
star
7

split2

Split Streams3 style
JavaScript
269
star
8

reusify

Reuse objects and functions with style
JavaScript
156
star
9

steed

horsepower for your modules
JavaScript
155
star
10

fastparallel

Zero-overhead parallel function call for node.js. Also supports each and map!
JavaScript
153
star
11

qest

The Internet of Things broker that loves devices and web developers.
JavaScript
144
star
12

close-with-grace

Exit your process, gracefully (if possible) - for Node.js
JavaScript
127
star
13

mqemitter

An Opinionated Message Queue with an emitter-style API
JavaScript
118
star
14

on-exit-leak-free

Execute a function on exit without leaking memory, allowing all objects to be garbage collected
JavaScript
113
star
15

cloneable-readable

Clone a Readable stream, safely
JavaScript
106
star
16

loopbench

Benchmark your event loop
JavaScript
101
star
17

commist

Build your commands on minimist!
JavaScript
99
star
18

fast-json-parse

The fastest way to parse JSON safely
JavaScript
85
star
19

climem

Monitor the memory consumption of your node process via CLI
JavaScript
85
star
20

desm

get the file directory from import.meta.url
JavaScript
83
star
21

fastbench

the simplest benchmark you can run on node
JavaScript
83
star
22

syncthrough

Transform your data as it pass by, synchronously.
JavaScript
77
star
23

mows

Using MQTT.js in the browser over WebSocket -- Built with browserify!
JavaScript
73
star
24

hyperemitter

Horizontally Scalable EventEmitter powered by a Merkle DAG
JavaScript
71
star
25

heroku-buildpack-graphicsmagick

Shell
68
star
26

fastseries

Zero-overhead asynchronous series/each/map function calls
JavaScript
65
star
27

docker-loghose

Collect all the logs from all docker containers
JavaScript
63
star
28

hwp

JavaScript
58
star
29

tinysonic

a quick syntax for JSON object
JavaScript
56
star
30

fastify-sandbox

load a plugin via a synchronous worker
JavaScript
54
star
31

infinicache

JavaScript
53
star
32

ponte

The M2M/IoT Bridge for REST developers
51
star
33

public-speaking

Matteo Collina's portfolio of public speaking engagements
CSS
48
star
34

h2url

experimental http2 client for node and the CLI
JavaScript
46
star
35

heroku-buildpack-imagemagick

An heroku buildpack with the latest version of ImageMagick
Shell
46
star
36

multines

Multi-process nes backend, turn nes into a fully scalable solution
JavaScript
45
star
37

autocannon-ci

run your benchmarks as part of your dev flow, for Node.js
JavaScript
45
star
38

mercurius-auto-schema

JavaScript
43
star
39

native-hdr-histogram

node.js bindings for hdr histogram C implementation
C
42
star
40

mqemitter-redis

Redis-powered MQEmitter
JavaScript
41
star
41

tentacoli

All the ways for doing requests/streams multiplexing over a single stream
JavaScript
39
star
42

take-your-http-server-to-ludicrous-speed

Take Your HTTP server to Ludicrous Speed
HTML
38
star
43

openapi-graphql

Create a GraphQL from an OpenAPI schema
TypeScript
36
star
44

levelgraph-talk-nodejsconfit

My Talk at nodejsconf.it 2014! "How to Cook a Graph Database in a Night"
CSS
36
star
45

single-user-cache

JavaScript
35
star
46

retimer

reschedulable setTimeout for you node needs
JavaScript
35
star
47

generify

A reusable project generator
JavaScript
32
star
48

we-are-not-object-oriented-anymore-demo

The demo for my "we are not object-oriented anymore" talk
JavaScript
31
star
49

node-errormailer

Sending email for each error in your node app was never easier! It fully support connect and express.
JavaScript
30
star
50

stream-iterators-utils

Utility belt for using async iterators with streams
JavaScript
30
star
51

autocow

Display cows every two seconds, because you can
JavaScript
29
star
52

ioredis-auto-pipeline

Automatic redis pipeline support
JavaScript
27
star
53

typescript-async-await-target-cost

Shell
27
star
54

worker

Running Node within Node (a fork of synchronous-worker)
C++
25
star
55

type-safe-fastify

An example on how to set up Fastify routes with full type safety
TypeScript
24
star
56

pbkdf2-password

Easy salt/password creation for Node.js, extracted from Mosca
JavaScript
24
star
57

one-two-three-fastify

JavaScript
23
star
58

mqtt-level-store

Store your in-flight MQTT message on Level, for Node
JavaScript
23
star
59

fastfall

call your callbacks in a waterfall, at speed
JavaScript
22
star
60

fastify-astro

Let's wrap Astro in a Fastify plugin
22
star
61

the-cost-of-logging

My talk "The Cost of Logging" about our uber-fast Pino logger
HTML
20
star
62

localswarm

Like airswarm, but using tcp ports and unix sockets - node.js style
JavaScript
20
star
63

unix-socket-leader

Elect a leader using unix sockets, for node
JavaScript
20
star
64

rake-minify

A rake task to minify javascripts and coffeescripts
Ruby
19
star
65

fastify-auth-mongo-jwt

Sample user-management (signup, login) with Fastify and JWT
JavaScript
19
star
66

docker-allcontainers

Get notified when a new container is started or stopped
JavaScript
19
star
67

mqemitter-mongodb

MongoDB based MQEmitter
JavaScript
18
star
68

fast-write-atomic

Fast way to write a file atomically, for Node.js
JavaScript
18
star
69

minimist

A fork of minimist, published as @matteo.collina/minimist
JavaScript
17
star
70

never-ending-stream

Automatically restarts your stream for you when it ends
JavaScript
16
star
71

throughv

stream.Transform with parallel chunk processing
JavaScript
16
star
72

fastify-massive

Massive.js plugin for Fastify
JavaScript
15
star
73

baseswim

A base swim node
JavaScript
15
star
74

modular_monolith

Example of the "Building a Modular Monolith with Fastify" talk
JavaScript
15
star
75

autocannon-compare

Compare two autocannon runs
JavaScript
15
star
76

dateformat

A CJS version of dateformat, forked from node-dateformat
JavaScript
14
star
77

kanban

Kanban is a node.js control-flow library. As the Japanese methodology, it is pull-based.
JavaScript
14
star
78

bhdr

benchmark utility powered by hdr histograms, for node
JavaScript
14
star
79

streampecker

Peek a stream!
JavaScript
13
star
80

blueslider

Turn your slides using you TI SensorTag
JavaScript
13
star
81

mcdo

JavaScript
13
star
82

reduplexer

reduplexer(writable, readable, options)
JavaScript
13
star
83

levelgraph-recursive

Breadth-first and Deep-first for your LevelGraph
JavaScript
13
star
84

object-router

Route your functions with pattern matching
JavaScript
12
star
85

help-me

Help command for node, partner of minimist and commist
JavaScript
12
star
86

mongo-clean

Clean all the collections in a mongo database
JavaScript
12
star
87

mqstreams

MQ pub/sub as streams - based on mqemitter
JavaScript
12
star
88

net-object-stream

Turn any binary stream into an object stream
JavaScript
11
star
89

levelup-talk-cloudconf

My talk for CloudConf on LevelUp
CSS
11
star
90

we-are-not-object-oriented-anymore

We are not Object Oriented anymore
HTML
11
star
91

fastify-undici-dispatcher

An undici dispatcher to in-process Fastify servers
JavaScript
11
star
92

reaching-ludicrous-speed

My Node.js Interactive 2015 presentation
HTML
11
star
93

hello-fastify

A Fastify "hello world" template, with tests
JavaScript
11
star
94

conf-app

JavaScript
10
star
95

capistrano-remote-cache-with-project-root

Ruby
10
star
96

nrts

node:test runner wrapper with TypeScript support
JavaScript
10
star
97

js-everywhere

The slides for my QCon 2016 talk "JS Everywhere"
HTML
10
star
98

scaling-state

Application level sharding made simple!
HTML
10
star
99

mac-key-press

C++
9
star
100

bespoke-run

Run your code snippets in Bespoke.js
JavaScript
9
star