Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
fix(get): make provider lookup order like Object.assign
Browse files Browse the repository at this point in the history
BREAKING CHANGE: shadow order for properties in providers is reversed
  • Loading branch information
zkat committed Apr 6, 2018
1 parent 7a40563 commit 33ff89b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions index.js
Expand Up @@ -4,7 +4,9 @@ class FiggyPudding {
constructor (specs, opts, providers) {
this.specs = specs || {}
this.opts = opts || (() => false)
this.providers = providers
this.providers = reverse((providers || []).filter(
x => x != null && typeof x === 'object'
))
this.isFiggyPudding = true
}
get (key) {
Expand Down Expand Up @@ -49,12 +51,18 @@ function pudGet (pud, key, validate) {

module.exports = figgyPudding
function figgyPudding (specs, opts) {
function factory () {
function factory (...providers) {
return new FiggyPudding(
specs,
opts,
[].slice.call(arguments).filter(x => x != null && typeof x === 'object')
providers
)
}
return factory
}

function reverse (arr) {
const ret = []
arr.forEach(x => ret.unshift(x))
return ret
}
2 changes: 1 addition & 1 deletion test/index.js
Expand Up @@ -93,7 +93,7 @@ test('multiple providers', t => {
b: {},
c: {}
})
const opts = testOpts({a: 1}, {a: 2, b: 2}, {a: 3, b: 3, c: 3})
const opts = testOpts({a: 3, b: 3, c: 3}, {a: 2, b: 2}, {a: 1})
t.equal(opts.get('a'), 1, 'a from first provider')
t.equal(opts.get('b'), 2, 'b from second provider')
t.equal(opts.get('c'), 3, 'c from third provider')
Expand Down

0 comments on commit 33ff89b

Please sign in to comment.