Skip to content

Instantly share code, notes, and snippets.

View hkjpotato's full-sized avatar

KJ(Kaijie) Huang hkjpotato

  • atl/nyc/virtual
View GitHub Profile
function connectHOC(mapStateToProps, mapDispatchToProps) {
//wrapWithConnect is the actual HOC
return function wrapWithConnect(WrappedComponent) {
//Connect is the actual Container Component
class Connect extends Component {
componentDidMount() {
//subscribe to the store
store.subscribe(this.onStateChange.bind(this)
}
onStateChange() {
export function connect(
mapStateToProps,
mapDispatchToProps
) {
//connectHOC is a function that return the Higher Order Component with connect to the store
return connectHOC(mapStateToProps, mapDispatchToProps);
}
function makeCompositeSelector(dependencies, transformation) {
//make the transformation memorizable
var memorizedTransformation = memorize(transformation)
//memorize the return selector itself so no need to traverse the dependencies if the input is exact the same
return memorize(function (input) {
var params = [];
dependencies.forEach(function(func) {
params.push(func(input));
});
function makeCompositeSelector(dependencies, transformation) {
return function (input) {
var params = [];
//loop through the dependencies and apply the input to get the corresponding param
dependencies.forEach(function(func) {
params.push(func(input));
});
//use the generated params as input for the transformation
return transformation.apply(null, params);
}
// this HOF return a memorized func, it has a scope for caching arguments & output
function memorize(func) {
let lastInput = null;
let lastOuput = null;
return function() {
if (!isArgumentsEqual(lastInput, arguments)) {
lastOuput = func.apply(null, arguments);
}
lastInput = arguments;
return lastOuput;
@hkjpotato
hkjpotato / graph.json
Last active June 12, 2017 19:56
dynamic force layout with multiple customization methods
{"nodes":[{"x":444,"y":275},{"x":378,"y":324},{"x":478,"y":278},{"x":471,"y":256},{"x":382,"y":269},{"x":371,"y":247},{"x":359,"y":276},{"x":364,"y":302},{"x":400,"y":330},{"x":388,"y":298},{"x":524,"y":296},{"x":570,"y":243},{"x":552,"y":159},{"x":502,"y":287},{"x":511,"y":313},{"x":513,"y":265},{"x":602,"y":132},{"x":610,"y":90},{"x":592,"y":91},{"x":575,"y":89},{"x":607,"y":73},{"x":591,"y":68},{"x":574,"y":73},{"x":589,"y":149},{"x":620,"y":205},{"x":621,"y":230},{"x":589,"y":234},{"x":602,"y":223},{"x":548,"y":188},{"x":532,"y":196},{"x":548,"y":114},{"x":575,"y":174},{"x":497,"y":250},{"x":576,"y":196},{"x":504,"y":201},{"x":494,"y":186},{"x":482,"y":199},{"x":505,"y":219},{"x":486,"y":216},{"x":590,"y":306},{"x":677,"y":169},{"x":657,"y":258},{"x":667,"y":205},{"x":552,"y":227},{"x":518,"y":173},{"x":473,"y":125},{"x":796,"y":260},{"x":731,"y":272},{"x":642,"y":288},{"x":576,"y":269},{"x":605,"y":187},{"x":559,"y":289},{"x":544,"y":356},{"x":505,"y":365},{"x":579,"y":289},{"x":619,"y":282},{"x":574,"y"
@hkjpotato
hkjpotato / README.md
Last active June 3, 2017 19:46
hello gistup

From HERE:

ABCD or EFGH hahahaha yoyo … .

XXXX.

var fn = function() {
console.log(arguments);
return fn.bind(null, ...arguments);
//without es6 we can write as:
// return Function.prototype.bind.apply(fn, [null].concat(
// Array.prototype.slice.call(arguments)
// ));
}
fb = fn(1); //[1]
function magician(targetfn) {
var numOfArgs = targetfn.length;
return function fn() {
if (arguments.length < numOfArgs) {
return fn.bind(null, ...arguments);
} else {
return targetfn.apply(null, arguments);
}
}
}
function magician (targetfn) {
var numOfArgs = targetfn.length;
if (arguments.length - 1 < numOfArgs) {
return magician.bind(null, ...arguments);
} else {
return targetfn.apply(null, Array.prototype.slice.call(arguments, 1));
}
}