Comparing version 2.2.1 to 2.2.2
@@ -6,6 +6,3 @@ 'use strict' | ||
var PatternSet = require('./lib/patternSet') | ||
var genKeys = require('./lib/genKeys') | ||
var matchingBuckets = require('./lib/matchingBuckets') | ||
var deepMatch = require('./lib/deepMatch') | ||
var deepSort = require('./lib/deepSort') | ||
var onlyRegex = require('./lib/onlyRegex') | ||
@@ -20,39 +17,6 @@ | ||
this._buckets = [] | ||
this._regexBucket = {data: []} | ||
this._regexBucket = new Bucket(this._isDeep) | ||
this._defaultResult = null | ||
} | ||
function addPatterns (toAdd) { | ||
this.filter.add(toAdd) | ||
} | ||
function addPatternSet (patternSet) { | ||
this.add(patternSet.pattern, patternSet.payload) | ||
} | ||
function removePattern (bucket, pattern, payload) { | ||
var foundPattern = false | ||
for (var i = 0; i < bucket.data.length; i++) { | ||
if (deepMatch(pattern, bucket.data[i].pattern)) { | ||
if (payload === null || payload === bucket.data[i].payload) { | ||
bucket.data.splice(i, 1) | ||
foundPattern = true | ||
removePattern(bucket, pattern, payload) | ||
} | ||
} | ||
} | ||
return foundPattern | ||
} | ||
function removeBucket (buckets, bucket) { | ||
for (var i = 0; i < buckets.length; i++) { | ||
if (bucket === buckets[i]) { | ||
buckets.splice(i, 1) | ||
} | ||
} | ||
} | ||
BloomRun.prototype.default = function (payload) { | ||
@@ -64,3 +28,3 @@ this._defaultResult = payload | ||
if (onlyRegex(pattern)) { | ||
this._regexBucket.data.push(new PatternSet(pattern, payload, this._isDeep)) | ||
this._regexBucket.add(new PatternSet(pattern, payload, this._isDeep)) | ||
return this | ||
@@ -75,18 +39,24 @@ } | ||
} else { | ||
bucket = new Bucket() | ||
bucket = new Bucket(this._isDeep) | ||
this._buckets.push(bucket) | ||
} | ||
genKeys(pattern).forEach(addPatterns, bucket) | ||
var patternSet = new PatternSet(pattern, payload, this._isDeep) | ||
bucket.data.push(patternSet) | ||
bucket.add(patternSet) | ||
if (this._isDeep) { | ||
bucket.data.sort(deepSort) | ||
} | ||
return this | ||
} | ||
function addPatternSet (patternSet) { | ||
this.add(patternSet.pattern, patternSet.payload) | ||
} | ||
function removeBucket (buckets, bucket) { | ||
for (var i = 0; i < buckets.length; i++) { | ||
if (bucket === buckets[i]) { | ||
buckets.splice(i, 1) | ||
} | ||
} | ||
} | ||
BloomRun.prototype.remove = function (pattern, payload) { | ||
@@ -100,5 +70,5 @@ var matches = matchingBuckets(this._buckets, pattern) | ||
if (removePattern(bucket, pattern, payload)) { | ||
if (bucket.remove(pattern, payload)) { | ||
removeBucket(this._buckets, bucket) | ||
bucket.data.forEach(addPatternSet, this) | ||
bucket.forEach(addPatternSet, this) | ||
} | ||
@@ -105,0 +75,0 @@ } |
'use strict' | ||
var BloomFilter = require('bloomfilter').BloomFilter | ||
var deepSort = require('./deepSort') | ||
var genKeys = require('./genKeys') | ||
var deepMatch = require('./deepMatch') | ||
function Bucket () { | ||
function Bucket (isDeep) { | ||
this.filter = new BloomFilter( | ||
@@ -11,4 +14,43 @@ 32 * 256, // number of bits to allocate. | ||
this.data = [] | ||
this.isDeep = isDeep | ||
} | ||
Bucket.prototype.add = function (set) { | ||
genKeys(set.pattern).forEach(addPatterns, this) | ||
this.data.push(set) | ||
if (this.isDeep) { | ||
this.data.sort(deepSort) | ||
} | ||
return this | ||
} | ||
function addPatterns (toAdd) { | ||
this.filter.add(toAdd) | ||
} | ||
Bucket.prototype.remove = function (pattern, payload) { | ||
var foundPattern = false | ||
var data = this.data | ||
for (var i = 0; i < data.length; i++) { | ||
if (deepMatch(pattern, data[i].pattern)) { | ||
if (payload === null || payload === data[i].payload) { | ||
data.splice(i, 1) | ||
foundPattern = true | ||
// to remove all occurences | ||
this.remove(pattern, payload) | ||
break | ||
} | ||
} | ||
} | ||
return foundPattern | ||
} | ||
Bucket.prototype.forEach = function (func, that) { | ||
this.data.forEach(func, that) | ||
return this | ||
} | ||
module.exports = Bucket |
'use strict' | ||
function onlyRegex (pattern) { | ||
var match = false | ||
var match = true | ||
@@ -9,3 +9,3 @@ for (var key in pattern) { | ||
match = true | ||
} else if (match) { | ||
} else if (typeof pattern[key] !== 'object') { | ||
match = false | ||
@@ -12,0 +12,0 @@ break |
{ | ||
"name": "bloomrun", | ||
"version": "2.2.1", | ||
"version": "2.2.2", | ||
"description": "JS object pattern matching, powered by bloom filters", | ||
@@ -5,0 +5,0 @@ "main": "bloomrun.js", |
18
test.js
@@ -536,1 +536,19 @@ 'use strict' | ||
}) | ||
test('recursive depth support, no other keys', function (t) { | ||
t.plan(1) | ||
var instance = bloomrun({ indexing: 'depth' }) | ||
var pattern1 = { some: { key: 'value' } } | ||
var pattern2 = { some: { key: 'value', a: 'b' } } | ||
function payloadOne () { } | ||
function payloadTwo () { } | ||
instance.add(pattern1, payloadOne) | ||
instance.add(pattern2, payloadTwo) | ||
t.equal(instance.lookup({ | ||
some: { key: 'value', a: 'b', c: 'd' } | ||
}), payloadTwo) | ||
}) |
48129
835