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 |
|
9 | A js pattern matcher based on bloom filters, inspired by [patrun](http://npm.im/patrun).
|
10 | But 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
|
20 | To install bloomrun, simply use npm:
|
21 |
|
22 | ```
|
23 | npm install bloomrun --save
|
24 | ```
|
25 |
|
26 | <a name="example"></a>
|
27 | ## Example
|
28 | The example below can be found [here][example] and ran using `node example.js`. It
|
29 | demonstrates how to use bloomrun for pattern matching with a payload.
|
30 |
|
31 | ```js
|
32 | 'use strict'
|
33 |
|
34 | var bloomrun = require('bloomrun')()
|
35 |
|
36 | bloomrun.add({say: 'hello' }, 'Hello World!')
|
37 | bloomrun.add({say: 'goodbye'}, function () {
|
38 | console.log('Goodbye World!')
|
39 | })
|
40 |
|
41 | var hello = bloomrun.lookup({say: 'hello'})
|
42 | console.log(hello)
|
43 |
|
44 | var goodbye = bloomrun.lookup({say: 'goodbye'})
|
45 | goodbye()
|
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 |
|
62 | Creates a new instance of Bloomrun.
|
63 |
|
64 | Options 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 |
|
75 | Adds a pattern to the Bloomrun instance. You can also provide an alternative
|
76 | payload to return instead of the pattern itself. This allows pattern based
|
77 | retrieval of objects. If no payload is provided the pattern itself will be
|
78 | returned.
|
79 |
|
80 | -------------------------------------------------------
|
81 |
|
82 | <a name="remove"></a>
|
83 | ### instance.remove(pattern [,payload])
|
84 |
|
85 | Removes a pattern from the Bloomrun instance. Filters are rebuilt after each
|
86 | removal which may mean the same pattern is matched by another filter. In cases
|
87 | where two patterns differ only by payload, the supplied payload can be used to
|
88 | determine the correct match. If no payload is supplied any matched pattern will
|
89 | be removed regardless of it's own payload.
|
90 |
|
91 | -------------------------------------------------------
|
92 |
|
93 | <a name="lookup"></a>
|
94 | ### instance.lookup(obj [, opts])
|
95 |
|
96 | Looks up the first entry that matches the given obj. A match happens
|
97 | when all properties of the added pattern matches with the one in the
|
98 | passed obj. If a payload was provided it will be returned instead of
|
99 | the pattern.
|
100 |
|
101 | Options:
|
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 |
|
109 | Returns an iterator, which is an object with a `next` method. `next`
|
110 | will return the next pattern that matches the object or `null` if there
|
111 | are no more.
|
112 | If `obj` is null, all patterns/payload will be returned.
|
113 |
|
114 | Options:
|
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 |
|
122 | Returns all patterns that matches the object. If a payload was provided
|
123 | this will be returned instead of the pattern.
|
124 | If `obj` is null, all patterns/payload will be returned.
|
125 |
|
126 | Options:
|
127 | * `patterns: true`, if you want to retrieve only patterns, not
|
128 | payloads
|
129 |
|
130 | ## Acknowledgements
|
131 |
|
132 | This library is heavily inspired by Richard Rodger's
|
133 | [patrun](http://npm.im/patrun) and [seneca](http://npm.im/seneca).
|
134 | Also, It would not be possible without
|
135 | [bloomfilter](https://www.npmjs.com/package/bloomfilter).
|
136 |
|
137 | The bloomrun logo was created, with thanks, by [Dean McDonnell](https:/github.com/mcdonnelldean)
|
138 |
|
139 | ## License
|
140 |
|
141 | Copyright 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
|