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 / 0_reuse_code.js
Created April 15, 2016 09:43
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@cyan33
cyan33 / cb.js
Last active July 18, 2017 14:08
Use `localStorage` to Save Username and Pasword
// this assumes that you have jquery, or any ajax utils like `isomorphic-fetch`
function storeDataCb(username, password) {
if (!window.localStorage) return
localStorage.username = username
localStorage.password = password
}
function logIn() {
@cyan33
cyan33 / compose.js
Created October 4, 2017 01:15
chain functions compose
/*
* I got this inspiration from the source code of redux.
* Essentially, compose(f, g, h) equals to (arg) => f(g(h(arg)))
*/
function compose(...funcs) {
if (!funcs.length) return (arg) => arg
if (funcs.length === 1) return funcs[0]
return funcs.reduce((a, b) => (...args) => a(b(...args)))
@cyan33
cyan33 / simple-promise.js
Created October 14, 2017 00:08
A simple promise implementation
// https://medium.com/gitconnected/understand-javascript-promises-by-building-a-promise-from-scratch-84c0fd855720
class SimplePromise {
constructor(executionFunction) {
this.promiseChain = [];
this.handleError = () => {};
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
@cyan33
cyan33 / optimizeCb.js
Created October 29, 2017 00:59
the optimize callback method in underscore.js
function optimizeCb(func, context, argCount) {
if (!context) return func
switch (argCount ? argCount : 3) {
case 1: return function(val) {
return func.call(context, val)
}
case 2: return function(val, other) {
return func.call(context, val, other)
}
// a code snippets from underscore.js for type checking in JavaScript
_.each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp', 'Error'], function(name) {
_['is' + name] = function(obj) {
return toString.call(obj) === '[object ' + name + ']';
};
});
function isObject(item) {
const type = typeof item
@cyan33
cyan33 / extend.js
Created October 29, 2017 02:59
the `extend` function in underscore
// in the namespace of `_`
// The simplest extend
function simpleExtend(obj, src) {
for (let key in src) {
obj[key] = src[key]
}
return obj
}
// the two different type of configuration of key assigners
@cyan33
cyan33 / gist:fab86a373b8c4dee16ecdb4315b9c7f6
Created May 11, 2016 10:27
The difference between 'undefined' and 'null' in JavaScript
In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:
var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
null is an assignment value. It can be assigned to a variable as a representation of no value:
var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object
@cyan33
cyan33 / promisify.js
Last active November 3, 2017 16:06
tiny promisify
function promisify(fn) {
return function(...args) {
return new Promise((res, rej) => {
fn(...args, function() {
res(arguments)
})
})
}
}
@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);