Skip to content

Instantly share code, notes, and snippets.

@pinkhominid
Created August 22, 2020 02:58
Show Gist options
  • Save pinkhominid/60ae1206d7b85deba2f8f5318f45e8c9 to your computer and use it in GitHub Desktop.
Save pinkhominid/60ae1206d7b85deba2f8f5318f45e8c9 to your computer and use it in GitHub Desktop.
Dedent code block
export function dedent(code) {
let spacesToTrim;
return code.split('\n').reduce((acc, line) => {
if (line.trim().length) {
if (spacesToTrim === undefined) {
spacesToTrim = /^\s*/.exec(line)[0].length;
}
acc += line.substring(spacesToTrim) + '\n';
} else acc += '\n';
return acc;
}, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment