Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / unbind.js
Last active June 20, 2018 19:59
JavaScript: Function.prototype.unbind (for @jugglinmike)
Function.prototype.bind = (function(origBind) {
return function() {
var fn = origBind.apply(this, arguments);
fn.__origFn__ = this.__origFn__ || this;
return fn;
};
}(Function.prototype.bind));
Function.prototype.unbind = function() {
return this.__origFn__;
@cowboy
cowboy / Gruntfile.js
Created March 18, 2013 17:21
Sample Gruntfile for gruntjs/grunt#721
'use strict';
module.exports = function(grunt) {
// Override process.stdout to log the name+args of the current task before
// every logged line.
var hooker = require('hooker');
var newline = true;
hooker.hook(process.stdout, 'write', function(str) {
var prefix = grunt.task.current.nameArgs;
if (newline && prefix && str !== '\n') {
@cowboy
cowboy / README.md
Last active December 14, 2015 12:09
Bocoup jQuery Training

Bocoup jQuery Training

You may use the class materials in two ways:

  1. The easy way - Skip to the Install class materials section at the bottom and follow the first 2 steps. When done, open relevant class materials .js files in your text editor.
  2. The more involved (but more awesome) way - Follow all the directions on this page, starting here:

Install the latest version of Node.js

Node.js is a platform built on Chrome's V8 JavaScript engine that can be run via the command line.

@cowboy
cowboy / deps.js
Created March 1, 2013 19:52
Grunt: Test that package.json dependencies were installed correctly.
grunt.registerTask('deps', 'Test that package.json dependencies were installed correctly.', function() {
var pkg = grunt.file.readJSON('package.json');
var total = 0;
['dependencies', 'devDependencies'].forEach(function(key) {
var deps = pkg[key];
if (!deps) { return; }
grunt.verbose.subhead(key);
Object.keys(deps).forEach(function(name) {
total++;
@cowboy
cowboy / concat.js
Last active December 14, 2015 04:59
node-task idea: concat
var when = require('when');
var fs = require('fs');
var _ = require('lodash');
var nodefn = require('when/node/function');
var readFile = nodefn.bind(fs.readFile);
var writeFile = nodefn.bind(fs.writeFile);
// Listen for events here.
var concat = new (require('events').EventEmitter);
@cowboy
cowboy / concat-promises.js
Created February 25, 2013 17:39
concat using promises
var when = require('when');
var fs = require('fs');
var concat = {};
concat.each = function(src, dest, options) {
if (!options) { options = {}; }
if (!options.separator) { options.separator = ''; }
return when.reduce(src, function(output, srcpath) {
var dfd = when.defer();
@cowboy
cowboy / insanity.log
Last active December 13, 2015 19:48
Grunt 0.4.0 release: imminent!
[17:28] <gruntly> [grunt-contrib-coffee] tkellen force-pushed we from 2539edf to 04ffeb9: http://git.io/VYXJHg
[17:28] <gruntly> grunt-contrib-coffee/we 04ffeb9 Tyler Kellen: 0.4 release
[17:33] <gruntly> [grunt-contrib-compass] tkellen created we (+1 new commit): http://git.io/xOTrGg
[17:33] <gruntly> grunt-contrib-compass/we b31f6f0 Tyler Kellen: 0.4 release
[17:34] <gruntly> [grunt-contrib-compass] tkellen force-pushed we from b31f6f0 to 47e06bb: http://git.io/9DmNiQ
[17:34] <gruntly> grunt-contrib-compass/we 47e06bb Tyler Kellen: 0.4 release
[17:37] <gruntly> [grunt-contrib-coffee] tkellen force-pushed we from 04ffeb9 to 2b5d85b: http://git.io/VYXJHg
[17:37] <gruntly> grunt-contrib-coffee/we 2b5d85b Tyler Kellen: 0.4 release
[17:38] <gruntly> [grunt-contrib-compress] tkellen created we (+1 new commit): http://git.io/rTipSQ
[17:38] <gruntly> grunt-contrib-compress/we ba99f37 Tyler Kellen: 0.4 release
@cowboy
cowboy / bocoup-training-more-efficient-event-handlers.js
Created February 12, 2013 21:38
Bocoup training: More Efficient jQuery Event Handlers
// Straightforward + simple.
$("button").on("click", function(event) {
event.preventDefault();
var button = $(this);
var numberElem = button.find(".number");
var number = Number(numberElem.text()) - 1;
numberElem.text(number);
if (number === 0) {
button.prop("disabled", true);
button.off("click");
@cowboy
cowboy / Gruntfile.js
Last active December 12, 2015 09:59
grunt 0.4.0: including dotfiles
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
test: {
non_expand: {
files: [
{src: '*', dest: 'build', dot: true},
]
},
@cowboy
cowboy / simple-iife-example.js
Last active December 12, 2015 03:49
IIFE: the simplest example i can contrive
var value, getValue;
value = 1;
getValue = function() { return value; };
value = 2;
getValue() // 2
var value, getValue;
value = 1;