Skip to content

Instantly share code, notes, and snippets.

View kkaefer's full-sized avatar
🏳️‍🌈

Konstantin Käfer kkaefer

🏳️‍🌈
View GitHub Profile
var sys = require('sys');
function Child() {}
// Defined on the prototype.
Object.defineProperty(Child.prototype, 'foo', { enumerable: false, value: 'test', writable: true });
Object.defineProperty(Child.prototype, 'bar', { enumerable: false, value: 'test2', writable: true });
var test = new Child;
// Defined local to the object "instance".
Object.defineProperty(test, 'baz', { enumerable: false, writable: true });
@kkaefer
kkaefer / gist:646017
Created October 25, 2010 23:33
node.js proxy to enforce HTTPS and remove cookies
var http = require('http');
var secure = new RegExp('(' + [
'facebook.com',
'twitter.com',
'google.com'
].join('|').replace('.', '\\.') + ')$');
var noSecure = new RegExp('^(' + [
'http://(www\.)?google\.com/(image|imghp)',
@kkaefer
kkaefer / gist:672868
Created November 11, 2010 17:38
instantiate with .call/apply
> function Class(param) { if (!param) console.log('fail'); }
> new Class
fail
{}
> x = {};
{}
> x.__proto__ = Class.prototype;
{}
> x instanceof Class
true
var util = require('util');
function Parent(arg) {
console.log('parent constructor: arg=' + arg);
}
Parent.prototype.foo = function() {
console.log('fooing!');
return this;
};
Object.defineProperty(global, 'foo', {
set: function(val) {
console.log(val);
}
});
var array = { key: 'value', key2: 'value2', key3: 'value3' };
for (foo in array);
$(function() {
function pushState() {
var url = $(this).attr('href');
replaceImage(url);
if (history.pushState) {
history.pushState({}, '', url);
}
return false;
}
json() {
python - $@ <<"EOS"
import json, sys
try:
obj = json.load(open(sys.argv[1]))
for key in sys.argv[2:]: obj = obj[key if type(obj) == dict else int(key)]
except ValueError as msg: sys.stderr.write('Invalid JSON: %s\n' % msg); exit(1)
except KeyError as msg: sys.stderr.write('Key "%s" does not exist\n' % key); exit(2)
if type(obj) == dict: print '\n'.join(obj.keys())
elif type(obj) == list: print '\n'.join(obj)
#world {
polygon-fill: #eee;
line-color: #ccc;
line-width: 1;
[zoom > 5] {
line-width:0.5;
}
}
function __stack__() {
try {
throw new Error;
} catch (err) {
var stack = err.stack.split('\n');
stack.splice(0, 3);
return stack.join('\n') + '\n';
}
}

var util = require('util');
var EventEmitter = require('events').EventEmitter;
module.exports = Queue;
function Queue(callback, concurrency) {
this.callback = callback;
this.concurrency = concurrency || 10;
this.next = this.next.bind(this);
this.invoke = this.invoke.bind(this);
this.queue = [];