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
function downloadIamge(imageSrc, name) {
var image = new Image()
image.setAttribute('crossOrigin', 'anonymous')
image.onload = function () {
var canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
var context = canvas.getContext('2d')
context.drawImage(image, 0, 0, image.width, image.height)
@simongong
simongong / cloudSettings
Created May 31, 2017 01:35
Visual Studio Code Settings Sync Gist
{"lastUpload":"2017-05-31T01:35:50.116Z","extensionVersion":"v2.8.1"}
@simongong
simongong / camelSnake.js
Created May 10, 2016 06:40
JavaScript: convert format of object keys between camel-case and snake-case
/**
* @param {Object|String} data string or keys of object are named in form of snake
* @param {number} depth to which level of keys should it process
* @return {Object|String} string or keys of object are named in form of camel case
*/
exports.snakeToCamel = function(data, depth) {
if (Util.isObject(data)) {
if (typeof depth === 'undefined') {
depth = 1;
}
@simongong
simongong / customeError.js
Created May 10, 2016 06:38
JavaScript: a customized error class
const Util = require('util');
module.exports = function AlpOpenError(message, extra) {
this.name = this.constructor.name;
this.message = message;
this.extra = extra;
};
Util.inherits(module.exports, Error);
@simongong
simongong / decodeEntities.js
Created May 10, 2016 06:36
JavaScript: decode a string in browser which is html-entity-encoded
// originally from: http://stackoverflow.com/questions/5796718/html-entity-decode/27385169
function(str) {
var element = document.createElement('div');
// regular expression matching HTML entities
var entity = /&(?:#x[a-f0-9]+|#[0-9]+|[a-z0-9]+);?/ig;
function decodeHTMLEntities() {
// find and replace all the html entities
str = str.replace(entity, function(m) {
element.innerHTML = m;
return element.textContent;
@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;
}
}
@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 / 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 / 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 / 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>