Skip to content

Instantly share code, notes, and snippets.

@zapthedingbat
zapthedingbat / fiddle.html
Last active December 14, 2015 04:39
Taggable jQuery plugin
<div id="tagme" style="height:500px;width:500px;background-color:#DDF"></div>
@zapthedingbat
zapthedingbat / markov.js
Created March 21, 2013 00:38
Markov Chain implementation
/*
Copyright (C) 2013 Sam Greenhalgh - RadicalResearch Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH TH
@zapthedingbat
zapthedingbat / gist:7908110
Created December 11, 2013 10:23
Debouncer Only executes the most recent call after the specified timeout.
// Only executes the most recent call after the specified timeout.
function Debouncer() {
var timeout;
this.execute = function(callback, wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
@zapthedingbat
zapthedingbat / Object.extend.js
Created December 13, 2013 10:08
Extend method copies properties from one object to another. Requires JavaScript 1.8.5+
Object.extend = function(toObj, fromObj){
var p = Object.getOwnPropertyNames(fromObj),
i = p.length;
while(i--)
{
Object.defineProperty(toObj, p[i], Object.getOwnPropertyDescriptor(fromObj, p[i]));
}
};
@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 / logeverything.js
Created August 19, 2014 12:22
Log every function call to the console
(function() {
var call = Function.prototype.call;
Function.prototype.call = function() {
console.log(this, arguments);
return call.apply(this, arguments);
};
}());