Skip to content

Instantly share code, notes, and snippets.

View simongong's full-sized avatar
🏠
Working from home

simongong

🏠
Working from home
View GitHub Profile
@simongong
simongong / cacheCompute.js
Created March 13, 2015 06:16
JavaScript: compute by recursive function call with cache
/*
* Extract from <JavaScript: The Good Parts>
* General routine with cache for recursive function invoking
*/
var cacheCompute = function(cache, compute){
var shell = function(n){
var result = cache[n];
if(typeof result !== 'number'){
result = compute(shell, n);
cache[n] = result;
@simongong
simongong / momentHelper.js
Created October 9, 2014 06:15
JavaScirpt: some helper functions with moment.js
/*
@param
date: '2014-01'
@return
'2014-02'
*/
function getNextMonth(date){
var current = moment(date).month();
return moment(date).month(current + 1).format("YYYY-MM");
}
@simongong
simongong / handlebarsHelpers.js
Last active August 29, 2015 14:05
JavaScript: handlebarsHelpers
// Transform a date string into an object, e.g.
// "2014-04" -> {0:"2014", 1:"04"}
translateDate: function(format, date){
var args = Array.prototype.slice.call(arguments);
var dates = date.split('-');
if(dates.length === 2){
args.splice(1, 1, dates[0], dates[1]);
}else if(dates.length === 3){
args.splice(1, 1, dates[0], dates[1], dates[2]);
}
@simongong
simongong / accessCookie.js
Created August 11, 2014 10:37
JavaScript: accessCookie()
var getCookie = function(name) {
var cookies = document.cookie.split(";");
for (var i=0; i < cookies.length; i++) {
var x = cookies[i].substr(0, cookies[i].indexOf("="));
var y = cookies[i].substr(cookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g,"");
if (x == name) {
return decodeURIComponent(y);
}
@simongong
simongong / jsExtend.js
Last active August 29, 2015 14:05
JavaScript: jsExtend
var isFunction = function(target){
var getType = {};
return !!target &&
(getType.toString.call(target) === '[object Function]' ||
typeof target === 'function');
};
var objectExtend = function(where) {
Array.prototype.slice.call(arguments, 1).forEach(function(source) {
var key;