Skip to content

Commit 1b42a87

Browse files
committed
Introduce smart trimming to fix #37
1 parent b000781 commit 1b42a87

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

src/stripIndent/stripIndent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import trimResultTransformer from '../trimResultTransformer';
44

55
const stripIndent = createTag(
66
stripIndentTransformer(),
7-
trimResultTransformer(),
7+
trimResultTransformer('smart'),
88
);
99

1010
export default stripIndent;

src/stripIndentTransformer/stripIndentTransformer.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { readFromFixture } from '../testUtils';
66
test('default behaviour removes the leading indent, but preserves the rest', () => {
77
const stripIndent = createTag(
88
stripIndentTransformer(),
9-
trimResultTransformer(),
9+
trimResultTransformer('smart'),
1010
);
1111
const expected = readFromFixture(__dirname, 'stripIndent');
1212
const actual = stripIndent`
@@ -21,7 +21,7 @@ test('default behaviour removes the leading indent, but preserves the rest', ()
2121
test('type "initial" does not remove indents if there is no need to do so', () => {
2222
const stripIndent = createTag(
2323
stripIndentTransformer(),
24-
trimResultTransformer(),
24+
trimResultTransformer('smart'),
2525
);
2626
expect(stripIndent``).toBe('');
2727
expect(stripIndent`foo`).toBe('foo');
@@ -31,7 +31,7 @@ test('type "initial" does not remove indents if there is no need to do so', () =
3131
test('removes all indents if type is "all"', () => {
3232
const stripIndents = createTag(
3333
stripIndentTransformer('all'),
34-
trimResultTransformer(),
34+
trimResultTransformer('smart'),
3535
);
3636
const expected = readFromFixture(__dirname, 'stripIndents');
3737
const actual = stripIndents`

src/stripIndents/stripIndents.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import trimResultTransformer from '../trimResultTransformer';
44

55
const stripIndents = createTag(
66
stripIndentTransformer('all'),
7-
trimResultTransformer(),
7+
trimResultTransformer('smart'),
88
);
99

1010
export default stripIndents;

src/trimResultTransformer/trimResultTransformer.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const supportedSides = ['', 'start', 'left', 'end', 'right'];
1+
const supportedSides = ['', 'start', 'left', 'end', 'right', 'smart'];
22

33
/**
44
* TemplateTag transformer that trims whitespace on the end result of a tagged template
@@ -23,6 +23,9 @@ const trimResultTransformer = (side = '') => {
2323
case 'end':
2424
case 'right':
2525
return endResult.replace(/\s*$/, '');
26+
27+
case 'smart':
28+
return endResult.replace(/[^\S\n]+$/gm, '').replace(/^\n/, '');
2629
}
2730
},
2831
};

src/trimResultTransformer/trimResultTransformer.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ test('can be used sequentially', () => {
3636
expect(trimStart` bar `).toBe('bar ');
3737
});
3838

39+
describe('smart trimming', () => {
40+
const trimSmart = createTag(trimResultTransformer('smart'));
41+
42+
test('leaves a string without surrounding whitespace as-is', () => {
43+
expect(trimSmart`a`).toBe('a');
44+
});
45+
46+
test('performs an end-side trim on a single-line string', () => {
47+
expect(trimSmart` a `).toBe(' a');
48+
});
49+
50+
test('trims whitespace at the end of each line', () => {
51+
expect(trimSmart`a \n b \nc `).toBe('a\n b\nc');
52+
});
53+
54+
test("removes the first line if it's empty", () => {
55+
expect(trimSmart` \na`).toBe('a');
56+
});
57+
58+
test('leaves the trailing newline character', () => {
59+
expect(trimSmart`a \n`).toBe('a\n');
60+
});
61+
62+
test("doesn't remove intentional newline characters", () => {
63+
expect(trimSmart`a\n \n`).toBe('a\n\n');
64+
});
65+
});
66+
3967
test('throws an error if invalid side supplied', () => {
4068
expect(() => {
4169
trimResultTransformer('up');

0 commit comments

Comments
 (0)