Skip to content

Instantly share code, notes, and snippets.

@rayddteam
Created October 11, 2017 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayddteam/cba9cee46e27830bfad6ab7cf9a97fd5 to your computer and use it in GitHub Desktop.
Save rayddteam/cba9cee46e27830bfad6ab7cf9a97fd5 to your computer and use it in GitHub Desktop.
// (c) Eric Elliott
const fn = (fn, {required = []}) => (params = {}) => {
const missing = required.filter(param => !(param in params));
if (missing.length) {
throw new Error(`${ fn.name }() Missing required parameter(s):
${ missing.join(', ') }`);
}
return fn(params);
};
const createEmployee = fn(
({
name = '',
hireDate = Date.now(),
title = 'Worker Drone'
} = {}) => ({
name, hireDate, title
}),
{
required: ['name']
}
);
console.log(createEmployee({ name: 'foo' })); // works
createEmployee(); // createEmployee() Missing required parameter(s): name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment