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 / 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;
@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 / 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 / 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 / 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 / index.html
Created March 25, 2015 10:57
css: Brush and Spinning loading
<div class="overlay">
<div class="loading"></div>
<div class="fool-tooltip" data-tooltip="Processing...">Processing...</div>
</div>
@simongong
simongong / react-component.sublime-snippet
Last active September 18, 2015 08:13
sublime-snippet: React component snippet
<snippet>
<content><![CDATA[
import React from 'react';
let ${1:componentName} = React.createClass({
propTypes: {
},
getInitialState() {
return {};
@simongong
simongong / getDispositionHeader.js
Last active May 10, 2016 06:34
JavsScript: get disposition header for response
function(userAgent, fileName) {
var suffix = '.zip';
if(userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
return 'attachment; filename=' + encodeURIComponent(fileName) + suffix;
} else if(userAgent.indexOf('firefox') >= 0) {
return 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileName)+'"' + suffix;
} else {
return 'attachment; filename=' + new Buffer(fileName).toString('binary') + suffix;
}
}
@simongong
simongong / css-class-name.js
Created May 10, 2016 06:33
JavaScript: Operate on class name of dom elements
module.exports = {
hasClassName: function(element, name) {
return new RegExp('(?:^|\\s+)' + name + '(?:\\s+|$)').test(element.className);
},
addClassName: function(element, name) {
if (!this.hasClassName(element, name)) {
element.className = element.className ? [element.className, name].join(' ') : name;
}
},
@simongong
simongong / addDispalyName.js
Created May 10, 2016 06:35
Add display name for functions in a commonJS module
for(var func in module.exports) {
if (typeof module.exports[func] === 'function') {
module.exports[func].displayName = func;
}
}