Skip to content

Instantly share code, notes, and snippets.

@sammoore
Last active November 16, 2017 19:12
Show Gist options
  • Save sammoore/7e8b080258a664bdb171c37d768c2078 to your computer and use it in GitHub Desktop.
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.
'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