UNPKG

5 kBMarkdownView Raw
1[![logo][logo-url]][npm-url]
2
3# bloomrun
4[![npm][npm-badge]][npm-url]
5[![travis][travis-badge]][travis-url]
6[![coveralls][coveralls-badge]][coveralls-url]
7[![david][david-badge]][david-url]
8
9A js pattern matcher based on bloom filters, inspired by [patrun](http://npm.im/patrun).
10But different: 10x faster, and with results that can be returned in __insertion order__ or __depth order__.
11
12* [Install](#install)
13* [Example](#example)
14* [API](#api)
15* [Acknowledgements](#acknowledgements)
16* [License](#license)
17
18<a name="install"></a>
19## Install
20To install bloomrun, simply use npm:
21
22```
23npm install bloomrun --save
24```
25
26<a name="example"></a>
27## Example
28The example below can be found [here][example] and ran using `node example.js`. It
29demonstrates how to use bloomrun for pattern matching with a payload.
30
31```js
32'use strict'
33
34var bloomrun = require('bloomrun')()
35
36bloomrun.add({say: 'hello' }, 'Hello World!')
37bloomrun.add({say: 'goodbye'}, function () {
38 console.log('Goodbye World!')
39})
40
41var hello = bloomrun.lookup({say: 'hello'})
42console.log(hello)
43
44var goodbye = bloomrun.lookup({say: 'goodbye'})
45goodbye()
46```
47
48<a name="api"></a>
49## API
50
51 * <a href="#constructor"><code><b>bloomrun()</b></code></a>
52 * <a href="#add"><code>instance.<b>add()</b></code></a>
53 * <a href="#remove"><code>instance.<b>remove()</b></code></a>
54 * <a href="#lookup"><code>instance.<b>lookup()</b></code></a>
55 * <a href="#iterator"><code>instance.<b>iterator()</b></code></a>
56 * <a href="#list"><code>instance.<b>list()</b></code></a>
57
58-------------------------------------------------------
59<a name="constructor"></a>
60### bloomrun([opts])
61
62Creates a new instance of Bloomrun.
63
64Options are:
65
66* `indexing`: it can be either `insertion` (default) or `depth`;
67 if set to `insertion`, it will try to match entries in insertion order;
68 if set to `depth`, it will try to match entries with the most
69 properties first.
70
71-------------------------------------------------------
72<a name="add"></a>
73### instance.add(pattern [,payload])
74
75Adds a pattern to the Bloomrun instance. You can also provide an alternative
76payload to return instead of the pattern itself. This allows pattern based
77retrieval of objects. If no payload is provided the pattern itself will be
78returned.
79
80-------------------------------------------------------
81
82<a name="remove"></a>
83### instance.remove(pattern [,payload])
84
85Removes a pattern from the Bloomrun instance. Filters are rebuilt after each
86removal which may mean the same pattern is matched by another filter. In cases
87where two patterns differ only by payload, the supplied payload can be used to
88determine the correct match. If no payload is supplied any matched pattern will
89be removed regardless of it's own payload.
90
91-------------------------------------------------------
92
93<a name="lookup"></a>
94### instance.lookup(obj [, opts])
95
96Looks up the first entry that matches the given obj. A match happens
97when all properties of the added pattern matches with the one in the
98passed obj. If a payload was provided it will be returned instead of
99the pattern.
100
101Options:
102 * `patterns: true`, if you want to retrieve only patterns, not
103 payloads
104
105-------------------------------------------------------
106<a name="iterator"></a>
107### instance.iterator(obj [, opts])
108
109Returns an iterator, which is an object with a `next` method. `next`
110will return the next pattern that matches the object or `null` if there
111are no more.
112If `obj` is null, all patterns/payload will be returned.
113
114Options:
115 * `patterns: true`, if you want to retrieve only patterns, not
116 payloads
117
118-------------------------------------------------------
119<a name="list"></a>
120### instance.list(obj [, opts])
121
122Returns all patterns that matches the object. If a payload was provided
123this will be returned instead of the pattern.
124If `obj` is null, all patterns/payload will be returned.
125
126Options:
127 * `patterns: true`, if you want to retrieve only patterns, not
128 payloads
129
130## Acknowledgements
131
132This library is heavily inspired by Richard Rodger's
133[patrun](http://npm.im/patrun) and [seneca](http://npm.im/seneca).
134Also, It would not be possible without
135[bloomfilter](https://www.npmjs.com/package/bloomfilter).
136
137The bloomrun logo was created, with thanks, by [Dean McDonnell](https:/github.com/mcdonnelldean)
138
139## License
140
141Copyright Matteo Collina 2015-2016, Licensed under [MIT][].
142
143[MIT]: ./LICENSE
144[example]: ./example.js
145
146[travis-badge]: https://travis-ci.org/mcollina/bloomrun.svg?branch=master
147[travis-url]: https://travis-ci.org/mcollina/bloomrun
148[npm-badge]: https://badge.fury.io/js/bloomrun.svg
149[npm-url]: https://npmjs.org/package/bloomrun
150[logo-url]: https://raw.githubusercontent.com/mcollina/bloomrun/master/assets/bloomrun.png
151[coveralls-badge]: https://coveralls.io/repos/mcollina/bloomrun/badge.svg?branch=master&service=github
152[coveralls-url]: https://coveralls.io/github/mcollina/bloomrun?branch=master
153[david-badge]: https://david-dm.org/mcollina/bloomrun.svg
154[david-url]: https://david-dm.org/mcollina/bloomrun