Skip to content

Instantly share code, notes, and snippets.

View asalant's full-sized avatar

Alon Salant asalant

  • Good Eggs
  • San Francisco
View GitHub Profile
process.env.NODE_ENV = 'test' unless process.env.NODE_ENV?
sys = require "sys"
_ = require "underscore"
dir = "jasmine-node/lib/jasmine-node/"
filename = "jasmine-2.0.0.rc1"
# Copy 'it', 'describe',... to global
for key, value of require("#{dir}#{filename}")
withSerializedSync = function(cls) {
var sync = cls.prototype.sync || Backbone.sync;
cls.prototype.sync = function() {
var args = arguments.length ? Array.prototype.slice.call(arguments,0) : [];
if (!this._lastSync) {
this._lastSync = sync.apply(this, args);
} else {
var _this = this;
this._lastSync = this._lastSync.then(function() {
return sync.apply(_this, args);
@asalant
asalant / date_reviver_perf.coffee
Last active August 29, 2015 14:19
JSON Date reviver performance investigation
isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/
isIsoDate = (value) ->
return value unless typeof(value) is 'string'
isoDateRegex.test(value)
isIsoDateOptimized = (value) ->
return value unless typeof(value) is 'string'
return value unless value.length is 24
@asalant
asalant / marks.sh
Created August 19, 2013 23:53
Command line bookmarks for zsh on OSX from http://jeroenjanssens.com/2013/08/16/quickly-navigate-your-filesystem-from-the-command-line.html. I put marks.sh in ~/.zsh and source it from ~/.zshrc.
export MARKPATH=$HOME/.marks
function jump {
cd -P "$MARKPATH/$1" 2>/dev/null || echo "No such mark: $1"
}
function mark {
mkdir -p "$MARKPATH"; ln -s "$(pwd)" "$MARKPATH/$1"
}
function unmark {
rm -i "$MARKPATH/$1"
Set mixed { '0': { name: 'Foo' },
'1':
{ path: 'mixed',
instance: undefined,
validators: [],
setters: [ [Function] ],
getters: [],
options: { type: [Function: Mixed] },
_index: null } }
Set mixed { '0': 'Bar',
@asalant
asalant / usage.js
Created December 18, 2012 01:51
Mixin for Backbone Model and Collection classes that will ensure that all ajax calls are run serially instead of concurrently (the default). Works by chaining jQuery Deferred.
var MyModel = Backbone.Model.extend({});
withSerializedSync(MyModel);
@asalant
asalant / coffee-repl.js
Last active March 3, 2018 08:22
CoffeeScript REPL using Node's REPL
#!/usr/bin/env node
var vm = require('vm');
var repl = require('repl');
var CoffeeScript = require('coffee-script');
repl.start({
prompt: "coffee> ",
eval: function(code, context, file, cb) {
try {
@asalant
asalant / gist:4109324
Created November 19, 2012 06:57
fibrous console session
$ node fibrous_repl.js
Starting fibrous REPL...
> var fs = require('fs');
undefined
> data = fs.sync.readFile('/etc/passwd', 'utf-8');
...
> console.log(data);
##
# User Database
#
@asalant
asalant / fibrous_repl.js
Created November 19, 2012 06:38
Run a fibrous node console
var vm = require('vm');
var repl = require('repl');
var fibrous = require('fibrous');
console.log("Starting fibrous REPL...");
repl.start({
eval: fibrous(function(code, context, file) {
return vm.runInContext(code, context, file);
})
});
@asalant
asalant / connectWithRetry.js
Created November 17, 2012 01:24
Retry connecting to mongo if initial connect fails
var mongoose = require('mongoose')
var mongoUrl = "mongodb://localhost:27017/test"
var connectWithRetry = function() {
return mongoose.connect(mongoUrl, function(err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 5 sec', err);
setTimeout(connectWithRetry, 5000);
}
});