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 / alwaysSendHeader.js
Last active August 3, 2022 15:24
A simple example of overloading XMLHttpRequest to always send a custom header
// get real XMLHttpRequest object
var RealXMLHttpRequest = window.XMLHttpRequest;
// create the overloaded XMLHttpObject
window.XMLHttpRequest = function(){
// just in case someone is still using IE6, use a fallback
var ajax = !!RealXMLHttpRequest ? new RealXMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"),
realOpen = ajax.open;
// steal the 'open' function, since the request has o be open
@catdad
catdad / clearfix.css
Created February 27, 2015 19:52
I keep losing my reference to this, so I am adding it here.
.clearfix:after {
content: "";
display: table;
clear: both;
}
@catdad
catdad / multiple-gulp-async.js
Last active August 29, 2015 14:21
Run multiple asynchronous tasks and stream tasks inside a single Gulp task
var gulp = require('gulp');
// a regular async function
function async(done) {
console.log('async start');
// take your time doing anything you want
setTimeout(function () {
console.log('async end');
done();
// the purpose of this is not to match any ID standard, but to allow for a
// reasonably and reliably unique ID when generating on a distributed system
function getId() {
return btoa(
(Math.random().toString(36).slice(-4)) + '_' +
(new Date()).toISOString() + '_' +
(Math.random().toString(36).slice(-6))
);
}
function parallelSync(funcs, done) {
if (typeof workers === 'function') {
done = workers;
workers = 1;
}
var counter = funcs.length;
var idx = 0;
var queue = [].concat(funcs);
var results = [];
// standardize a Promise with Continuation Passing Style
function PromiseCPS (promise) {
return function cpsFunc(done) {
promise.then(
function promiseSuccess() {
done.apply(undefined, [undefined].concat([].slice.call(arguments)));
}, function promiseFailure(reason) {
done(reason);
}
);
@catdad
catdad / inheritance.js
Last active August 29, 2015 14:25
... because this is the right answer when asked about JavaScript inheritance
function inherit(proto) {
function F() {};
F.prototype = proto;
return new F;
// Note: use `Object.create` instead of `inherit` if you do not need to support old browsers
}
// create any "class"
function A() {
var start = process.hrtime();
// do work here
var diff = process.hrtime(start);
var nano = diff[0] * 1e9 + diff[1];
console.log('benchmark took %d nanoseconds', nano);
console.log('benchmark took %d milliseconds', nano * 1e-6);
// http://jsfromhell.com/array/shuffle
function shuffle(v){
for(var j, x, i = v.length; i; j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
return v;
};
@catdad
catdad / jshint-reporter.js
Created December 15, 2015 13:00
really bad jshint reporter
var devLintReporter = function(issues, config, opts) {
if (!issues.length) { return; }
opts = opts || {};
var chalk = gutil.colors;
var files = {};
console.log(chalk.yellow('----------------------'));
console.log(chalk.yellow('JSHint Report:'));