Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / create_108_usb_install.sh
Created July 29, 2012 13:33
bash: Create a 10.8 "USB Install Stick" from InstallESD.dmg
#!/bin/bash
# Before running this script, open Disk Utility.
#
# Partition USB Stick:
# * 1 Partition
# * Options -> GUID Partition Table
# * Mac OS Extended (Journaled)
# * Name: Foobar
#
@cowboy
cowboy / Gruntfile.js
Created July 27, 2012 12:33
grunt v0.4.0a: jquery init template generated gruntfile
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
// Metadata.
pkg: '<json:super-awesome.jquery.json>',
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
@cowboy
cowboy / Gruntfile.js
Created July 26, 2012 21:07
grunt: lint + watch example
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: '<json:awesome.jquery.json>',
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
@cowboy
cowboy / Gruntfile.js
Created July 26, 2012 20:42
grunt: sample gruntfile for jquery init template w/ all jshint options/globals
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: '<json:awesome.jquery.json>',
banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\\n" : "" %>' +
@cowboy
cowboy / am-i-being-dumb.js
Created July 24, 2012 17:06
JavaScript: split on :, handling escaped \:
// Split colon-separated parts, unescaping (but not splitting on) any \:
function split(str) {
return str.replace(/\\:/g, '\uFFFF').split(':').map(function(part) {
return part.replace(/\uFFFF/g, ':');
});
}
// Eg.
@cowboy
cowboy / sudo-keepalive-example.sh
Created July 15, 2012 20:55
Bash: Sudo keep-alive (good for long-running scripts that need sudo internally but shouldn't be run with sudo)
#!/bin/bash
# Might as well ask for password up-front, right?
sudo -v
# Keep-alive: update existing sudo time stamp if set, otherwise do nothing.
while true; do sudo -n true; sleep 60; kill -0 "$$" || exit; done 2>/dev/null &
# Example: do stuff over the next 30+ mins that requires sudo here or there.
function wait() {
@cowboy
cowboy / gist:3086861
Created July 10, 2012 23:18 — forked from rmurphey/gist:3086328
What's wrong with Netmag's "Optimize your JavaScript" post

What's wrong with Netmag's "Optimize your JavaScript" post

I tweeted earlier that this should be retracted. Generally, these performance-related articles are essentially little more than linkbait -- there are perhaps an infinite number of things you should do to improve a page's performance before worrying about the purported perf hit of multiplication vs. division -- but this post went further than most in this genre: it offered patently inaccurate and misleading advice.

Here are a few examples, assembled by some people who actually know what they're talking about (largely Rick Waldron and Ben Alman, with some help from myself and several others from the place that shall be unnamed).

Things that are just plain wrong

  • Calling array.push() five times in a row will never be a "performance improvement." The author has clearly co
@cowboy
cowboy / filterify.js
Created July 2, 2012 20:41
JavaScript: practical partial application, like creating a function that can be passed to Array#filter or the like.
/*
* Function#filterify
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
Function.prototype.filterify = (function() {
// IIFE gives us a place for storing local vars...
@cowboy
cowboy / 1.before.js
Created June 25, 2012 17:46
grunt: an example of building task targets dynamically
/*global module:false*/
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
min: {
ariaAccessibility: {
src: ['src/javascripts/jquery.selectBoxIt.ariaAccessibility.js'],
dest: 'src/javascripts/jquery.selectBoxIt.ariaAccessibility.min.js'
},
@cowboy
cowboy / fn-decl-expr.js
Created June 20, 2012 18:24
javascript: function declaration vs expression
function foo(state) {
function bar() { return 1; }
return state ? bar : null;
}
foo(false) // null (bar is created but not used)
foo(true)() // 1 (bar is created and returned)
function foo(state) {
return state ? function() { return 1; } : null;
}