Skip to content

Instantly share code, notes, and snippets.

View ZhihaoLau's full-sized avatar
💭
Meh

lzo ZhihaoLau

💭
Meh
View GitHub Profile
@ZhihaoLau
ZhihaoLau / partial.js
Last active June 16, 2017 13:04
[FP] partial application
// Reference: https://github.com/getify/Functional-Light-JS/blob/master/ch3.md#some-now-some-later
function partial(fn, ...presetArgs) {
return function(...laterArgs) {
return fn(...presetArgs, ...laterArgs);
};
}
// // Or:
@ZhihaoLau
ZhihaoLau / TinyUILib-CheckList-Demo.jsx
Created April 29, 2017 16:37
TinyUILib-CheckList-Demo
<check-list
v-model="selected"
:options="options"
:toggleAllAble="true"
/>
@ZhihaoLau
ZhihaoLau / runGenerator.js
Created April 3, 2017 08:57
runGenerator module (issue: if generator defined in object as a properties, thorws error)
/**
* Check if `obj` is a generator.
*
* @param {Mixed} obj
* @return {Boolean}
* @api private
*/
function isGenerator (obj) {
return (typeof obj.next === 'function') && (typeof obj.throw === 'function')
@ZhihaoLau
ZhihaoLau / promise.js
Created February 21, 2017 03:24 — forked from jish/promise.js
An example "always" behavior for ES6 promises. This only works if you do not create / return intermediate promises.
// A thing I want to do
// This flow only involves **one** promise, for example an ajax call
// None of the subsequent `then` or `catch` calls, return new promises.
var explode = false;
var promise = new Promise(function(resolve, reject) {
if (explode) {
@ZhihaoLau
ZhihaoLau / configurableLogger.js
Created October 15, 2016 16:11
configurable middleware example
function configableLogger(format) {
var re = /:(\w+)/g;
return function(req, res, next) {
var str = format.replace(re, (match, property) => {
return req[property];
});
console.log(str);
next();
}
}
@ZhihaoLau
ZhihaoLau / fakeMap.js
Created October 14, 2016 06:34
fakeMap in Array@reduce
var arr = [1, 2, 3, 4];
function handleNext(value) {
return value * 2
}
Array.prototype.fakeMap = function(cb) {
return this.reduce((accumulator, next, index) => {
@ZhihaoLau
ZhihaoLau / times.js
Created October 14, 2016 02:42
‘Ruby times method’ in JavaScript
// Ruby = 5.times { |i| puts i }
// JS = (1).times(function(i){console.log(i);})
Number.prototype.times = function(cb) {
var i = -1;
while (++i < this) {
cb(i);
}
return +this;
@ZhihaoLau
ZhihaoLau / recursion.js
Created October 12, 2016 02:41
recursion.js
let categories = [
{ id: 'animals', 'parent': null },
{ id: 'mammals', 'parent': 'animals' },
{ id: 'cats', 'parent': 'mammals' },
{ id: 'dogs', 'parent': 'mammals' },
{ id: 'chihuahua', 'parent': 'dogs' },
{ id: 'persian', 'parent': 'cats' },
{ id: 'siamese', 'parent': 'cats' },
]
@ZhihaoLau
ZhihaoLau / shorthandGenerator.js
Created October 5, 2016 16:30
ES6 method shorthand generator
let o = {
*createIterator(items) {
for (let i = 0; i < items.length; i++) {
yield items[i];
}
}
};
let it = o.createIterator([1, 2, 3]);
@ZhihaoLau
ZhihaoLau / createIterator.js
Created October 5, 2016 16:14
createIterator - generator way
function *createIterator(items) {
for(let i = 0; i < items.length; i++) {
yield items[i];
}
}
let it = createIterator([1, 2, 3]);
console.log(it.next()); // Object {value: 1, done: false}
console.log(it.next()); // Object {value: 2, done: false}