Skip to content

Instantly share code, notes, and snippets.

View cyan33's full-sized avatar
🖤
EqualityMatters

Chang Yan cyan33

🖤
EqualityMatters
View GitHub Profile
@cyan33
cyan33 / minifyCSS.js
Created December 29, 2017 13:21
minifyCSS
const minifyCSS = (css) => css.replace(/\n/g, '').replace(/\s\s+/g, ' ')
@cyan33
cyan33 / connect.js
Created December 27, 2017 14:08
connect v4.2.0
/*
* author: Dan Abromov
*/
const { Component, createElement } = require('react')
const storeShape = require('../utils/storeShape')
const shallowEqual = require('../utils/shallowEqual')
const wrapActionCreators = require('../utils/wrapActionCreators')
const isPlainObject = require('lodash/isPlainObject')
const hoistStatics = require('hoist-non-react-statics')
@cyan33
cyan33 / gameloop.js
Created December 6, 2017 21:09
gameloop flow
setInterval(function gameloop() {
update();
render();
}, 30);
@cyan33
cyan33 / ts-webpack.config.js
Created November 21, 2017 17:15
ts webpack
module.exports = {
entry: "./src/index.tsx",
output: {
filename: "bundle.js"
},
resolve: {
// Add '.ts' and '.tsx' as a resolvable extension.
extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
},
module: {
@cyan33
cyan33 / fibonacci.js
Created November 15, 2017 17:05
generator based fibonacci
function *gen() {
var f1 = 0;
var f2 = 1;
yield f1;
yield f2;
while (true) {
yield f1 + f2;
[f1, f2] = [f2, f1 + f2];
@cyan33
cyan33 / async-wrapper-example.js
Created November 14, 2017 17:47
How the async wrapper is used
var login = async(function* (username, password, session) {
var user = yield getUser(username);
var hash = yield crypto.hashAsync(password + user.salt);
if (user.hash !== hash) {
throw new Error('Incorrect password');
}
session.setUser(user);
});
@cyan33
cyan33 / async-generator.js
Created November 14, 2017 17:41
async / await are just the syntax sugar of generators
async function addTwoResult(a, b) {
var first = await resolveAfter2Seconds(a);
var second = await resolveAfter2Seconds(b);
return first + second;
}
// is equivalent to
function *addTwoResult(a, b) {
var first = yield resolveAfter2Seconds(a);
@cyan33
cyan33 / loop-generator.js
Created November 14, 2017 05:05
use generator function to do expensive calculation
function *count() {
var x = 1;
while (true) {
yield x++;
}
}
var gen = count()
// you could infinitely call gen.next()
@cyan33
cyan33 / generator-example.js
Last active November 14, 2017 17:35
basic generator example
function *makeGenerator() {
yield 1;
yield 3;
yield 5;
}
var gen = makeGenerator();
gen.next() // { done: false, value: 1 }
gen.next() // { done: false, value: 3 }
gen.next() // { done: false, value: 5 }
@cyan33
cyan33 / async-await-example.js
Created November 14, 2017 04:43
Basic example about how to use async and await in ES7
function resolveAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
});
}
async function addTwoResult(a, b) {
var first = await resolveAfter2Seconds(a);
var second = await resolveAfter2Seconds(b);