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

Commit 332a3c7

Browse files
xdissentpkozlowski-opensource
authored andcommitted
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.
1 parent fcd761b commit 332a3c7

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/Angular.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,10 @@ function toJsonReplacer(key, value) {
751751
*
752752
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
753753
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
754-
* @returns {string} Jsonified string representing `obj`.
754+
* @returns {string|undefined} Jsonified string representing `obj`.
755755
*/
756756
function toJson(obj, pretty) {
757+
if (typeof obj === 'undefined') return undefined;
757758
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
758759
}
759760

test/AngularSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -879,5 +879,9 @@ describe('angular', function() {
879879
it('should not serialize scope instances', inject(function($rootScope) {
880880
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
881881
}));
882+
883+
it('should serialize undefined as undefined', function() {
884+
expect(toJson(undefined)).toEqual(undefined);
885+
});
882886
});
883887
});

0 commit comments

Comments
 (0)