Skip to content

Instantly share code, notes, and snippets.

View timruffles's full-sized avatar

Tim Ruffles timruffles

View GitHub Profile
@timruffles
timruffles / normalizeCoordinates.js
Created October 22, 2015 15:50
normalizes a set of positive coordinates to 0..1
function normaliseCoordinates(asObjects) {
var dimensions = {
x: [Infinity, -Infinity],
y: [Infinity, -Infinity],
}
var axisToDeltas = {
x: "width",
y: "height",
};
@timruffles
timruffles / google-sheets-formula.vb
Last active March 6, 2025 23:57
google sheets - uk stamp duty calculator, new rate (2015)
// put this into a cell and then name a range 'housePrice'
=MIN(MAX(0,housePrice-250000),250000-125000)*0.02 + MIN(MAX(0,housePrice - 250000), 925000-250000) * 0.05 + MIN(MAX(0,housePrice - 9250000), 1500000-925000) * 0.1
@timruffles
timruffles / svg-html-entities.js
Created May 27, 2015 08:29
svg - supporting HTML entities, to avoid 'entity not defined' when you're exporting SVGs from the browser
// create a doctype that includes definitions for all HTML entities - http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
var doctype = '<?xml version="1.0" standalone="no"?>' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [ '
+ HTML_ENTITIES() + ' ]>';
function HTML_ENTITIES() {
return '<!ENTITY quot "&#34;"> <!ENTITY amp "&#38;"> <!ENTITY apos "&#39;"> <!ENTITY lt "&#60;"> <!ENTITY gt "&#62;"> <!ENTITY nbsp "&#160;"> <!ENTITY iexcl "&#161;"> <!ENTITY cent "&#162;"> <!ENTITY pound "&#163;"> <!ENTITY curren "&#164;"> <!ENTITY yen "&#165;"> <!ENTITY brvbar "&#166;"> <!ENTITY sect "&#167;"> <!ENTITY uml "&#168;"> <!ENTITY copy "&#169;"> <!ENTITY ordf "&#170;"> <!ENTITY laquo "&#171;"> <!ENTITY not "&#172;"> <!ENTITY shy "&#173;"> <!ENTITY reg "&#174;"> <!ENTITY macr "&#175;"> <!ENTITY deg "&#176;"> <!ENTITY plusmn "&#177;"> <!ENTITY sup2 "&#178;"> <!ENTITY sup3 "&#179;"> <!ENTITY acute "&#180;"> <!ENT
@timruffles
timruffles / ngModelController.js
Created April 23, 2015 12:55
source code for demonstrating using ngModelController with SVG and Angular - full post https://truffles.me.uk/turn-anything-into-an-ng-model-with-ngmodelcontroller
"use strict";
/*
the `clock()` directive the only bit of the code that's handling ngModel - the
rest is basic angular or DOM code
*/
function clockDirective() {
@timruffles
timruffles / gist:e1f14572412f43d09da4
Created April 7, 2015 11:18
custom d3 multi format time scale - get your own formats/intervals into axis
// array of [format string, useFormat]. formats are accessed in order, return true to use the format
// - this is the default - it pretty much just finds the smallest time period that's non-zero
var multiScale = d3.time.format.utc.multi([
[".%L", function(d) { return d.getUTCMilliseconds(); }],
[":%S", function(d) { return d.getUTCSeconds(); }],
["%I:%M", function(d) { return d.getUTCMinutes(); }],
["%I %p", function(d) { return d.getUTCHours(); }],
["%a %d", function(d) { return d.getUTCDay() && d.getUTCDate() != 1; }],
["%b %d", function(d) { return d.getUTCDate() != 1; }],
["%B", function(d) { return d.getUTCMonth(); }],
@timruffles
timruffles / promise-queue.js
Last active August 29, 2015 14:15
quick promise based queue sketch
function queue(concurrency) {
var outstanding = [];
return function(fn) {
outstanding.push(fn);
if(outstanding.length < concurrency) {
pop();
}
}
function pop() {
@timruffles
timruffles / proxy.sh
Last active August 29, 2015 14:14
oauth local testing via ssh proxy
# ssh's into target box, forwards all incoming connections to :3000 to localhost :3030
# -R [bind_address:]port:host:hostport
# -N no remote command - just forward
# -g allows remote to connect to local
#
# you'll need to add `GatewayPorts yes` to your ssh config - e.g at `/etc/ssh/sshd_config` -
# and then restart sshd on the host machine (`service ssh restart` on ubuntu)
# some-host.com:443 -> localhost:4443
ssh -NgR 0.0.0.0:443:localhost:4443 some-host.com
@timruffles
timruffles / record_calls.rb
Last active August 29, 2015 14:13
record calls
module RecordCalls
# usage:
# resque_calls = RecordCalls.on(Resque, :enqueue)
#  resque_calls.called_with? PostTask, :hello, :*, project.id
#
# :* is a placeholder for calls, change RecordCalls.placeholder if you want a different one
@placeholder = :*
class << self
@timruffles
timruffles / test.rb
Created January 9, 2015 18:04
rails - POST JSON in controller tests in rails 3.2.x - can't easily do it, necessary for things as simple as post body containing { action: "something" }
# from http://stackoverflow.com/a/13206873/427710
env = Rack::MockRequest.env_for('/posts/1', :method => 'PUT', <some other params>)
status, headers, body = PostsController.action(:update).call(env)
@timruffles
timruffles / 0_README.md
Last active August 29, 2015 14:10
transforms vs ifs

I was changing the behaviour of this controller, and wondered if there was some way to make it less branchy and more self-documenenting.

I remembered an idea of Dave Thomas's - thinking about programs as series of transforms - and tried to apply it.

Which do you prefer?