Skip to content

Instantly share code, notes, and snippets.

View catdad's full-sized avatar
🍍
What's happening?

Kiril Vatev catdad

🍍
What's happening?
View GitHub Profile
@catdad
catdad / console.js
Created June 16, 2014 17:34
Simple `console.log` development patch for IE8+
if (!window.console) { window.console = { log: function () {} }; } // console fix for IE8
if (!console.log) { console.log = Function.prototype.bind.call(console.log, console); } // console fix for IE9
var c = {
hash: {},
useNative: true,
time: function(str){
if (this.useNative && console && console.time) console.time(str);
else this.hash[str] = (new Date()).getTime();
},
timeEnd: function(str){
if (this.useNative && console && console.time && console.timeEnd) console.timeEnd(str);
else if (this.hash[str]){
@catdad
catdad / downloadString.js
Created July 24, 2014 13:33
Download a client-side generated string of text.
function downloadString(str){
// get a filename
var filename = (prompt('Filename'));
if ('Blob' in window) {
//create file blob
var blob = new Blob([str], { 'type': 'text/plain;charset=utf-8' });
if (window.navigator.msSaveBlob) {
//works in IE 10, 11
@catdad
catdad / css-prefix.js
Last active August 29, 2015 14:04
Detect the browser vendor prefix.
var prefix = (function(){
var styles = window.getComputedStyle(document.documentElement, '');
var matches = [].slice.call(styles).join(' ').match(/-(moz|webkit|ms|o)-/);
return (matches) ? matches.shift() : null;
})();
// based on this post
// http://davidwalsh.name/vendor-prefix
@catdad
catdad / EventEmitter.js
Last active August 29, 2015 14:06
A simple event emitter class.
var EventEmitter = function(){
var events = {};
this.on = function(name, callback){
name = name.toLowerCase();
events[name] = events[name] || [];
events[name].push(callback);
return this;
};
@catdad
catdad / CPS.js
Last active August 29, 2015 14:06
// standardize done arguments with Continuation Passing Style
var createDoneArgs = function(args){
var doneArgs = [].slice.call(args);
doneArgs.unshift(undefined);
return doneArgs;
};
//usage
function(arg1, arg2, npcCallback) {
var that = this;
@catdad
catdad / queue.js
Last active August 29, 2015 14:06
var Queue = function(){
var deferArray = [],
running = false;
function recursiveExecute(done) {
// maintain scope
(function recurse(){
if (running && deferArray.length) {
var func = deferArray.shift();
@catdad
catdad / node-colors.js
Created October 29, 2014 01:25
Very simplified NodeJS colors in the console
var color = {
red: [31, 39],
green: [32, 39],
yellow: [33, 39],
blue: [34, 39],
magenta: [35, 39],
cyan: [36, 39],
white: [37, 39],
gray: [90, 39],
dim: [2, 22]
var sortDOM = (function(){
var sort = [].sort;
return function(elems, comparator) {
// Sort the elements.
// Make sure to get the pure elements array out of the jQuery wrapper.
var sortCollection = sort.call($(elems).get(), comparator);
// Check to make sure we have items in the collection
if (sortCollection.length === 0) {
@catdad
catdad / clickTouch.js
Last active August 29, 2015 14:13
Detecting use of touch or mouse.
// Determine the event name to use for mouse and touch detection/interaction.
// All browsers will listen to touch.
var clickTouch = "touchend";
if (window.navigator.pointerEnabled) {
// IE11 mobile event, covering mouse and touch
clickTouch += " pointerup";
} else if (window.navigator.msPointerEnabled) {
// IE10 mobile event, covering mouse and touch
clickTouch += " MSPointerUp";
} else {