Skip to content

Instantly share code, notes, and snippets.

@groner
Created August 10, 2016 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groner/049471edae56647e22a6101d8f667293 to your computer and use it in GitHub Desktop.
Save groner/049471edae56647e22a6101d8f667293 to your computer and use it in GitHub Desktop.
function dedent(block) {
// Find the common leading space in a block of text, then remove it. Tabs are NOT handled.
//let spaces = Math.min.apply(null, block.match(/^ +(?=[^ ])/gm).map((ws) => ws.length));
//let spaces = block.match(/^ +(?=[^ ])/gm).map((ws) => ws.length).reduce(Math.min);
let spaces = block.match(/^ +(?=[^ ])/gm).reduce((a,b) => a<b?a:b).length;
return block.replace(new RegExp(`^ {1,${spaces}}`, 'gm'), '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment