Skip to content

Instantly share code, notes, and snippets.

@cdanyl
cdanyl / Lenses_es6.js
Last active February 21, 2017 15:22
Functional Lenses in es6
const user = {
id: 1,
name: 'foo',
company: {
id: 12,
name: 'bar',
address: {
street: 'randomstreet',
}
},
@cdanyl
cdanyl / generateReportCard.js
Created February 10, 2017 11:16
Decorators pattern
// When a process has side effects that need to be executed only in certain situations,
// this functionality can be layered onto an existing operation using Decorators.
// A Decorator takes an object and wraps auxiliary functionality around it,
// letting you add on what you need when you need it and keeping the core procedure untouched.
// Let’s imagine a Service Object that generates report cards for each student in a class.
// A teacher can generate these report cards at any point, but there are some situations where those report cards should be mailed to the students’ parent.
// One way to address this would be to have two separate Service Objects,
// GenerateReportCards and GenerateReportCardsAndEmailParent,
@cdanyl
cdanyl / eligibleForSportsEnrollmentPolicy.js
Created February 10, 2017 11:12
Policy Objects pattern
// Helmkamp describes the potential overlap in concept between a Policy Object and a Query Object or a Service Object.
// He writes, “Policy Objects are similar to Service Objects, but I use the term ‘Service Object’ for write operations and 'Policy Object’ for reads.
// They are also similar to Query Objects, but Query Objects focus on executing SQL to return a result set, whereas Policy Objects operate on domain models already loaded into memory.”
// Let’s imagine a collection of students that comprise the entire freshman student body.
// We want to filter out all students that are eligible to register for sports, which we define with these rules:
// Not suspended
// Not expelled
// Are passing all classes
@cdanyl
cdanyl / queryObjects2.js
Created February 10, 2017 11:08
Query Objects pattern
// Query objects provide a nice tool for extracting query logic and associated operations into a contained module,
// pulling the logic out into a more maintainable and readable structure,
// while also providing a very readable API where the query object is used.
// Let’s imagine an API endpoint that returns a JSON representation of all students that are currently passing.
// Without using Query Objects, we might have a function in an API controller method or a Service Object that could look like this
// (note the user of DetermineStudentPassingStatus, the example from the Service Objects post):
var MongoClient = require('mongodb').MongoClient
@cdanyl
cdanyl / formObjects1.js
Created February 10, 2017 11:05
Form Objects pattern
// A Form Object can encapsulate all associated logic into a single object, keeping it focused, isolated, and easy to test.
// You may have a sign up form that creates an account, so an associated Form Object could handle the following logic:
// Make sure all fields that are required are present
// Make sure all values are valid
// Persist the data to the database
// Provide success or error feedback to the user
// Let’s imagine a teacher is registering new students for the school year.
// The application can hand the form data off to the Form Object for handling all aspects of its processing flow:
@cdanyl
cdanyl / valueObject.js
Last active May 28, 2018 11:42
Value Objects pattern
// Value Objects as “…simple objects whose equality is dependent on their value rather than an identity.”
// Because JavaScript adheres to the “pass-by-reference” principle for all JavaScript objects,
// there are no native examples of this in ECMAScript 5 or even Harmony.
var _ = require('underscore');
var Grade = function(percentage) {
this.percentage = percentage;
this.grade = this.grade(percentage);
};
@cdanyl
cdanyl / serviceObject.js
Last active May 28, 2018 11:43
Service Objects pattern are objects that perform a discrete operation or action.
// Service Objects can be an extremely valuable tool in cleaning up and refactoring your code.
// Isolating actions keeps logic clean, orderly, easy to test, and ultimately leads to a more maintainable code base.
var _ = require('underscore');
/**
* For a given student, determine whether or not they are passing
* @constructor
* @param {Object} student - The student that being evaluated.
*/
// Composition Example
// http://codepen.io/ericelliott/pen/XXzadQ?editors=001
// https://gist.github.com/ericelliott/fed0fd7a0d3388b06402
const distortion = { distortion: 1 };
const volume = { volume: 1 };
const cabinet = { cabinet: 'maple' };
const lowCut = { lowCut: 1 };
const inputLevel = { inputLevel: 1 };
@cdanyl
cdanyl / stateMachine.es6
Last active May 28, 2018 11:44
Simple State Machine es6
const machine = {
init(context) {
this.context = context;
this._stateTransitions = {};
this._stateTransitionsAny = {};
this._defaultTransition = null;
this._initialState = null;
this._currentState = null;
return this;
@cdanyl
cdanyl / scratch_7.es6
Created February 6, 2017 14:09
factory function and composition in es6
/* basic factorys */
const makeAnimal = state => ({
eat: () => `${state.name} eat`,
breathe: () => `${state.name} breathe`
});
const makeMachine = state => ({
reload: () => `${state.name} reload`,
turnOn: () => `${state.name} turned on`,