Skip to content

Instantly share code, notes, and snippets.

@radvieira
radvieira / gist:5118583
Created March 8, 2013 18:17
Master class - Prototypes
var Person = function(name) {
this.name = name;
};
Person.prototype.getName = function() {
return this.name;
};
var thomas = new Person('Thomas');
var amy = new Person('Amy');
@radvieira
radvieira / gist:5118005
Created March 8, 2013 17:04
js master class
Function.prototype.cached = function() {
var cache = {},
that = this;
return function(arg) {
if(!(arg in cache)) {
cache[arg] = that(arg);
}
return cache[arg];
};
@radvieira
radvieira / gist:5111308
Created March 7, 2013 20:04
count down
// Exercise 2 - Closures
// Wrap the following code in a closure and export only the "countdown" function.
var exercise = (function() {
// Code
var index;
function log(){
console.log(index);
@radvieira
radvieira / gist:5111288
Created March 7, 2013 20:02
repeat x times
Function.prototype.repeat = function(count) {
return {
times: function(str) {
var i;
for (i = 0; i < count; i++) {
console.log(str);
}
}
}
};
@radvieira
radvieira / Pipe request on success
Last active December 12, 2015 05:18
Here's how you can pipe request to a writablestream a successful response.
var stream = fs.createWriteStream(writeTo);
request(uri).on('response', function(response) {
if(response.statusCode === 200) {
this.pipe(stream);
}
});
@radvieira
radvieira / npm sqlite3 install
Created January 30, 2013 04:09
npm sqlite3 install output using node v 0.8.18.
$ npm install sqlite3
npm http GET https://registry.npmjs.org/sqlite3
npm http 304 https://registry.npmjs.org/sqlite3
> sqlite3@2.1.5 install /Users/raulvieira/Development/img-crawler/node_modules/sqlite3
> node-gyp rebuild
gyp http GET http://nodejs.org/dist/v0.8.18/node-v0.8.18.tar.gz
gyp http 200 http://nodejs.org/dist/v0.8.18/node-v0.8.18.tar.gz
CC(target) Release/obj.target/sqlite3/deps/sqlite3/sqlite3.o