Skip to content

Instantly share code, notes, and snippets.

View sstur's full-sized avatar

Simon Sturmer sstur

View GitHub Profile
@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);
}
}
@sstur
sstur / gist:5925300
Created July 4, 2013 06:19
Detect Support for Data-URI
var img = new Image();
img.onload = img.onerror = function() {
if (img.width != 1 || img.height != 1) {
document.documentElement.className += " no-data-uri";
}
};
img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
@sstur
sstur / extend-class.js
Created May 15, 2013 09:08
This function begets a child class (constructor function) from a parent class (constructor function). It can be attached to the constructor `Class.extend = extend;` or invoked directly `extend(Parent, {})`
//Usage: Model.extend = extend; MyModel = Model.extend({some: method})
function extend(protoProps, classProps) {
var args = Array.prototype.slice.call(arguments);
var ParentClass = (typeof this == 'function') ? this : args.shift();
function ChildClass() {
var result = ParentClass.apply(this, arguments);
return (Object(result) === result) ? result : this;
}
ChildClass.prototype = Object.create(ParentClass.prototype, {
constructor: {