F1e3ab214a976a39cfd713bc93deb10f

Cleaner way?

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 !

A8d3f35baafdaea851914b17dae9e1fc

Adam

April 24, 2009, April 24, 2009 16:31, permalink

No rating. Login to rate!

Edit: Nevermind. Missed the multi-line part.

F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 24, 2009, April 24, 2009 18:41, permalink

No rating. Login to rate!

:D

C482376fce4fe48eea836bc2d9491a4e

Andrew Pawson

April 24, 2009, April 24, 2009 19:15, permalink

No rating. Login to rate!

...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));
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

April 24, 2009, April 24, 2009 20:15, permalink

No rating. Login to rate!

Ya this is a little to adhoc to consider as a core prototype. + semicolons == lame

Your refactoring





Format Copy from initial code

or Cancel