Skip to content

Instantly share code, notes, and snippets.

View ZhihaoLau's full-sized avatar
💭
Meh

lzo ZhihaoLau

💭
Meh
View GitHub Profile
@ZhihaoLau
ZhihaoLau / createIterator.js
Last active October 5, 2016 16:14
createIterator - the es5 way
function createIterator(items) {
var i = 0;
return {
next: function() {
// NZakas' way is really straight forward what 'done' and 'value' is.
var done = (i >= items.length);
var value = done ? undefined : items[i++];
return {
done: done,
@ZhihaoLau
ZhihaoLau / settledPromise.js
Created September 8, 2016 11:48
Creating Settled Promise
let promise = Promise.resolve('hello');
promise.then(function(data) {
console.log(data);
});
// Or: ------------------------
let promise = Promise.reject('oops');
@ZhihaoLau
ZhihaoLau / unsettledPromise.js
Last active September 8, 2016 09:56
Creating Unsettled Promises
var fs = require('fs');
function readFile(filename) {
return new Promise(function(resolve, reject) {
fs.readFile(filename, {encoding: 'utf8'}, function(err, data) {
if (err) {
reject(err);
return;
}
resolve(data);
@ZhihaoLau
ZhihaoLau / AsyncSum.js
Created September 7, 2016 03:01
AsyncSum.js
function add(getX, getY, cb) {
var x, y;
getX(function(xVal) {
x = xVal;
if (y !== undefined) {
cb(x + y);
}
});
get(function(yVal) {
@ZhihaoLau
ZhihaoLau / safari_private_mode_isssue.js
Last active August 15, 2016 03:52
safari无痕浏览模式下,setItem抛出异常导致程序不能正常运行
// cause safari private mode do not support storage
if (typeof localStorage === 'object') {
try {
localStorage.setItem('localStorage', 1);
localStorage.removeItem('localStorage');
} catch (e) {
Storage.prototype._setItem = Storage.prototype.setItem;
Storage.prototype.setItem = function() {};
alert('你的浏览器似乎不支持本地缓存,如果你是Safari用户,请关闭“无痕浏览”模式');
@ZhihaoLau
ZhihaoLau / .gitignore
Last active January 20, 2017 09:55
generic .gitignore configuration
.DS_Store
*.log
dist
node_modules
.sass-cache
.svn
.gitmodules
@ZhihaoLau
ZhihaoLau / isEmpty.js
Created July 3, 2016 04:50
Check if object is empty
// pre es5
function isEmpty(myObject) {
for (var key in myObject) {
if (myObject.hasOwnProperty(key)) {
return false
}
}
return true
}
@ZhihaoLau
ZhihaoLau / combineReducers.js
Created June 5, 2016 07:44
implement Redux's combineReducers from scratch
// accept a array of reducers
const combineReducers = (reducers) => {
// returns a huge reducer, looks like below gist
return (state = {}, action) => {
return Object.keys(reducers).reduce(
(nextState, key) => {
nextState[key] = reducers[key](
state[key],
action
)
@ZhihaoLau
ZhihaoLau / createStore.js
Created June 3, 2016 17:22
implement createStore from scratch
const createStore = (reducer) => {
// only this place create the one and only state
let state
let listeners = []
const getState = () => state
const dispatch = (action) => {
// change state only by dispatching an action
state = reducer(state, action)
@ZhihaoLau
ZhihaoLau / cool_generator.js
Last active June 1, 2016 14:32
[ES6] Using generator with for ... of statement
function* fibonacci() {
let [prev, curr] = [0, 1];
// using deconstruct feature implement reducer, really cool...
for (;;) {
[prev, curr] = [curr, curr + prev];
yield curr;
}
}