indent = function(string, indents) {
indents = (new Array(indents)).join(' ')
return string.replace(/^/gm, function(){ return indents })
}
// Examples
print(indent(indent.toString(), 1))
print(indent(indent.toString(), 2))
Refactorings
No refactoring yet !
Andrew Pawson
April 24, 2009, April 24, 2009 19:15, permalink
...not really any cleaner, and generally bad practice, but it may be more convenient to modify the String object itself...
String.prototype.indent = function(indents) {
indents = (new Array(indents)).join(' ');
return this.replace(/^/gm, function(){ return indents });
};
print(indent.toString().indent(20));
Tj Holowaychuk
April 24, 2009, April 24, 2009 20:15, permalink
Ya this is a little to adhoc to consider as a core prototype. + semicolons == lame
Cleaner way?