Last active
August 15, 2018 10:40
-
-
Save davidsharp/9cd926c0a64f4eee0e6350b0cdaa8ed2 to your computer and use it in GitHub Desktop.
Requiem - a function for requiring multiple Node modules, designed for destructuring
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
/* 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}),{}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.