Skip to content

Instantly share code, notes, and snippets.

@grimen
Created June 25, 2019 19:54
Show Gist options
  • Save grimen/6d206632ca61f7d357af1537ec9946d9 to your computer and use it in GitHub Desktop.
Save grimen/6d206632ca61f7d357af1537ec9946d9 to your computer and use it in GitHub Desktop.
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