Skip to content

Instantly share code, notes, and snippets.

@zapthedingbat
zapthedingbat / onceMore.js
Created September 30, 2014 09:19
Calling the returned function signals to run the specified function or, if the function is already executing, flag that it should run once more.
function onceMore(f) {
var isWorking = false,
isWaiting = false;
function execute() {
isWaiting = false;
isWorking = true;
f().then(complete);
}
@zapthedingbat
zapthedingbat / NServiceBusTimeoutRegister.cs
Created October 28, 2014 13:13
Timeout calls to IBus.Send().Register()
// NOTE: There is a bug in NServiceBus where registered callbacks that are not invoked remain in a static dictionary.
var asyncResult = _bus.Send(message).Register(ar => { }, null);
if (!asyncResult.AsyncWaitHandle.WaitOne(_timeout))
{
// Signal the async result so callback threads don't remain blocked
((EventWaitHandle)asyncResult.AsyncWaitHandle).Set();
throw new OperationCanceledException();
}
@zapthedingbat
zapthedingbat / HasAttributeConstraint.cs
Created November 4, 2014 19:13
NUnit Has.Attribute<T> Constraint asserts an attribute is applied to a memberInfo with a given set of construction arguments.
public static class Has
{
public static IResolveConstraint Attribute<T>() where T : class
{
return new Constraints.AttributeConstraint<T>(new Dictionary<string, object>(), new object[0]);
}
public static IResolveConstraint Attribute<T>(IDictionary<string, object> namedConstructorArguments, params object[] constructorArguments) where T : class
{
return new Constraints.AttributeConstraint<T>(namedConstructorArguments, constructorArguments);
@zapthedingbat
zapthedingbat / unpatch.js
Created November 29, 2014 21:20
Remove a monkey-patch from a JavaScript method.
// Unpatch.js
// Sam Greenhalgh
// http://RadicalResearch.co.uk/
// Example usage
// window.console.log = unpatch(window.console.log);
function unpatch(method) {
var _ = Function.prototype.apply;
var r;
@zapthedingbat
zapthedingbat / ClassicInheritance.js
Created March 5, 2015 18:08
Classic Inheritance JS
Function.prototype.extends=function(base){
var t = function () {}
t.prototype = base.prototype;
this.prototype = new t();
this.prototype.constructor = this;
};
var Animal = function(){
this.isAlive = true;
}
@zapthedingbat
zapthedingbat / chromebackbug.html
Last active February 17, 2016 18:04
Chrome Unload Back Bug
<!DOCTYPE html>
<html>
<head>
<script>
window.addEventListener('unload', function(){
var origionalUrl = location.href;
var newUrl = origionalUrl.replace('#.*$', '') + '#nope';
console.log('cancel navigation', newUrl);
window.location.replace(newUrl);
window.location.replace(origionalUrl);

Keybase proof

I hereby claim:

  • I am zapthedingbat on github.
  • I am zapthedingbat (https://keybase.io/zapthedingbat) on keybase.
  • I have a public key whose fingerprint is 560A 68F0 73EF 2F76 AE8C 014F 35C4 4D58 1084 3365

To claim this, I am signing this object:

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ZapTheDingbat.IO
{
public class CsvReader : IEnumerable<IReadOnlyList<string>>, IEnumerator<IReadOnlyList<string>>
const runs = 10;
function run(method, runIndex) {
console.time(`run ${runIndex}`);
return method().then(() => {
console.timeEnd(`run ${runIndex}`);
if (runIndex < runs){
return run(method, runIndex+1);
}
});
@zapthedingbat
zapthedingbat / birthday.js
Created February 24, 2017 11:18
Calculating the probability that a member of a team shares their birthday with someone else in the same team.
//https://en.wikipedia.org/wiki/Birthday_problem#Calculating_the_probability
function p(n){
let a = 1;
for(let i = 0; i < n; i++){
a *= ((365 - i) / 365);
}
return 1 - a;
}
const chance = p(parseInt(process.argv.pop())) * 100;
console.log(`There is a ${chance}% chance of two people in the same team sharing the same birthday`);