Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / grunt-config-options.js
Created June 6, 2012 18:10
grunt: config options
grunt.initConfig({
foo: {
_options: {
param: 'default'
},
bar: {},
baz: {
_options: {
param: 'override default'
}
@cowboy
cowboy / object-forin-forown.js
Created June 5, 2012 17:34
JavaScript: Object#forIn and Object#forOwn
/*
* Object#forIn, Object#forOwn
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
Object.defineProperties(Object.prototype, {
forIn: {
@cowboy
cowboy / readOptionalJSON.js
Created June 5, 2012 16:36
Grunt: readOptionalJSON
'use strict';
module.exports = function(grunt) {
function readOptionalJSON(filepath) {
var data = {};
try {
data = grunt.file.readJSON(filepath);
grunt.log.write('Reading data from ' + filepath + '...').ok();
} catch(e) {}
@cowboy
cowboy / ditto.txt
Created June 4, 2012 17:39
IRC: ditto
<iros> qunit question - I have some behavior I want to test that is only important before DOM ready... is there a way to do that with qunit?
<tbranyen> iframe
<danheberden> ditto
<ben_alman> ditto to whom
<danheberden> it that isn't apparent, then it's of no consequence
<ben_alman> stop avoiding the question
<danheberden> avoidance is only discerned when the answer sought isn't received
<ben_alman> ditto my previous statement; also note the qualification of said ditto
<iros> lol
<ben_alman> ditto, iros
@cowboy
cowboy / Abstraction.js
Created May 24, 2012 20:25
A modern JavaScript if-elseif-else abstraction, because control flow statements are so 1995
/*
* Abstraction.js
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* http://benalman.com/about/license/
*/
var Abstraction = (function($) {
var _ = $.prototype;
@cowboy
cowboy / tweets.txt
Created May 24, 2012 19:42
stupid JavaScript-related tweets
// http://twitter.com/cowboy/status/205726591622463488
// http://twitter.com/cowboy/status/205726020026904579
Whee (function(p,q,r){r=p[q];p[q]=function(){return Math.random()&lt;0.9?r.apply(this,arguments):'[object WAT]'}}(Object.prototype,'toString'))
// http://twitter.com/cowboy/status/196967238710992896
You know what sucks? function F(){} var f = new F(); F.prototype = {}; f instanceof F // false #javascript
// http://twitter.com/cowboy/status/196961893502566400
Fun with functions: '$'.constructor((new(new(new(new(new function $(){$.prototype.$=$.toString=$;return':)'}).$).$).$).$).$) #javascript
@cowboy
cowboy / self-assessment.md
Created May 23, 2012 17:49 — forked from rmurphey/self-assessment.md
Self-Assessment for Bocoup's "Foundations of Programming with JavaScript" Training

Self-Assessment for Bocoup's "Foundations of Programming with JavaScript" training

This gist is aimed at helping you assess whether you'll be a good fit for Bocoup's "Foundations of Programming with JavaScript" training. This class is aimed at introducing you to fundamental concepts of development using JavaScript. In this class, you'll work through the process of building a simple game of hangman; along the way, you'll learn about functions, arrays, objects, and prototypes, and get a brief introduction to the jQuery library. You'll also learn about approaches that will help you write more maintainable code, and get introduced to tools for debugging and testing your JavaScript.

@cowboy
cowboy / tab-indent-rant.js
Created May 3, 2012 19:18
JavaScript: tab indents
// Coders, if your goal is to align foo and bar, using a <tab> character is
// incorrect. If your goal, however, is to create an additional level of
// indentation, then go you. Either way, with a tab size other than 4, this
// looks like shit.
var foo = 1,
bar = 2;
@cowboy
cowboy / indexof-split.js
Created April 26, 2012 21:26
JavaScript: indexOf-based string "split" (loosely based on Ruby's String#split)
// For Gianni
String.prototype.indexOfSplit = function(separator, n) {
var str = String(this);
var result = [];
var position;
while (str && --n > 0) {
position = separator === '' ? 1 : str.indexOf(separator);
if (position === -1) {
result.push(str);
@cowboy
cowboy / grunt.md
Created April 25, 2012 16:29 — forked from mikesherov/grunt.md
getting grunt to work on windows with cygwin and git

Getting grunt to work with Cygwin and git on Windows is a bit difficult considering the cygwin package that is installed by git has an outdated version of node running on it. There are several issues to work through:

  1. Cygwin by default installs an old version of node.
  2. Open Cygwin shell
  3. which node tells you where the executable is
  4. node --version tells you if it's old
  5. cd to the directory listed by which node
  6. mv node.exe node.exe.bak
  7. download node from nodejs.org and install it
  8. Node installed through installer doesn't update PATH to point to correct version of node installed normally.