Skip to content

Instantly share code, notes, and snippets.

@cesarfigueroa
Created May 29, 2017 22:38
Show Gist options
  • Save cesarfigueroa/5e8566b0aaff4436da717fbeced48122 to your computer and use it in GitHub Desktop.
Save cesarfigueroa/5e8566b0aaff4436da717fbeced48122 to your computer and use it in GitHub Desktop.
const WHITESPACE_PATTERN = /^[ \t]*(?=\S)/gm;
export function stripIndent(strings, ...expressions) {
const resultString = strings.reduce((accumulator, part, i) => {
return accumulator + expressions[i - 1] + part;
});
const indents = Math.min(
...resultString.match(WHITESPACE_PATTERN).map(whitespace => whitespace.length)
);
return resultString.split('\n').map(line => line.slice(indents)).join('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment