Skip to content

Instantly share code, notes, and snippets.

@qinbx
qinbx / dom-to-json.js
Created March 3, 2021 05:41 — forked from sstur/dom-to-json.js
Stringify DOM nodes using JSON (and revive again)
function toJSON(node) {
let propFix = { for: 'htmlFor', class: 'className' };
let specialGetters = {
style: (node) => node.style.cssText,
};
let attrDefaultValues = { style: '' };
let obj = {
nodeType: node.nodeType,
};
if (node.tagName) {
@qinbx
qinbx / splitString.js
Created July 20, 2016 04:53 — forked from hendriklammers/splitString.js
Javascript: Split String into size based chunks
/**
* Split a string into chunks of the given size
* @param {String} string is the String to split
* @param {Number} size is the size you of the cuts
* @return {Array} an Array with the strings
*/
function splitString (string, size) {
var re = new RegExp('.{1,' + size + '}', 'g');
return string.match(re);
}
@qinbx
qinbx / Add space beside < and > characters
Created March 30, 2016 08:40
- Add space beisde one more more "<" character
var arr = ['abc', 'ab<', 'a<b', 'a>b', '>ab', 'a >>b', '<<ab>> ', '<scr>i<<pt>'];
arr.map(function (item, index) {
console.log(index, item, item.replace(/(\s*<+\s*|\s*>+\s*)/g, function (arg) {
return (/^\S/.test(arg) ? ' ' : '') + arg + (/\S$/.test(arg) ? ' ' : '');
}));
});
/*
* Easing Functions - inspired from http://gizma.com/easing/
* only considering the t value for the range [0, 1] => [0, 1]
*/
EasingFunctions = {
// no easing, no acceleration
linear: function (t) { return t },
// accelerating from zero velocity
easeInQuad: function (t) { return t*t },
// decelerating to zero velocity
@qinbx
qinbx / print.js
Last active August 29, 2015 14:21 — forked from shaliko/print.js
(function() {
var beforePrint = function() {
console.log('Functionality to run before printing.');
};
var afterPrint = function() {
console.log('Functionality to run after printing');
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
@qinbx
qinbx / README.md
Last active August 29, 2015 14:17 — forked from mbostock/.block

This example demonstrates how to add visible and draggable handles to D3’s d3.svg.brush component, rather than that relying on the invisible boundary of the brush extent. The handle sizes here are exaggerated for demonstration purposes!

@qinbx
qinbx / namespace.js
Last active January 8, 2026 23:43
Javascipt namespace
function NameSpace(arg){
var root= this,
arr,
nodePath=[];
// check arg
if(typeof arg !=='string' || /^([a-z_$][0-9a-z_$]*\.)*[a-z_$][0-9a-z_$]*$/gi.test(arg)==false){
console.error( "Invalid argument", arg)
return;
}
@qinbx
qinbx / API data format- error.js
Last active August 29, 2015 14:03
API data format
// Error message format
{
"success": false,
"messageKey": "user_name_error",
"message": [ // Not required, if provided, will show this message to user
{type:"success", text:"task 1 done success"},
{type:"error", text:"task 2 failed"},
],
"mayRetry":false
}