Skip to content

Instantly share code, notes, and snippets.

@tored
tored / gist:1834665
Created February 15, 2012 09:17
Git: diff mode changes
git diff ${GIT_HASH} --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -d'\n' chmod +x
git diff ${GIT_HASH} --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -d'\n' chmod -x
@tored
tored / bind.js
Last active October 11, 2015 13:47
bind: binds a function to an object scope
function bind(o, fn) {
return function() {
return fn.apply(o, arguments);
};
}
@tored
tored / hasClass.js
Last active October 11, 2015 13:47
hasClass: checks if DOM element has a specific CSS class
function hasClass(el, cls) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
return reg.test(el.className);
}
@tored
tored / addClass.js
Last active October 11, 2015 13:47
addClass: adds CSS class to DOM element
function addClass(el, cls) {
if (!hasClass(el, cls)) {
el.className += (el.className ? ' ': '') + cls;
}
}
@tored
tored / removeClass.js
Last active October 11, 2015 13:47
removeClass: removes CSS class from DOM element
function removeClass(el, cls) {
var reg;
if (hasClass(el, cls)) {
reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
el.className = el.className.replace(reg,' ').replace(/^\s+|\s+$/g, '');
}
}
@tored
tored / toggleClass.js
Last active October 11, 2015 13:47
toggleClass: removes or add a CSS class to a DOM element
function toggleClass(el, cls) {
if (hasClass(el, cls)) {
removeClass(el, cls);
} else {
addClass(el, cls);
}
}
@tored
tored / timer.js
Last active October 11, 2015 13:48
timer
function timer() {
var date = new Date();
return function () {
return (((new Date()).getTime() - date.getTime())/1000);
};
}
@tored
tored / isoDateString.js
Last active October 11, 2015 13:48
isoDateString
function isoDateString(d) {
function pad(n) {
return n < 10 ? '0' + n : n;
}
return d.getUTCFullYear() + '-' +
pad(d.getUTCMonth()+1) + '-' +
pad(d.getUTCDate()) + 'T' +
pad(d.getUTCHours()) + ':' +
pad(d.getUTCMinutes()) + ':' +
pad(d.getUTCSeconds()) + 'Z';
@tored
tored / curry.js
Last active October 11, 2015 13:48
curry
function curry(fn) {
var slice = Array.prototype.slice,
args = slice.call(arguments).slice(1);
return function () {
return fn.apply(null, args.concat(slice.call(arguments)));
};
}
@tored
tored / beget.js
Last active October 11, 2015 13:48
beget: return a new object with o as a prototype
function beget(o) {
function F() {}
F.prototype = o;
return new F();
}