Skip to content

Instantly share code, notes, and snippets.

View mattbasta's full-sized avatar
🌭
Still writing JavaScript

Matt Basta mattbasta

🌭
Still writing JavaScript
View GitHub Profile
// this is crazy
Function.prototype.callMeMaybe = function(ctx) {
if (Math.random() > .5) {
return this.apply(ctx, Array.prototype.slice.call(arguments, 1));
}
};
function square(n) {
return n * n;
}
"""BBEdit UNIX filter that wraps one or more optionally-indented, commented
lines to 79 chars, preserving indentation.
Just select all the lines (in their entirety) the comment spans, and invoke.
Works with #, --, and //-style comments.
For example, this... ::
# Upping this to 10000 makes it 3x faster. 10000 takes 15.936s. 5000 takes 16.303s.
#!/bin/bash
# Which version should we fetch?
b2gremote="https://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-central/"
appname="B2G"
if [ "$1" == "aurora" ]; then
b2gremote="https://ftp.mozilla.org/pub/mozilla.org/b2g/nightly/latest-mozilla-aurora/"
appname="B2GAurora"
fi
$.fn.serializeObject = function() {
var data = {};
_.each($(this).serializeArray(), function(v, k) {
k = v.name;
v = v.value;
if (_.contains(k, '__prefix__')) {
return;
}
if (k.match(/\-\d+\-/g)) {
// Turn `users-0-name=Basta` into
mock({
utils: function() {
return { ... };
}
}, function() {
var foomodule = require('foomodule');
test('foomodule', function(done) {
foomodule.should_not_explode();
done();
mock(function(patch) {
patch('utils', function() {
return { ... };
});
}, function() {
var foomodule = require('foomodule');
test('foomodule', function(done) {
foomodule.should_not_explode();
done();
var mockobj = mock({
utils: function() {
return { ... };
}
});
mockobj(['foomodule'], function(foomodule) {
test('foomodule', function(done) {
foomodule.should_not_explode();
done();
@mattbasta
mattbasta / strings.po
Last active December 15, 2015 02:49
Strings for Fireplace
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2013-03-27T20:42:19.302Z\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"MIME-Version: 1.0\n"
@mattbasta
mattbasta / rainbow.js
Last active December 14, 2015 18:39 — forked from anonymous/rainbow.js
/*
Original graphicsbc source:
a0,0
p250,300
t20,0
L
10
Ha0 ,255,128

Avoid code duplication and reusing code is always an admirable goal. However, in some occasions, it's not a bad idea to duplicate a little bit of code in order to make your software better.

An Example

When writing a Node app, accessing a directory listing is fairly simple: the fs.readdir command provides a list of objects in a directory, and a call to fs.stat will tell you whether each object is a directory or a file.

That's fairly straightforward, but recursing (especially when using callbacks) can require some mental yoga. Each directory needs to keep track of how many subdirectories it expects to receive callbacks for and results need to be aggregated in pieces rather than sequentially.

When I needed a recursive directory listing this past week, I was tempted to use a library: why solve a solved problem? The glob library provides this functionality with very little effort: