Skip to content

Instantly share code, notes, and snippets.

@thomasboyt
Created February 1, 2016 17:27
Show Gist options
  • Save thomasboyt/501d1384f1a99c5b5b14 to your computer and use it in GitHub Desktop.
Save thomasboyt/501d1384f1a99c5b5b14 to your computer and use it in GitHub Desktop.
/*
* 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