Here is a simple example of what I'm trying to achieve:
foo.js :
module.exports.one = function(params) { */ stuff */ }
bar.js :
module.exports.two = function(params) { */ stuff */ }
stuff.js:
const foo = require('Path/foo');
const bar = require('Path/bar');
I want to do :
otherFile.js:
stuff = require('Path/stuff');
stuff.one(params);
stuff.two(params);
I do not want to do [in stuff.js]
module.exports = {
one : foo.one,
two: bar.two
}
The solution I came with is :
const files = ['path/foo', 'path/bar']
module.exports = files
.map(f => require(f))
.map(f => Object.keys(f).map(e => ({ [e]: f[e] })))
.reduce((a, b) => a.concat(b), [])
.reduce((a, b) => Object.assign(a, b), {})
or uglier/shorter :
module.exports = files
.map(f => require(f))
.reduce((a, b) => Object.assign(a, ...Object.keys(b).map(e => ({ [e]: b[e] }))));
It feels "hackish".
Is there a cleaner way to do this ?