Skip to content

Instantly share code, notes, and snippets.

💭
I may be slow to respond.

David Aurelio davidaurelio

💭
I may be slow to respond.
  • Munich, Germany
Block or report user

Report or block davidaurelio

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@davidaurelio
davidaurelio / create.js
Created Feb 22, 2011
Constructor-less inheritance for ECMAScript 5
View create.js
var BaseObject = {
create: function create() {
var instance = Object.create(this);
instance._construct.apply(instance, arguments);
return instance;
},
extend: function extend(properties, propertyDescriptors) {
propertyDescriptors = propertyDescriptors || {};
View output-beautified.js
!function() {
function n() {
for (var n = arguments.length, e = Array(n), r = 0; r < n; r++) e[r] = arguments[r];
return "(s*(" + e.join(")s*,s*(") + ")s*)";
}
new RegExp("rgb" + n()), new RegExp("rgba" + n()), new RegExp("hsl" + n()), new RegExp("hsla" + n());
}();
View repro.js
var u = require('uglify-es');
var code = `
(function() {
var NUMBER = '[-+]?\\d*\\.?\\d+';
var PERCENTAGE = NUMBER + '%';
function call() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
@davidaurelio
davidaurelio / EventEmitter.js
Created Mar 22, 2012
Constructable mixins in JavaScript
View EventEmitter.js
/*
EventEmitter is usable as constructor and as mixin.
*/
function EventEmitter() {}
// this does the trick
EventEmitter.prototype = EventEmitter;
EventEmitter.addListener = function(type, listener) {
this.listeners(type, true).push(listener);
@davidaurelio
davidaurelio / .profile
Last active Dec 20, 2015
bash helper function to make debugging node cli programs easier.
View .profile
node-debug () {
PROGRAM="$(which $1)" # make sure we get an absolute path for programs in PATH
shift # remove the original program argument
set -- "$PROGRAM" "$@" # prepend the program path
node --debug-brk $@ & # start node in paused debug mode, send to background
node-inspector # start node inspector
kill %% # kill the background task (if still running)
}
@davidaurelio
davidaurelio / partial.js
Created Apr 30, 2013
Simple higher-order partial function that takes advantage of sparse arrays to make parameters skippable.
View partial.js
function partial(fn, fixedArgs) {
return function() {
var args = fixedArgs.slice(), skipped = 0;
for (var i = 0, n = arguments.length; i < n; i++) {
while (i + skipped in args) { skipped++; }
args[i + skipped] = arguments[i];
}
return fn.apply(this, args);
};
}
@davidaurelio
davidaurelio / async.js
Created Mar 28, 2013
Naïve parallel/chain implementation for asynchronous functions taking node-style callbacks
View async.js
var concat = [].concat, slice = [].slice;
function chain(fns, initialArgs, callback) {
(function next(error) { // `next` is the callback for each chained function
if (error) { callback(error); return; }
var fn = fns.shift();
if (fn) {
fn.apply(null, slice.call(arguments, 1).concat(next));
} else {
View lazyProperty-example.js
/**
* From AS2:
* class A {
* var foo = 5;
* }
*
* class A extends B {
* var foo = 6;
* }
*/
@davidaurelio
davidaurelio / lazy-property.js
Created Dec 3, 2012
Lazy properties for ES5
View lazy-property.js
function lazyProperty(object, name, create) {
Object.defineProperty(object, name, {
configurable: true, // or false if defined on prototype objects
enumerable: false,
get: function() {
return this[name] = create(this);
},
set: function(value) {
Object.defineProperty(this, name, {
configurable: true,
@davidaurelio
davidaurelio / naive-super.js
Created Sep 7, 2012
Naive implementation of super in JS
View naive-super.js
Object.defineProperty(Object.prototype, 'super', {
get: function() { return Object.getPrototypeOf(this); }
});
function Foo() {}
Foo.prototype.reset = function() {
this.fooProp = 'reset';
console.log('Foo#reset()');
}
You can’t perform that action at this time.