Skip to content

Instantly share code, notes, and snippets.

@davidsharp
Last active August 15, 2018 10:40
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 davidsharp/9cd926c0a64f4eee0e6350b0cdaa8ed2 to your computer and use it in GitHub Desktop.
Save davidsharp/9cd926c0a64f4eee0e6350b0cdaa8ed2 to your computer and use it in GitHub Desktop.
Requiem - a function for requiring multiple Node modules, designed for destructuring
/* REQUIEM
** Usage example:
** const {fs,crypto,child_process:child} = requiem('fs','child_process','crypto')
** note: modules containing '-', etc should be wrapped in " or '
** you could also use this like so:
** const {'../example-module.js':myModule} = requiem('../example-module.js')
*/
const requiem = (...args) => args.reduce((o,c)=>({...o,[c]:typeof c==='string'?require(c):null}),{})
@davidsharp
Copy link
Author

In regards to the updated version, I opted for setting invalid things to null, because it seems preferable to undefined, equally I could not include them via .reduce((o,c)=>({...o,...(typeof c==='string'?{[c]:require(c)}:{})}),{}), which would give the same result as the old one, but null feels more useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment