Skip to content

Instantly share code, notes, and snippets.

@heisian
Created August 22, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heisian/8ccbaf928dfb65e756fed7b39ce0c44d to your computer and use it in GitHub Desktop.
Save heisian/8ccbaf928dfb65e756fed7b39ce0c44d to your computer and use it in GitHub Desktop.
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