Skip to content

Instantly share code, notes, and snippets.

View magalhini's full-sized avatar

Ricardo Magalhães magalhini

View GitHub Profile
@magalhini
magalhini / promise-pipe-post
Created June 3, 2013 11:07
$promise for a piped POST
var asking = new $.Deferred();
var feedback = asking.pipe(function (res) {
console.log('Sending:', res);
return $.post('/echo/json', res);
});
// Simulate form submit...
asking.resolve('my details');
@magalhini
magalhini / fjoin.js
Created June 3, 2013 15:04
Concatenate files with node.js
/* USAGE:
node fjoin file1 file2 file 3
or
fjoin({files, target})
*/
var util = require('util'),
fs = require('fs'),
files = [],
readstream,
@magalhini
magalhini / placeholderShim.js
Last active December 19, 2015 02:39
Tiny gist to mimic placeholder behaviour in old IEs. To do: remove partial jQuery dependency.
/* Add Event Shim */
var addEvent = (function () {
var filter = function(el, type, fn) {
for (var i = 0, len = el.length; i < len; i++) {
addEvent(el[i], type, fn);
}
};
if (document.addEventListener) {
return function (el, type, fn) {
if ( el && el.nodeName || el === window ) {
@magalhini
magalhini / castElement.js
Last active December 20, 2015 12:19 — forked from Couto/castElement.js
function castElement(el, target) {
// Convert nodelists and weird dom stuff into arrays
var attrs = [].slice.call(el.attributes, 0),
children = [].slice.call(el.childNodes, 0),
// create final element
element = document.createElement(target);
if (el.nodeName.toLowerCase() === 'textarea') {
element.innerHTML = el.value;
}
@magalhini
magalhini / A-Pen-by-Ricardo-Magalhães.markdown
Created February 10, 2014 13:15
A Pen by Ricardo Magalhães.
var browser = (function () {
var ua = navigator.userAgent.toLowerCase(),
platform = navigator.platform.toLowerCase(),
UA = ua.match(/(opera|ie|firefox|chrome|version)[\s\/:]([\w\d\.]+)?.*?(safari|version[\s\/:]([\w\d\.]+)|$)/) || [null, 'unknown', 0],
mode = UA[1] === 'ie' && document.documentMode,
name = (UA[1] === 'version') ? UA[3] : UA[1],
version = mode || parseFloat((UA[1] === 'opera' && UA[4]) ? UA[4] : UA[2]),
browser;
return {
@magalhini
magalhini / Mars Challenge
Last active August 29, 2015 13:58
Mars Challenge
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Mars Challenge" />
<meta charset="utf-8">
<title>Mars Challenge</title>
</head>
<body>
<h2>Mars Challenge</h2>
<pre class="neutral">
/**
* __series
* Given an array of functions, it will call every function,
* once at a time, sequentially.
* Every function will have a trigger function as its last argument,
* that should be called when the function is done.
* If arguments are given to this trigger function, those will be passed
* to the next function.
*
* @example
@magalhini
magalhini / screenshot.js
Last active August 29, 2015 14:03
node.js Image Comparison with GIF output
/* Takes two images and compares them.
* If they're different, a GIF with the differences will be created.
*
* @requires ImageMagick to be installed to generate the GIF :(
* Install it via Homebrew: brew install imagemagick
*
* @usage: node screenshot.js file1.jpg file2.jpg
*/
var pngparse = require('pngparse');
var exec = require('child_process').exec;
@magalhini
magalhini / development.md
Last active August 29, 2015 14:09
Web Development: Articles & Resources