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 / 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 / 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 / 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 / cloudSettings
Created May 31, 2017 01:35
Visual Studio Code Settings Sync Gist
{"lastUpload":"2017-05-31T01:35:50.116Z","extensionVersion":"v2.8.1"}
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)