Skip to content

Instantly share code, notes, and snippets.

View aseemk's full-sized avatar

Aseem Kishore aseemk

View GitHub Profile
@aseemk
aseemk / github.com.js
Created May 9, 2012 23:34
dotjs script for GitHub: account for whitespace in commit diffs!
// if this is a commit page, properly account for whitespace in diffs!
// XXX this works by adding ?w=1 to the URL, which requires a page reload. =/
// to prevent this behavior, change the URL to ?w. (?w=0 and ?w= don't work!)
// TODO consider changing this behavior to add a link/button instead?
// TODO is the .page-commit-show class the right thing to base this on?
if ($(document.body).hasClass('page-commit-show')) {
// XXX this'll overwrite any other query params, but good enough for now?
// also note that setting location.search -- even to whatever it already
// is -- causes a page change, so only set it if to a different value!
// TODO use location.replace() instead to prevent new history entry, but
@aseemk
aseemk / migrate._coffee
Created May 10, 2012 22:34
Helper script to migrate Streamline files to the new ._js/._coffee extensions.
#!/usr/bin/env _coffee
#
# Migrates our Streamline v0.1 files to the new Streamline v0.3 extensions,
# i.e. git-moves all foo_.coffee files to foo._coffee.
{exec} = require 'child_process'
Path = require 'path'
# regex to recognize old-style streamline files:
STREAMLINE_REGEX = /_\.(js|coffee)$/
@aseemk
aseemk / gist:2939002
Created June 15, 2012 22:27
CoffeeScript's interesting line continuation behavior
# all on one long line works:
if not foo or not bar or not baz = Some.reallyLongMethodName foo, bar, _
error = true
# newline with matched three-space indentation level works:
if not foo or not bar or
not baz = Some.reallyLongMethodName foo, bar, _
error = true
# but newline with one regular indentation level *doesn't* work:
@aseemk
aseemk / randomStr.js
Created July 12, 2012 05:00
Random alphanumeric (base-62) strings in Node.js, cryptographically strong
var bases = require('bases');
var crypto = require('crypto');
// Returns a base-62 (alphanumeric only) string of the given length:
function randomStr(length) {
// We generate a random number in a space at least as big as 62^length,
// and if it's too big, we just retry. This is still statistically O(1)
// since repeated probabilities less than one converge to zero. Hat-tip to
// a Google interview for teaching me this technique! ;)
@aseemk
aseemk / bases.md
Created July 16, 2012 18:26
JavaScript utility to convert numbers to different bases/alphabets.
@aseemk
aseemk / gist:3905781
Created October 17, 2012 14:23
Wrapper script around gmvault that allows setting default options.
#!/usr/bin/env bash
#
# Wrapper around gmvault binary that sets default options.
GMVAULT_DIR="$HOME/Dropbox/Applications/Utilities/gmvault-v1.7-beta/bin/"
DB_DIR="$HOME/Dropbox/.gmvault-db"
# Run in a sub-shell since we need to change directories:
(
cd $GMVAULT_DIR
@aseemk
aseemk / gist:3966223
Created October 27, 2012 20:57
Neo4j mutable Cypher, e.g. creating comments (node + relationships + update stats)
results = @query _, """
START author=node({authorId}), target=node({targetId})
CREATE comment={commentData}
CREATE comment -[:#{@REL_AUTHOR} {relAuthorData}]-> author
CREATE comment <-[:#{@REL_COMMENT} {relTargetData}]- target
SET author.numCommentsBy = COALESCE(author.numCommentsBy?, 0) + 1
SET target.numComments = COALESCE(target.numComments?, 0) + 1
RETURN comment
""",
authorId: author.id
@aseemk
aseemk / gist:3966253
Created October 27, 2012 21:04
GitHub post-receive hook error
$ git push
Counting objects: 104, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (83/83), done.
Writing objects: 100% (83/83), 12.38 KiB, done.
Total 83 (delta 64), reused 0 (delta 0)
error: hooks/post-receive died of signal 2
remote: /data/github/current/vendor/gems/ruby/1.8/gems/redis-2.2.0/lib/redis/connection/hiredis.rb:23:in `connect': Timeout::Error (Timeout::Error)
remote: from /data/github/current/vendor/gems/ruby/1.8/gems/redis-2.2.0/lib/redis/client.rb:204:in `establish_connection'
remote: from /data/github/current/vendor/gems/ruby/1.8/gems/redis-2.2.0/lib/redis/client.rb:23:in `connect'
@aseemk
aseemk / app.coffee
Last active December 10, 2015 16:38
A simple Restify API server example.
bunyan = require 'bunyan'
restify = require 'restify'
requireDir = require 'require-dir'
middleware = requireDir './middleware'
routes = requireDir './routes'
settings = require './settings'
app = restify.createServer
name: 'Foo Bar API'