Skip to content

Commit

Permalink
fix(iteration): fix and test iteration of opts.other keys
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Aug 16, 2018
1 parent 974e879 commit 7a76217
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 5 additions & 3 deletions index.js
Expand Up @@ -17,7 +17,7 @@ class FiggyPudding {
}
}
})
this.__opts = opts || (() => false)
this.__opts = opts || {}
this.__providers = reverse((providers || []).filter(
x => x != null && typeof x === 'object'
))
Expand All @@ -43,12 +43,14 @@ class FiggyPudding {
for (let key of Object.keys(this.__specs)) {
yield [key, this.get(key)]
}
const matcher = this.__opts.other || _matcher
const matcher = _matcher || this.__opts.other
if (matcher) {
const seen = new Set()
for (let p of this.__providers) {
const iter = p.entries ? p.entries(matcher) : Object.entries(p)
for (let [key, val] of iter) {
if (matcher(key)) {
if (matcher(key) && !seen.has(key)) {
seen.add(key)
yield [key, val]
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/iteration.js
Expand Up @@ -89,3 +89,28 @@ test('values', t => {
], '.values() iterates over values')
t.done()
})

test('opts.other iteration', t => {
const testOpts = puddin({
a: {}
}, {
other (key) { return /^special-/.test(key) }
})
const arr = []
const opts = testOpts({
'special-a': 3,
a: 1,
b: 2,
'special-b': 4,
'a-special': 5
})
for (let [key, value] of opts.entries()) {
arr.push([key, value])
}
t.deepEqual(arr, [
['a', 1],
['special-a', 3],
['special-b', 4]
], 'iterates over opts.other keys after primary keys')
t.done()
})

0 comments on commit 7a76217

Please sign in to comment.