Created
February 1, 2016 17:27
-
-
Save thomasboyt/501d1384f1a99c5b5b14 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* makes it easy to check an options object for required named params, e.g. | |
* | |
* export default function getBaseVolumeCurve(points, opts) { | |
* const {time, middleRange, lookbackPeriodDays, volPeriodDays} = ensureFields(opts); | |
* // ... | |
* } | |
* | |
* if the key you're getting doesn't exist, it will throw an error | |
*/ | |
export default function ensureFields(obj) { | |
const handler = { | |
get: (receiver, name) => { | |
if (receiver[name] === undefined) { | |
throw new Error(`Missing required argument ${name}`); | |
} | |
return receiver[name]; | |
} | |
}; | |
return new Proxy(obj, handler); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment