Created
October 11, 2017 08:49
-
-
Save rayddteam/cba9cee46e27830bfad6ab7cf9a97fd5 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
// (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