Skip to content
This repository has been archived by the owner on Apr 9, 2022. It is now read-only.

Commit

Permalink
feat(@angular-devkit/core): add trimNewlines literal tag
Browse files Browse the repository at this point in the history
  • Loading branch information
filipesilva committed May 18, 2018
1 parent 22626d1 commit 09861ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/angular_devkit/core/src/utils/literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ export function stripIndents(strings: TemplateStringsArray, ...values: any[]) {
.join('\n')
.trim();
}

// tslint:disable-next-line:no-any
export function trimNewlines(strings: TemplateStringsArray, ...values: any[]) {
const endResult = String.raw(strings, ...values);

return endResult
// Remove the newline at the start.
.replace(/^(?:\r?\n)+/, '')
// Remove the newline at the end and following whitespace.
.replace(/(?:\r?\n(?:\s*))$/, '');
}
12 changes: 11 additions & 1 deletion packages/angular_devkit/core/src/utils/literals_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { oneLine, stripIndent, stripIndents } from './literals';
import { oneLine, stripIndent, stripIndents, trimNewlines } from './literals';

describe('literals', () => {
describe('stripIndent', () => {
Expand Down Expand Up @@ -43,4 +43,14 @@ describe('literals', () => {
expect(test).toBe('hello world how are you? blue red test');
});
});

describe('trimNewlines', () => {
it('works', () => {
const test = trimNewlines`
hello world
`;

expect(test).toBe(' hello world');
});
});
});

0 comments on commit 09861ee

Please sign in to comment.