Last active
October 30, 2021 16:22
-
-
Save jberger/6d7302091f0da13060eca18d2f5aa28d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function defaultObject(defaultValue) { | |
return new Proxy({}, { | |
get: function(target, name) { | |
return target.hasOwnProperty(name) ? target[name] : defaultValue; | |
}, | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t.test('defaultObject', t => { | |
const foo = util.defaultObject('foo'); | |
foo.bar = 'bar'; | |
t.equal(foo.foo, 'foo'); | |
t.equal(foo.bar, 'bar'); | |
t.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment