fix(core): stringify shouldn't throw when toString returns null/undefined (#14975)

Fixes #14948

PR Close #14975
This commit is contained in:
Dzmitry Shylovich 2017-03-07 10:31:28 +03:00 committed by Miško Hevery
parent 0759911431
commit 8e6995c91e
2 changed files with 24 additions and 0 deletions

View File

@ -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);
}

View File

@ -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'));
});
}