Created
December 16, 2014 11:25
-
-
Save malthe/02350255c759d5478e89 to your computer and use it in GitHub Desktop.
This is similar to Python's "textwrap.dedent" function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dedent(text) { | |
var re_whitespace = /^([ \t]*)(.*)\n/gm; | |
var l, m, i; | |
while ((m = re_whitespace.exec(text)) !== null) { | |
if (!m[2]) continue; | |
if (l = m[1].length) { | |
i = (i !== undefined) ? Math.min(i, l) : l; | |
} else break; | |
} | |
if (i) | |
text = text.replace(new RegExp('^[ \t]{' + i + '}(.*\n)', 'gm'), '$1'); | |
return text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment