Skip to content

Instantly share code, notes, and snippets.

View reu's full-sized avatar

Rodrigo Navarro reu

View GitHub Profile
@reu
reu / gulp-self-enclosing.js
Created April 16, 2014 17:35
Self enclosing function for each file
var es = require("event-stream");
module.exports = function() {
return es.map(function(file, callback) {
file.contents = new Buffer("(function() {\n" + String(file.contents) + "\n})();");
callback(null, file);
});
};
@reu
reu / line-reader.js
Created January 4, 2014 16:38
How to read a file line by line in Node.js using the yet unstable Readline library (http://nodejs.org/api/readline.html)
var fs = require("fs"),
readline = require("readline");
var reader = readline.createInterface({
input: fs.createReadStream("large-file.txt"),
output: fs.createWriteStream("/dev/null"),
terminal: false
});
reader.on("line", function(line) {
@reu
reu / angular-autoscroll.js
Created October 10, 2013 04:42
Angular.js autoscroll directive.
angular.module("autoscroll", [])
.service("scroller", function() {
this.defaultTime = 500;
this.scroll = function(element, height, time) {
element.animate({
scrollTop: height
}, time || this.defaultTime);
}
@reu
reu / debounce.js
Last active December 22, 2015 23:19
Returns a function that as long as it continues to be called, it will not be invoked. It will only be called after `time` milliseconds.
function debounce(fn, time) {
var time = time || 300,
scope = this,
delay;
return function() {
var args = arguments;
if (delay) clearTimeout(delay);
delay = setTimeout(function() {
@reu
reu / overload.js
Last active December 19, 2015 12:49
Function.prototype.addMethod = function(name, method) {
var existingMethod = this.prototype[name];
this.prototype[name] = function() {
if (method.length == arguments.length) {
return method.apply(this, arguments);
} else if (typeof existingMethod == "function") {
return existingMethod.apply(this, arguments);
}
function inherit(child, parent) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
}
var Person = function Person(firstName, lastName) {
if (!firstName || !lastName) throw "Primeiro e último nome requerido!";
this.firstName = firstName;
this.lastName = lastName;
@reu
reu / raw_sockets.rb
Created June 5, 2013 17:28
Simple sniffer in Ruby using pcap
require 'rubygems'
require 'pcaprub'
capture = PCAPRUB::Pcap.open_live('lo0', 65535, true, 0)
capture.setfilter('icmp')
while 1==1
puts(capture.stats())
pkt = capture.next()
if pkt
puts "captured packet"
@reu
reu / pub-sub.js
Created April 9, 2013 01:51
node.js redis pub-sub example
var redis = require("redis")
, subscriber = redis.createClient()
, publisher = redis.createClient();
subscriber.on("message", function(channel, message) {
console.log("Message '" + message + "' on channel '" + channel + "' arrived!")
});
subscriber.subscribe("test");
@reu
reu / async.js
Created November 21, 2012 12:13
Async javascript loading
var async = function(url, callback) {
var script = document.createElement("script");
script.src = url;
if (callback != null) {
callback.done = false;
script.onreadystatechange = s.onload = function() {
var state = script.readyState;
@reu
reu / password_prompt.rb
Created October 19, 2012 03:19
Password prompt for CLIs
def password_prompt(message = "Inform your password: ")
puts message
begin
system "stty -echo -icanon"
gets.rstrip
ensure
system "stty echo icanon"
end
end