I've got a few solutions to this here:

https://gist.github.com/dlmanning/2cde81e4bbbea87ed5e3