Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 09861ee

Browse files
committed
feat(@angular-devkit/core): add trimNewlines literal tag
1 parent 22626d1 commit 09861ee

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/angular_devkit/core/src/utils/literals.ts

+11
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,14 @@ export function stripIndents(strings: TemplateStringsArray, ...values: any[]) {
5555
.join('\n')
5656
.trim();
5757
}
58+
59+
// tslint:disable-next-line:no-any
60+
export function trimNewlines(strings: TemplateStringsArray, ...values: any[]) {
61+
const endResult = String.raw(strings, ...values);
62+
63+
return endResult
64+
// Remove the newline at the start.
65+
.replace(/^(?:\r?\n)+/, '')
66+
// Remove the newline at the end and following whitespace.
67+
.replace(/(?:\r?\n(?:\s*))$/, '');
68+
}

packages/angular_devkit/core/src/utils/literals_spec.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { oneLine, stripIndent, stripIndents } from './literals';
8+
import { oneLine, stripIndent, stripIndents, trimNewlines } from './literals';
99

1010
describe('literals', () => {
1111
describe('stripIndent', () => {
@@ -43,4 +43,14 @@ describe('literals', () => {
4343
expect(test).toBe('hello world how are you? blue red test');
4444
});
4545
});
46+
47+
describe('trimNewlines', () => {
48+
it('works', () => {
49+
const test = trimNewlines`
50+
hello world
51+
`;
52+
53+
expect(test).toBe(' hello world');
54+
});
55+
});
4656
});

0 commit comments

Comments
 (0)