Skip to content

Commit

Permalink
Default pattern now supported
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdonnelldean committed Jun 21, 2016
1 parent 5aa79ea commit d120c59
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion bloomrun.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function BloomRun (opts) {
this._isDeep = opts && opts.indexing === 'depth'
this._buckets = []
this._properties = new Set()
this._defaultResult = null
}

function addPatterns (toAdd) {
Expand Down Expand Up @@ -56,6 +57,10 @@ function removeProperty (key) {
}

BloomRun.prototype.add = function (pattern, payload) {
if (Object.keys(pattern).length === 0 && pattern.constructor === Object) {
this._defaultResult = payload || payload
}

var buckets = matchingBuckets(this._buckets, pattern)
var bucket
var properties = this._properties
Expand Down Expand Up @@ -104,7 +109,7 @@ BloomRun.prototype.remove = function (pattern, payload) {

BloomRun.prototype.lookup = function (pattern, opts) {
var iterator = new Iterator(this, pattern, opts)
return iterator.next()
return iterator.next() || this._defaultResult
}

BloomRun.prototype.list = function (pattern, opts) {
Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ test('pattern is returned on match', function (t) {
t.deepEqual(instance.lookup(pattern), pattern)
})

test('default match is supported', function (t) {
t.plan(1)

var instance = bloomrun()
var pattern = {}
var payload = { cmd: 'set-policy' }

instance.add(pattern, payload)

t.deepEqual(instance.lookup({foo: 'bar'}), payload)
})

test('payload is returned instead of pattern if it exists', function (t) {
t.plan(1)

Expand Down

0 comments on commit d120c59

Please sign in to comment.