Created
August 22, 2020 02:58
-
-
Save pinkhominid/60ae1206d7b85deba2f8f5318f45e8c9 to your computer and use it in GitHub Desktop.
Dedent code block
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
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