This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function(methodComposer) { | |
return ClassToCompose => { | |
class ComposedClass extends ClassToCompose { | |
constructor() { | |
super(...arguments); | |
} | |
} | |
const baseProto = ClassToCompose.prototype; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DarthVader = { | |
name : 'Anakin', | |
mother : { | |
name : 'Shmi' | |
} | |
} | |
function getFatherName(person) { | |
return person.father.name | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function safe(obj) { | |
return new Proxy(obj, { | |
get: function(target, name){ | |
return hasKey(target, name) ? | |
(isObject(target[name]) ? safe(target[name]) : target[name]) : Undefined; | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mySafeObj = safe({ | |
name : 'Anakin', | |
mother : { | |
name : 'Shmi' | |
} | |
}); | |
console.log(mySafeObj.name); // returns "Anakin" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DarthVader = { | |
name : 'Anakin', | |
mother : { | |
name : 'Shmi' | |
} | |
}; | |
function deepAccessUsingString(obj, key){ | |
return key.split('.').reduce((nestedObject, key) => { | |
if(nestedObject && key in nestedObject) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const safeDarthVader = safe({ | |
name : 'Anakin', | |
mother : { | |
name : 'Shmi' | |
} | |
}); | |
function getFatherName(person) { | |
return person.father.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compare(left, right){ | |
return left.val === right.val; | |
} | |
function render(props){ | |
const objVal = { | |
...props, | |
setProps: (newProps) => { | |
if(!compare(props, newProps)) { | |
this(newProps); |