Skip to content

Commit

Permalink
fix(core): stringify shouldn't throw when toString returns null/undef…
Browse files Browse the repository at this point in the history
…ined (angular#14975)

Fixes angular#14948

PR Close angular#14975
  • Loading branch information
DzmitryShylovich authored and juleskremer committed Aug 24, 2017
1 parent ee24758 commit e54e865
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export function stringify(token: any): string {
}

const res = token.toString();

if (res == null) {
return '' + res;
}

const newLineIndex = res.indexOf('\n');
return newLineIndex === -1 ? res : res.substring(0, newLineIndex);
}
19 changes: 19 additions & 0 deletions packages/core/test/util_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* 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 {stringify} from '../src/util';

export function main() {
describe('stringify', () => {
it('should return string undefined when toString returns undefined',
() => expect(stringify({toString: (): string => undefined})).toBe('undefined'));

it('should return string null when toString returns null',
() => expect(stringify({toString: (): string => null})).toBe('null'));
});
}

0 comments on commit e54e865

Please sign in to comment.