Skip to content

Instantly share code, notes, and snippets.

View jeffijoe's full-sized avatar
:shipit:
Netting them dots

Jeff Hansen jeffijoe

:shipit:
Netting them dots
View GitHub Profile
@jeffijoe
jeffijoe / todosRepository.js
Last active April 30, 2021 19:43
Snippet for my Medium article
// Let's do an in-memory implementation for now.
const _todos = []
export default class TodosRepository {
// Marking all methods async makes them return promises!
async find(query) {
const filtered = _todos.filter((todo) => {
// Check the user ID
if (todo.userId !== query.userId)
return false
@jeffijoe
jeffijoe / todosService.js
Last active September 6, 2016 10:14
Snippet for my Medium article
import assert from 'assert'
// Using object destructuring to make it look good.
export function makeTodosService({
// "repository" is a fancy term to descibe an object
// that is used to retrieve data from a datasource - the actual
// data source does not matter. Could be a database, a REST API,
// or some IoT things like sensors or whatever.
todosRepository,
// We also want info about the user that is using the service,
@jeffijoe
jeffijoe / di-test-example.js
Last active June 16, 2018 10:39
Snippet for my Medium article
describe('Todos Service', function () {
beforeEach(() {
subject = makeTodosService({
db: testDatabaseSomehow
})
})
it('works', async function () {
const todos = await subject.getTodos()
expect(todos.length).to.equal(3)
@jeffijoe
jeffijoe / di-todos-class-example.js
Created September 6, 2016 09:46
Snippet for my Medium article
export default class TodosService {
constructor({ db }) {
this.db = db
}
getTodos() {
return this.db.query('select * from todos')
}
}
@jeffijoe
jeffijoe / di-todos-example.js
Created September 6, 2016 09:45
Snippet for my Medium article
export default function makeTodosService ({ db }) {
return {
getTodos: () => {
return db.query('select * from todos')
}
}
}
@jeffijoe
jeffijoe / not-di.js
Created September 6, 2016 09:45
Snippet for my Medium article
import db from '../mydatabase'
export default {
getTodos: () => {
return db.query('select * from todos')
}
}
@jeffijoe
jeffijoe / express-di.js
Created September 6, 2016 09:42
Snippet for my Medium article
var express = require('express')
var app = express()
var session = require('express-session')
app.use(session({
store: require('connect-session-knex')()
}))
@jeffijoe
jeffijoe / compositionRoot.js
Created September 6, 2016 08:53
Code snippets for my Medium article.
const currentUser = {
id: 123,
name: 'Jeff'
}
const todosRepository = new TodosRepository()
const todosService = makeTodosService({
todosRepository,
currentUser
})
@jeffijoe
jeffijoe / ReflectionHelper.cs
Created June 24, 2015 07:08
Utility I use in my tests to steal someone's private property.
// SkyClip
// - ReflectionHelper.cs
// --------------------------------------------------------------------
// Author: Jeff Hansen <jeff@jeffijoe.com>
// Copyright (C) Jeff Hansen 2015. All rights reserved.
using System;
using System.Reflection;
namespace SkyClip.TestHelpers.Reflection
@jeffijoe
jeffijoe / bootstrap.onFieldValidatedHandler.js
Last active August 29, 2015 14:06
jQuery.Validator onFieldValidatedHandler
var setValidationStatusFor = function($field, isValid, errorMessage) {
var
$formgroup = $field.closest(".form-group"),
$helpBlock = $formgroup.find(".help-block");
if (isValid) {
if ($helpBlock.data().hasShownErrorBefore) {
$helpBlock.text($helpBlock.data().originalText);
}
$formgroup.removeClass("has-error");