Skip to content

Instantly share code, notes, and snippets.

@zapthedingbat
zapthedingbat / jquery.valuechange.js
Created March 19, 2014 10:21
jQuery Valuechange event and handler
/* Valuechange event and handler
* See: http://learn.jquery.com/events/event-extensions/
*/
(function($) {
var name = "valuechange";
$.event.special[name] = {
// Event is bound
@zapthedingbat
zapthedingbat / unshiftContext.js
Created April 17, 2014 16:51
Creates a function that executes with the given context and pushes the original context to the first argument of the function.
// Creates a function that executes with the given context and pushes the original context to the first argument of the function.
unshiftContext: function(func, thisArg) {
return function() {
var args = Array.prototype.slice.call(arguments);
args.unshift(this);
func.apply(thisArg, args);
};
},
@zapthedingbat
zapthedingbat / postcode.js
Last active August 29, 2015 14:04
Format a UK postcode to include spaces that may have been omitted.
function formatPostcode(postcode){
return(
//if character fourth from the right is not space
postcode.slice(-4,-3) !== " "
//insert space three characters from the right
?postcode.slice(0,-3) + " " + postcode.slice(-3)
//otherwise use origional
:postcode
)
// upper case
@zapthedingbat
zapthedingbat / wait.js
Created July 30, 2014 13:10
jquery method returns a function that returns a promise that resolves when all the promises returned by specified functions resolve/reject
function wait(/* functions returning promises*/) {
return (function() {
return $.when.apply($, Array.prototype.map.call(actions, function(a) { return a(); }));
}).bind(Array.prototype.slice.call(arguments));
}
@zapthedingbat
zapthedingbat / hash.js
Created August 6, 2014 15:31
Basic hash code function
function hash(s){
return Array.prototype.reduce.call(s, function(a,b){ a = (( a << 5) -a ) + b.charCodeAt(0); return a&a }, 0);
}
@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;
}