Skip to content

Instantly share code, notes, and snippets.

@malthe
Created December 16, 2014 11:25
Show Gist options
  • Save malthe/02350255c759d5478e89 to your computer and use it in GitHub Desktop.
Save malthe/02350255c759d5478e89 to your computer and use it in GitHub Desktop.
This is similar to Python's "textwrap.dedent" function
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