Skip to content

Instantly share code, notes, and snippets.

View sstur's full-sized avatar

Simon Sturmer sstur

View GitHub Profile
@sstur
sstur / gist:8545473
Created January 21, 2014 18:30
JavaScript Reserved Words
abstract, boolean, break, byte, case, catch, char, class, const, continue, debugger,
default, delete, do, double, else, enum, export, extends, false, final, finally,
float, for, function, goto, if, implements, import, in, instanceof, int, interface,
long, native, new, null, package, private, protected, public, return, short, static,
super, switch, synchronized, this, throw, throws, transient, true, try, typeof, var,
volatile, void, while, with
@sstur
sstur / string-splice.js
Created December 12, 2013 19:32
String splice similar to array.splice: stringSplice('abcdef', 2, 2, '_') //=> ab_ef
function stringSplice(source, index, deleteChars, insert) {
insert = insert || '';
return source.slice(0, index) + insert + source.slice(index + Math.abs(deleteChars));
}
@sstur
sstur / find-first.js
Created December 5, 2013 03:48
Find the first occurrence (if any) in source string of set of search strings
// example findFirst('a = b(1, {}, 2)', ['(', '{']) => 5
function findFirst(source, list, pos) {
var found = -1;
for (var i = 0, len = list.length; i < len; i++) {
var index = source.indexOf(list[i], pos);
if (index >= 0) {
if (found < 0 || index < found) found = index;
}
}
return found;
@sstur
sstur / clone.js
Created November 21, 2013 06:31
Deep-copy an object, similar to calling JSON.parse(JSON.stringify(obj)) but preserves dates and undefined
function clone(obj) {
if (Object(obj) !== obj) return obj;
if (typeof obj.toJSON == 'function') {
return obj.toJSON();
}
var type = toString.call(obj).slice(8, -1);
if (type in CLONE) {
return CLONE[type].call(obj, clone);
}
var copy = {};
@sstur
sstur / safe-eval.js
Last active December 28, 2015 22:18
Override eval to always evaluate in global scope. Hide from it a pseudo global using try/catch.
eval = (function(eval) { return function(code) { return eval(code) } })(eval);
try {
throw null
} catch(app) {
app = function() { console.log('Hello World') }
app.version = '1.2';
//... all my code here ...
//now all my code has access to app, but eval doesn't
//and my code can execute in global scope (if desired)
console.log(eval('typeof app'))
@sstur
sstur / dom-to-json.js
Last active October 8, 2023 04:17
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) {
@sstur
sstur / mysql_escape_string.js
Created September 20, 2013 19:13
Escape string for MySQL
function mysql_escape_string(str) {
return str.replace(/[\0\x08\x09\x1a\n\r"'\\\%]/g, function(ch) {
switch (ch) {
case "\0":
return "\\0";
case "\x08":
return "\\b";
case "\x09":
return "\\t";
case "\x1a":
@sstur
sstur / typeof.js
Created September 10, 2013 13:48
typeof replacement one-liner
//typeof replacement one-liner
var type = (val === null) ? 'null' : Array.isArray(val) ? 'array' : typeof val;
@sstur
sstur / html-parser.js
Last active February 9, 2017 19:09
Pure JS HTML Parser (ported from CKEditor 4.2)
/*!
* HTML Parser
* Ported from CKEditor 4.2 (f74e558351)
*
*/
/*global require, exports, module, define */
var HTMLParser;
(function(definition) {
if (typeof exports == 'object' && typeof module == 'object') {
// CommonJS/Node
@sstur
sstur / addevent.js
Last active December 19, 2015 14:09
Cross-browser addEvent
function addEvent(element, eventName, callback) {
if (element.addEventListener) {
element.addEventListener(eventName, callback, false)
} else {
element.attachEvent(eventName, callback, false);
}
}