Skip to content

Instantly share code, notes, and snippets.

View maxbeatty's full-sized avatar

Max Beatty maxbeatty

View GitHub Profile
@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,
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 / index.js
Created May 30, 2014 16:41
Global config via JSON file in Node (`node index.js`)
global.config = require('./path-to-config.json');
require('./other');
@maxbeatty
maxbeatty / index.js
Created June 30, 2014 20:56
Detecting extensions in Express.js (v3) route
var express = require('express'),
app = express();
app.configure(function() {
app.use(app.router);
app.use(function(req, res, next) { res.send(404); });
});
app.get('/:name', function(req, res, next) {
var re = /\.(csv|pdf)$/,
@maxbeatty
maxbeatty / key-obj.litcoffee
Created July 2, 2014 16:50
CoffeeScript checking if key exist in object

You would think it works just like JavaScript:

'pushState' in window.history

But this actually compiles to:

var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
__indexOf.call(window.history, 'pushState') >= 0;

What you want to do is use of instead of in:

@maxbeatty
maxbeatty / collection.coffee
Created July 9, 2014 02:31
Promises make dealing with validation + AJAX cases a breeze when testing
saveDraft: -> $.Deferred (deferred) =>
# do validation
validationError = @validate()
if validationError
# use deferred to return immediately with failure
return deferred.reject validationError
$.post @url, @toJSON()
@maxbeatty
maxbeatty / postmortem.coffee
Created July 30, 2014 02:20
Be careful how you update and delete with Sequelize
require('dotenv').load()
db = require './server/lib/db'
addressRepo = require './server/repositories/addressRepository'
db.sequelize.sync().complete (err) ->
throw err if err
addressRepo.findAll {}, (err, addresses) ->
throw err if err
console.log addresses.length + ' addresses to begin with'
@maxbeatty
maxbeatty / obfuscate.html
Created August 4, 2014 03:12
How I'm obfuscating email addresses in 2014
<p>
<strong>First Last</strong> whatever.
</p>
<a href="javascript:void(0)" class="pick-me">Email Me</a>
<script>
// sets href to "mailto:firstlast@domain.com"
document.querySelector('.pick-me').onclick = function (e) {
this.href = [
@maxbeatty
maxbeatty / jeff-icon.sass
Created September 17, 2014 19:00
Adventures in Spriting
$lowRes: sprite-map("chat/sprites/*.png")
$highRes: sprite-map("chat/sprites-retina/*.png")
.chat-icon
margin-right: 10px
cursor: pointer
+sprite-background(message-icon, $lowRes, $highRes)
&:hover
+sprite-background(message-icon_hover, $lowRes, $highRes)
&.unread
@maxbeatty
maxbeatty / gist:f70a6cf72c7c2c4cc1a8
Created October 20, 2014 04:34
SSL caveat when installing nginx via homebrew
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
/usr/local/etc/openssl/certs
and run
/usr/local/opt/openssl/bin/c_rehash
This formula is keg-only, which means it was not symlinked into /usr/local.
Mac OS X already provides this software and installing another version in