Created
June 25, 2019 19:54
-
-
Save grimen/6d206632ca61f7d357af1537ec9946d9 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
const createFor = (exported = {}) => { | |
return (defaults = {}) => { | |
const functions = {} | |
exported['defaults'] = {...defaults} | |
for (const [key, value] of Object.entries(exported)) { | |
if (typeof value === 'function') { | |
functions[key] = exported[key] | |
exported[key] = (...args) => { | |
let options = args.pop() | |
if (typeof options !== 'object' || Array.isArray(options) || !args.length) { | |
args.push(options) | |
options = {} | |
} | |
options = { | |
...defaults, | |
...options, | |
} | |
args.push(options) | |
return functions[key](...args) | |
} | |
} | |
} | |
return exported | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment