Comparing version 2.2.2 to 2.3.0
@@ -90,2 +90,13 @@ 'use strict' | ||
if (!pattern && this._defaultResult) { | ||
if (opts && opts.patterns && opts.payloads) { | ||
list.push({ | ||
default: true, | ||
payload: this._defaultResult | ||
}) | ||
} else if (!opts || !opts.patterns) { | ||
list.push(this._defaultResult) | ||
} | ||
} | ||
return list | ||
@@ -92,0 +103,0 @@ } |
@@ -14,3 +14,5 @@ 'use strict' | ||
this.pattern = obj | ||
this.onlyPatterns = opts && opts.patterns | ||
this.onlyPatterns = opts && opts.patterns && !opts.payloads | ||
this.patternsAndPayloads = opts && opts.patterns && opts.payloads | ||
this.onlyPayloads = (opts && !opts.patterns && opts.payloads) || !opts | ||
@@ -45,6 +47,11 @@ if (obj) { | ||
if (!this.pattern || deepMatch(current.pattern, this.pattern)) { | ||
if (this.onlyPatterns) { | ||
if (this.onlyPayloads) { | ||
match = current.payload | ||
} else if (this.onlyPatterns) { | ||
match = current.pattern | ||
} else { | ||
match = current.payload | ||
} else if (this.patternsAndPayloads) { | ||
match = { | ||
pattern: current.pattern, | ||
payload: current.payload | ||
} | ||
} | ||
@@ -51,0 +58,0 @@ } |
{ | ||
"name": "bloomrun", | ||
"version": "2.2.2", | ||
"version": "2.3.0", | ||
"description": "JS object pattern matching, powered by bloom filters", | ||
@@ -5,0 +5,0 @@ "main": "bloomrun.js", |
@@ -127,5 +127,17 @@ [![logo][logo-url]][npm-url] | ||
Options: | ||
* `patterns: true`, if you want to retrieve only patterns, not | ||
payloads | ||
* `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: | ||
```js | ||
{ | ||
pattern, | ||
payload | ||
} | ||
``` | ||
------------------------------------------------------- | ||
@@ -140,5 +152,28 @@ <a name="list"></a> | ||
Options: | ||
* `patterns: true`, if you want to retrieve only patterns, not | ||
payloads | ||
* `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: | ||
```js | ||
{ | ||
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: | ||
```js | ||
{ | ||
default: true, | ||
payload | ||
} | ||
``` | ||
------------------------------------------------------- | ||
@@ -145,0 +180,0 @@ |
37
test.js
@@ -554,1 +554,38 @@ 'use strict' | ||
}) | ||
test('patterns and data can be listed while using payloads', function (t) { | ||
t.plan(1) | ||
var instance = bloomrun() | ||
var pattern = { group: '123' } | ||
function payloadOne () { } | ||
function payloadTwo () { } | ||
instance.add(pattern, payloadOne) | ||
instance.add(pattern, payloadTwo) | ||
t.deepEqual(instance.list({ group: '123' }, { patterns: true, payloads: true }), [{ | ||
pattern: pattern, | ||
payload: payloadOne | ||
}, { | ||
pattern: pattern, | ||
payload: payloadTwo | ||
}]) | ||
}) | ||
test('list should return the default', function (t) { | ||
t.plan(3) | ||
var instance = bloomrun() | ||
var payload = { cmd: 'set-policy' } | ||
instance.default(payload) | ||
t.deepEqual(instance.list(), [payload]) | ||
t.deepEqual(instance.list(null, { patterns: true }), []) | ||
t.deepEqual(instance.list(null, { patterns: true, payloads: true }), [{ | ||
default: true, | ||
payload: payload | ||
}]) | ||
}) |
50152
880
217