Skip to content

Instantly share code, notes, and snippets.

@jsocol
jsocol / EventArray.js
Created May 3, 2012 19:49
EventArray.js - Arrays you can watch.
/**
* EventArray.js - Arrays you can watch.
* Copyright 2012, James Socol <me@jamessocol.com>
*
* Released under the MIT X11 and BSD 2-clause Licenses.
*
* EventArray.js is a simple wrapper around JS arrays that makes it
* easier to use them for data-driven UIs by providing a way to watch
* them for mutations. Think of the lovechild of an Array and an
* EventEmitter.
@jsocol
jsocol / gist:2574884
Created May 2, 2012 07:53
Mongolian + Deferred = Tolerable Mongo in Node
/**
* Imagine, if you will, a simple task. You have a blog written in Node, with
* its content stored in Mongo, and you want to display a single post (for
* simplicity, let's pretend the URL contains the ObjectID) and its comments.
*
* Normally, you'd have to fall into callback/scope hell, and do something
* along the lines of this, already in an onRequest handler:
*/
function displayPost(request, response) {
// We'll look up the post and its comments.
@jsocol
jsocol / gist:2422571
Created April 19, 2012 17:48
Mozillians Git-Fu
# Mozillians Git-Fu.
# Useful steps for moving around git branches and servers related to
# the Mozillians release process. Any dev should be able to do these
# in a pinch, there are no special privileges.
# Always assuming 'origin' refers to github.com/mozilla/mozillians.git
# Current branch is in () in my pseudo-prompt.
# HowTo: Cut a new 'next' branch.
# After tagging, we wipe out the current next branch and fork from
@jsocol
jsocol / bugzilla.mozilla.org.js
Created January 11, 2012 00:39 — forked from potch/README.rst
BugColorZilla!
function textColor(bg) {
var r = parseInt(bg.substr(0,2),16),
g = parseInt(bg.substr(2,2),16),
b = parseInt(bg.substr(4,2),16);
var yiq = (r * 299 + g * 587 + b * 114) / 1000;
return yiq >= 128 ? "black" : "white";
}
function bgColor(el, i) {
return ("000000" + $.trim($("a", el).eq(i).text())).substr(-6);
@jsocol
jsocol / gist:1568904
Created January 6, 2012 04:03
Bookmarklet to add inline full-size images to Reddit.
javascript: (function () {
var things = document.querySelectorAll("div.thing");
for (var j = things.length - 1; j > 0; j--) {
var thing = things[j],
entry = thing.querySelectorAll("div.entry")[0],
thumb = thing.querySelectorAll("a.thumbnail")[0],
img = new Image;
img.style.display = 'block';
if (thumb) {
var href = thumb.href;
@jsocol
jsocol / links.rst
Created December 19, 2011 21:09
Go Big or Go Home - Building Bid Django Sites
@jsocol
jsocol / Explain.rst
Last active September 28, 2015 07:27
@json_view decorator

I finally put this in its own package! Check out django-jsonview.

This is a decorator that guarantees a response will be JSON, and sends meaningful (to a developer) error messages. It relies on a BadRequest exception I created elsewhere, because there is no standard way of handling that in Django (frex, we should really return HTTP 400 on form validation failures but they don't make that particularly easy). That could easily be removed, though.

@jsocol
jsocol / pack.js
Created November 10, 2011 23:38
Packing rectangles...
/**
* Return an array of paired factors (arrays) of an integer.
*/
function factor(n) {
var fact = [[1, n]],
check = 2,
root = Math.sqrt(n);
while (check <= root) {
if (n % check == 0) {
fact.push([check, n / check]);
SET @gid = (SELECT id FROM group WHERE name = 'staff');
INSERT INTO profile_groups SELECT
@gid, id FROM auth_user WHERE email LIKE '%@mozilla...;
@jsocol
jsocol / gist:1242521
Created September 26, 2011 15:38
Moxie report generator
#!/usr/bin/env python
"""Written for Python 2.5. If 2.6, remove the from __future__ import."""
from __future__ import with_statement
import optparse
import os
import stat
import sys
TEMPLATE = """<?php