Skip to content

Instantly share code, notes, and snippets.

View davidaurelio's full-sized avatar
💭
I may be slow to respond.

David Aurelio davidaurelio

💭
I may be slow to respond.
View GitHub Profile
@davidaurelio
davidaurelio / lazy-property.js
Created December 3, 2012 08:54
Lazy properties for ES5
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,
/**
* From AS2:
* class A {
* var foo = 5;
* }
*
* class A extends B {
* var foo = 6;
* }
*/
@davidaurelio
davidaurelio / async.js
Created March 28, 2013 08:35
Naïve parallel/chain implementation for asynchronous functions taking node-style callbacks
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 {
@davidaurelio
davidaurelio / partial.js
Created April 30, 2013 10:15
Simple higher-order partial function that takes advantage of sparse arrays to make parameters skippable.
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 / .profile
Last active December 20, 2015 11:09
bash helper function to make debugging node cli programs easier.
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 / EventEmitter.js
Created March 22, 2012 09:54
Constructable mixins in JavaScript
/*
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);
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];
}
!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());
}();
@davidaurelio
davidaurelio / create.js
Created February 22, 2011 14:48
Constructor-less inheritance for ECMAScript 5
var BaseObject = {
create: function create() {
var instance = Object.create(this);
instance._construct.apply(instance, arguments);
return instance;
},
extend: function extend(properties, propertyDescriptors) {
propertyDescriptors = propertyDescriptors || {};
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>count bits</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>