Skip to content

Commit

Permalink
List both patterns and payloads at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Oct 11, 2016
1 parent 7007fb6 commit bea3f34
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 8 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,20 @@ are no more.
If `obj` is null, all patterns/payload will be returned.

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
}
```

-------------------------------------------------------
<a name="list"></a>
Expand All @@ -136,8 +148,20 @@ this will be returned instead of the pattern.
If `obj` is null, all patterns/payload will be returned.

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
}
```

-------------------------------------------------------

Expand Down
15 changes: 11 additions & 4 deletions lib/iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ function Iterator (parent, obj, opts) {

this.parent = parent
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

if (obj) {
if (onlyRegex(obj)) {
Expand Down Expand Up @@ -41,10 +43,15 @@ Iterator.prototype.next = function () {
var current = this.buckets[this.i].data[this.k]

if (!this.pattern || deepMatch(current.pattern, this.pattern)) {
if (this.onlyPatterns) {
match = current.pattern
} else {
if (this.onlyPayloads) {
match = current.payload
} else if (this.onlyPatterns) {
match = current.pattern
} else if (this.patternsAndPayloads) {
match = {
pattern: current.pattern,
payload: current.payload
}
}
}

Expand Down
21 changes: 21 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,24 @@ test('recursive depth support, no other keys', function (t) {
some: { key: 'value', a: 'b', c: 'd' }
}), payloadTwo)
})

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
}])
})

0 comments on commit bea3f34

Please sign in to comment.