Commit 98ced81f authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Add helper function to trim and dedent multiline strings

parent bfe223ac
import { trimAndDedent } from '../trim-and-dedent';
describe('trimAndDedent', () => {
[
['Foo', 'Foo'],
[' Foo', 'Foo'],
[
`First line
Second line
Third line`,
`First line
Second line
Third line`,
],
[
`
Hello, Jane!
Indented line
Goodbye, John!
`,
`Hello, Jane!
Indented line
Goodbye, John!`,
],
].forEach(([str, expectedResult]) => {
it('normalizes strings with multiple lines', () => {
const result = trimAndDedent(str);
assert.equal(result, expectedResult);
});
});
});
/**
* Remove leading and trailing empty lines from a string.
*/
function trimEmptyLines(str: string): string {
return str.replace(/^\s*\n|\n\s*$/g, '');
}
/**
* Remove common indentation from each line of a string.
*/
function dedent(str: string) {
// Match the smallest indentation
const match = str.match(/^[ \t]*(?=\S)/gm);
const indent = match && Math.min(...match.map(el => el.length));
if (indent) {
const regexp = new RegExp(`^ {${indent}}`, 'gm');
return str.replace(regexp, '');
}
return str;
}
/**
* Remove leading and trailing empty lines from a string, then remove common
* indentation from each line.
*
* This is useful to improve readability for multi-line template literals,
* where you don't want to
* 1) start the first line right after the first backtick.
* 2) indent the string inconsistently with the rest of the code.
*
* This function allows to move from this:
* function foo(arg) {
* if (arg === 3) {
* console.log(`First line
* Second line
* Third line`);
* }
* }
*
* to this:
* function foo(arg) {
* if (arg === 3) {
* console.log(trimAndDedent(`
* First line
* Second line
* Third line
* `));
* }
* }
*/
export function trimAndDedent(str: string): string {
return dedent(trimEmptyLines(str));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment