Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coalescing imports/exports w/o Repeating Yourself (too much)
const Redis = require('ioredis')
const services = require('./services')
const redis = new Redis()
;(() => {
const oneInstance = new services.One()
const one = oneInstance.getNumberOne()
services.two.validate(one)
const two = services.two.calculate(one)
const three = services.three(redis)
three.setNumber('TWO', two)
})()
module.exports = {
one: class One {
getNumberOne() {
return 1
}
}
}
module.exports = {
three: (redis) => {
return {
setNumber: (key, data) => {
if (typeof data === 'number') {
redis.set(key, data)
}
}
}
}
}
module.exports = {
two: {
validate: function validateTwoInput(input) {
if (typeof input !== 'number') throw new Error('Input must be a number!')
}
calculate: (input) => {
return 1 + input
}
}
}
module.exports = {
...require('lib/src/one'),
...require('lib/src/two'),
...require('lib/src/three'),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment