mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
feat(Angular.js): skip JSON.stringify for undefined
Return early in `angular.toJson` if the object to be stringified is `undefined`. IE8 stringifies `undefined` to `'undefined'` whereas other browsers return `undefined`. This normalizes behavior and passes currently broken unit tests in IE8.
This commit is contained in:
parent
ccda0f3509
commit
5a294c8646
2 changed files with 6 additions and 1 deletions
|
|
@ -775,9 +775,10 @@ function toJsonReplacer(key, value) {
|
|||
*
|
||||
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
|
||||
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
|
||||
* @returns {string} Jsonified string representing `obj`.
|
||||
* @returns {string|undefined} Jsonified string representing `obj`.
|
||||
*/
|
||||
function toJson(obj, pretty) {
|
||||
if (typeof obj === 'undefined') return undefined;
|
||||
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -909,6 +909,10 @@ describe('angular', function() {
|
|||
it('should not serialize scope instances', inject(function($rootScope) {
|
||||
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
|
||||
}));
|
||||
|
||||
it('should serialize undefined as undefined', function() {
|
||||
expect(toJson(undefined)).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue