Last active
November 16, 2017 19:12
-
-
Save sammoore/7e8b080258a664bdb171c37d768c2078 to your computer and use it in GitHub Desktop.
A fun riff on the CommonJS require function with an expression-like syntax via template-literal tagging.
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
'use strict'; | |
/** | |
* Example Usage: | |
* | |
* const b = 'b'; | |
* const r = 'r'; | |
* const foo = include `${b}a${r}`; | |
*/ | |
module.exports = function include(strings, ...expressions) { | |
const ref = expressions.reduce((out, expr, idx) => { | |
return `${out}${expr}${strings[idx + 1]}`; | |
}); | |
return require(ref); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment