Skip to content

Commit 0f3e91f

Browse files
authored
testUtils: refactor out dedentString from dedent (#3010)
1 parent 5ed55b8 commit 0f3e91f

File tree

2 files changed

+44
-28
lines changed

2 files changed

+44
-28
lines changed

src/__testUtils__/__tests__/dedent-test.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4-
import { dedent } from '../dedent';
4+
import { dedent, dedentString } from '../dedent';
55

6-
describe('dedent', () => {
6+
describe('dedentString', () => {
77
it('removes indentation in typical usage', () => {
8-
const output = dedent`
8+
const output = dedentString(`
99
type Query {
1010
me: User
1111
}
@@ -14,7 +14,7 @@ describe('dedent', () => {
1414
id: ID
1515
name: String
1616
}
17-
`;
17+
`);
1818
expect(output).to.equal(
1919
[
2020
'type Query {',
@@ -30,23 +30,23 @@ describe('dedent', () => {
3030
});
3131

3232
it('removes only the first level of indentation', () => {
33-
const output = dedent`
33+
const output = dedentString(`
3434
first
3535
second
3636
third
3737
fourth
38-
`;
38+
`);
3939
expect(output).to.equal(
4040
['first', ' second', ' third', ' fourth'].join('\n'),
4141
);
4242
});
4343

4444
it('does not escape special characters', () => {
45-
const output = dedent`
45+
const output = dedentString(`
4646
type Root {
4747
field(arg: String = "wi\th de\fault"): String
4848
}
49-
`;
49+
`);
5050
expect(output).to.equal(
5151
[
5252
'type Root {',
@@ -57,38 +57,49 @@ describe('dedent', () => {
5757
});
5858

5959
it('also removes indentation using tabs', () => {
60-
const output = dedent`
60+
const output = dedentString(`
6161
\t\t type Query {
6262
\t\t me: User
6363
\t\t }
64-
`;
64+
`);
6565
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
6666
});
6767

6868
it('removes leading and trailing newlines', () => {
69-
const output = dedent`
69+
const output = dedentString(`
7070
7171
7272
type Query {
7373
me: User
7474
}
7575
7676
77-
`;
77+
`);
7878
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
7979
});
8080

8181
it('removes all trailing spaces and tabs', () => {
82-
const output = dedent`
82+
const output = dedentString(`
8383
type Query {
8484
me: User
8585
}
86-
\t\t \t `;
86+
\t\t \t `);
8787
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
8888
});
8989

9090
it('works on text without leading newline', () => {
91-
const output = dedent` type Query {
91+
const output = dedentString(` type Query {
92+
me: User
93+
}
94+
`);
95+
expect(output).to.equal(['type Query {', ' me: User', '}'].join('\n'));
96+
});
97+
});
98+
99+
describe('dedent', () => {
100+
it('removes indentation in typical usage', () => {
101+
const output = dedent`
102+
type Query {
92103
me: User
93104
}
94105
`;

src/__testUtils__/dedent.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
export function dedentString(string: string): string {
2+
const trimmedStr = string
3+
.replace(/^\n*/m, '') // remove leading newline
4+
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs
5+
6+
// fixes indentation by removing leading spaces and tabs from each line
7+
let indent = '';
8+
for (const char of trimmedStr) {
9+
if (char !== ' ' && char !== '\t') {
10+
break;
11+
}
12+
indent += char;
13+
}
14+
15+
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
16+
}
17+
118
/**
219
* An ES6 string tag that fixes indentation and also trims string.
320
*
@@ -25,17 +42,5 @@ export function dedent(
2542
}
2643
}
2744

28-
const trimmedStr = str
29-
.replace(/^\n*/m, '') // remove leading newline
30-
.replace(/[ \t\n]*$/, ''); // remove trailing spaces and tabs
31-
32-
// fixes indentation by removing leading spaces and tabs from each line
33-
let indent = '';
34-
for (const char of trimmedStr) {
35-
if (char !== ' ' && char !== '\t') {
36-
break;
37-
}
38-
indent += char;
39-
}
40-
return trimmedStr.replace(RegExp('^' + indent, 'mg'), ''); // remove indent
45+
return dedentString(str);
4146
}

0 commit comments

Comments
 (0)