Skip to content

Instantly share code, notes, and snippets.

View gmmorris's full-sized avatar

Gidi Meir Morris gmmorris

View GitHub Profile
@gmmorris
gmmorris / extend_all_method_composer.js
Created November 1, 2015 16:46
An example of a All Method Constructor Composer for my How to Grok a Higher Order Class article
export default function(methodComposer) {
return ClassToCompose => {
class ComposedClass extends ClassToCompose {
constructor() {
super(...arguments);
}
}
const baseProto = ClassToCompose.prototype;
@gmmorris
gmmorris / functional_higher_order_class.js
Created November 1, 2015 20:22
An example of a Constructor Prototype Composer in a more functional syntax for my How to Grok a Higher Order Class article
/*
* A Higher Order Function which takes a Class definition and returns a new function.
*
* @param {Function} ClassToCompose The class definition we wish to analyze
* @return {Function} A function which takes a property name and return a boolean specifying whether
* the class has a method with that name which is composable
*/
function isComposablePropertyOf(ClassToCompose) {
const proto = ClassToCompose.prototype;
return propertyName => {
@gmmorris
gmmorris / destructured arguments
Created February 3, 2016 14:56
A small example of using destructuring for named function arguments and orderless optional arguments. This allows you to create a single function with different argument options without having to maintain complex argument ordering
const isFunction = fn => typeof fn === 'function'
const isNumber = n => typeof n === 'number' && !isNaN(n)
function createFunctionPipe({ interval, fn, args = [] } = {}) {
if(!isFunction(fn)){
throw Error('Invalid callback (fn) argument');
}
if(isNumber(interval)) {
return setTimeout(() => fn(...args), interval);
@gmmorris
gmmorris / DarthVaderUncaughtTypeError.js
Last active February 7, 2016 21:57
Code example for causing an undefined property access error
const DarthVader = {
name : 'Anakin',
mother : {
name : 'Shmi'
}
}
function getFatherName(person) {
return person.father.name
}
@gmmorris
gmmorris / HelperFunctionsForSafeProxyAccess.js
Last active February 7, 2016 21:29
Helper functions for the Safe Access Proxy article
const isObject = obj => obj && typeof obj === 'object';
const hasKey = (obj, key) => key in obj;
const Undefined = new Proxy({}, {
get: function(target, name){
return Undefined;
}
});
const either = (val,fallback) => (val === Undefined? fallback : val);
@gmmorris
gmmorris / SafeAccessFunction.js
Created February 7, 2016 21:35
Implementation for the Safe Access Proxy article
function safe(obj) {
return new Proxy(obj, {
get: function(target, name){
return hasKey(target, name) ?
(isObject(target[name]) ? safe(target[name]) : target[name]) : Undefined;
}
});
}
@gmmorris
gmmorris / SafeDarthVader.js
Last active August 3, 2016 21:29
example of usage for the Safe Access Proxy article
const mySafeObj = safe({
name : 'Anakin',
mother : {
name : 'Shmi'
}
});
console.log(mySafeObj.name); // returns "Anakin"
@gmmorris
gmmorris / StringBasedAccess.js
Created February 7, 2016 22:03
Example of safe access using a string for the Safe Access Proxy article
const DarthVader = {
name : 'Anakin',
mother : {
name : 'Shmi'
}
};
function deepAccessUsingString(obj, key){
return key.split('.').reduce((nestedObject, key) => {
if(nestedObject && key in nestedObject) {
@gmmorris
gmmorris / WorkingExampleOfSafeFatherNameAccess.js
Last active January 6, 2017 12:01
Example of how to fix the original function using the safe function
const safeDarthVader = safe({
name : 'Anakin',
mother : {
name : 'Shmi'
}
});
function getFatherName(person) {
return person.father.name
@gmmorris
gmmorris / MyComponent.js
Created August 1, 2016 00:00
An example for a self "rerendering" JSON component which allows you to build JSON objects which could update themselves in the component tree
function compare(left, right){
return left.val === right.val;
}
function render(props){
const objVal = {
...props,
setProps: (newProps) => {
if(!compare(props, newProps)) {
this(newProps);