The repeatSpaces function: I must have missed something.
Could you not use a built-in like String.repeat ? (works in Chrome 31)
If it is not available a shim like:
https://stackoverflow.com/a/202627/684890
String.prototype.repeat = function repeat(repeatTimes)
{
return new Array(repeatTimes+ 1).join(this);
};
would be sufficient?