Skip to content

Instantly share code, notes, and snippets.

@JustinDFuller
Created October 14, 2018 00:12
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 JustinDFuller/0aea46495d06912acacc1508c3ecbcfe to your computer and use it in GitHub Desktop.
Save JustinDFuller/0aea46495d06912acacc1508c3ecbcfe to your computer and use it in GitHub Desktop.
Chain of dependency inversion
/* json.js */
export default function ({ readFileAsync, writeFileAsync }) {
return {
readJsonFile(fileName) {
return readFileAsync(`${fileName}.json`).then(JSON.parse)
},
writeJsonFile(filePath, fileContent) {
return writeFileAsync(filePath, JSON.stringify(fileContent))
}
}
}
/* content.js */
export default function ({ readJsonFile, writeJsonFile }) {
return {
getContent(contentName) {
// business logic goes here.
return readJsonFile(contentName)
},
writeContent(contentName, contentText) {
// business logic goes here
return writeJsonFile(contentName, contentText)
}
}
}
/* index.js where the app starts */
import fs from 'fs-extra-promise'
import jsonInterface from './json'
import contentInterface from './content'
const json = jsonInterface(fs)
const content = contentInterface(json)
// content can be used by an http server
// or just exported if this is a library
export default content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment