Skip to content

Instantly share code, notes, and snippets.

View JustinDFuller's full-sized avatar
🧐

Justin Fuller JustinDFuller

🧐
View GitHub Profile
import orm from 'my_favorite_database_library'
export const models = {
user: orm.define({
id: orm.PRIMARY_KEY,
email: orm.STRING(255)
})
}
import { models } from './models'
export const controllers = {
user: {
get({ email }) {
return models.user.get({
where: {
email
},
limit: 1
@JustinDFuller
JustinDFuller / routes.js
Created July 21, 2019 00:39
Route Handlers
import { controllers } from './controllers'
export const routes = {
user: {
async get(request, response) {
response.json(await controllers.user.get(request.query))
}
}
}
import express from 'express'
import { routes } from './routes'
const server = express()
/**
* Implement routes
*
* server.get('/user', routes.user.get)
@JustinDFuller
JustinDFuller / extensible.js
Created January 17, 2019 01:39
An example of an extensible interface
// not extensible
function getUser() {
return {
name: 'Justin',
email: 'justinfuller@email.com'
}
}
// Extensible
@JustinDFuller
JustinDFuller / dependency-inversion-chain.js
Created October 14, 2018 00:12
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))
}
@JustinDFuller
JustinDFuller / react-component.d.ts
Created October 13, 2018 19:16
react component interface
interface ComponentLifecycle {
constructor(props: Object);
componentDidMount?(): void;
shouldComponentUpdate?(nextProps: Object, nextState: Object, nextContext: any): boolean;
componentWillUnmount?(): void;
componentDidCatch?(error: Error, errorInfo: ErrorInfo): void;
setState(
state: ((prevState: Object, props: Object) => Object,
callback?: () => void
): void;
@JustinDFuller
JustinDFuller / dependencies.js
Created October 13, 2018 18:18
Dependency Inversion
/* This is my file I'll be testing foo.js */
export default function ({ readFileAsync }) {
return {
readJsonFile (filePath) {
return readFileAsync(filePath).then(JSON.parse)
}
}
}
@JustinDFuller
JustinDFuller / mocking.js
Created October 13, 2018 18:13
Mocking and stubbing
/* This is my file I'll be testing foo.js */
import fs from 'fs'
import { promisify } from 'util'
const readFileAsync = promisify(fs.readFile)
export function readJsonFile (filePath) {
return readFileAsync(filePath).then(JSON.parse)
}
@JustinDFuller
JustinDFuller / dependencies.js
Created October 7, 2018 17:42
Accept an interface of dependencies
/**
* Normally you might do:
* import fs from 'fs-extra-promise'
* but here we will allow the user of this file to provide the fs module
*/
export default function ({ fs }) {
return function(fileName) {
return fs.readFileAsync(fileName, 'utf8')
}
}