Skip to content

Instantly share code, notes, and snippets.

View maxbeatty's full-sized avatar

Max Beatty maxbeatty

View GitHub Profile
arr1 = ['T-GEO-US', 'T-GEO-CA']
arr2 = ['T-GEO-US', 'T-GEO-GA']
targetings = [arr1, arr2]
# we want only T-GEO-US to be returned
# Current:
flatTargetings = [].concat.apply [], targetings
# flatTargetings = ['T-GEO-US', 'T-GEO-CA', 'T-GEO-US', 'T-GEO-GA']
geoTargetings = _.uniq flatTargetings
@maxbeatty
maxbeatty / slow_install.js
Created March 16, 2014 23:41
slow npm install
#!/usr/bin/env node
// This script will attempt to install your dependencies one by one
// I wrote it while debugging repeated failed attempts to `npm install`
// In the end, `npm cache clean` solved things for me
var json = require(__dirname + '/../package.json'),
depNames = Object.keys(json.dependencies),
devDepNames = Object.keys(json.devDependencies),
spawn = require('child_process').spawn,
assert = require 'assert'
driver = require '../../src/driver'
describe 'driver', ->
it 'should have drive as skill', ->
assert.equal driver.data.skill, "Drive", "skill not set"
restify = require("restify")
passport = require("passport")
GoogleStrategy = require("passport-google").Strategy
jsonContentType = (req, res, next) ->
res.setHeader("content-type", "application/json")
next(req, res, next)
server = restify.createServer(
name: "Sparked API"
@maxbeatty
maxbeatty / step-scoped.coffee
Created December 14, 2013 01:44
Keeping variables scoped for [step](https://github.com/creationix/step) calls
Step = require 'step'
Step(
->
return 'mine'
(err, @scoped) ->
console.log '1', @scoped
return 'diff1'
(err, msg) ->
@maxbeatty
maxbeatty / arrCompare.coffee
Created December 6, 2013 20:43
Compare two arrays in CoffeeScript
# src: http://stackoverflow.com/a/14853974/613588
# attach the .compare method to Array's prototype to call it on any array
Array.prototype.compare = (array) ->
# if the other array is a falsy value, return
return false unless array
# compare lengths - can save a lot of time
return false if @length isnt array.length
for i in [0..@length]
@maxbeatty
maxbeatty / dropdown.css
Created December 6, 2013 18:34
Simple dropdown menu without YUI
.pure-menu-can-have-children:hover {
background: #eee;
}
.pure-menu-can-have-children:hover ul {
top: 35px; /* height of menu li */
left: 0;
visibility: visible;
border: 1px solid #b7b7b7;
background: #fff;
findAllWithLineItems: (id, searchCriteria, cb) ->
self = @
Step(
->
self.findAll searchCriteria, @
return
(err, orders) ->
return cb err if err
@maxbeatty
maxbeatty / Model.coffee
Last active December 22, 2015 19:09
Editing Backbone Model attributes that are arrays
class ArrfulModel extends Backbone.Model
editArray: (method) ->
# populate array of arguments from callee
calleeArgs = (i for i in arguments[1])
# what attribute are we editting?
attr = calleeArgs.shift()
# get returns reference so must clone

Literate CoffeeScript

Literate Coffeescript allows you to write code as Markdown documents and when compiled, the indented blocks are treated as code and the rest is ignored as comments.

Literate programming is a methodology that combines a programming language with a documentation language, thereby making programs more robust, more portable, more easily maintained, and arguably more fun to write than programs that are written only in a high-level language.

We have a lot of crazy logic because of always-flexible business rules. Writing in a more verbose, natural way to describe why we have an odd snippet of code would make it easier for someone else to come back to later.

Here's a quick example of how we could rewrite for loops that populate an array in a more coffee-like manner