Skip to content

Instantly share code, notes, and snippets.

View jdiamond's full-sized avatar

Jason Diamond jdiamond

  • 04:15 (UTC -07:00)
View GitHub Profile
using System;
using NUnit.Framework;
namespace BehaveN.Tests
{
[TestFixture]
public class Describe_NameParser
{
[Test]
public void it_should_throw_an_exception_when_parsing_null()
@jdiamond
jdiamond / indexeddb.html
Created September 16, 2012 22:25
IndexedDB Example
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>IndexedDB Example</title>
</head>
<body>
<h1>IndexedDB Example</h1>
<p>
@jdiamond
jdiamond / mongoose-as-promised.js
Created December 30, 2012 18:58
Mongoose as Promised
var mongoose = require('mongoose');
var Q = require('q');
mongoose.Promise.prototype.then = function(fulfilled, rejected) {
var deferred = Q.defer();
this.addCallback(deferred.resolve);
this.addErrback(deferred.reject);
return deferred.promise.then(fulfilled, rejected);
@jdiamond
jdiamond / promises.md
Last active December 16, 2015 13:19
Promise example for Domenic

Problem

This is part of the router in a single-page application I'm working on:

function navigateTo(url) {
    return findOrCreatePageContainer(url)      // finds div or creates new one
        .then(loadAndInitPageIfNotAlreadyDone) // loads content into div
        .catch(injectErrorMessage)             // injects error message into div
        .then(transitionToNewPage);            // displays content in div

Your original example:

public virtual List<UserDC> GetAllActiveAgentsByReason(int reasonID)
{
    try
    {
        if (reasonID < 0)
        {
 throw new ArgumentException("An invalid reasonID was passed.");
@jdiamond
jdiamond / require-mustache.js
Created May 2, 2013 02:31
RequireJS Mustache plugin
define([ 'text', 'mustache' ], function(text, Mustache) {
return {
load: function(name, require, callback, config) {
text.load(name, require, function(text) {
callback(Mustache.compile(text));
}, config);
}
};
});
var arr1 = [ { name: 'Nikki' }, { name: 'Jason' }, { name: 'Thanh' } ];
var arr2 = [ { name: 'Nikki' }, { name: 'Thanh' } ];
var index = {};
arr1.forEach(function(item) {
index[item.name] = item;
});
console.log(index);
@jdiamond
jdiamond / Vagrantfile
Created August 11, 2014 17:39
Yeoman Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
INSTALL_NODE = <<SCRIPT
sudo add-apt-repository -y ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install -y nodejs
@jdiamond
jdiamond / memory.js
Last active August 29, 2015 14:19
Watch memory and dump heaps in Node.js
function startWatchingMemory() {
if (process.env.MEMWATCH) {
var memwatch = require('memwatch-next');
var heapdump = process.env.HEAPDUMP && require('heapdump');
var hd = null;
memwatch.on('leak', function(info) {
log(info);
@jdiamond
jdiamond / swap.sh
Created June 2, 2015 17:49
Enable swapfile on Ubuntu
# create the swapfile:
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
# enable it:
swapon /swapfile
sysctl vm.swappiness=10
sysctl vm.vfs_cache_pressure=50