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

Commit 5a294c8

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 ccda0f3 commit 5a294c8

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
@@ -775,9 +775,10 @@ function toJsonReplacer(key, value) {
775775
*
776776
* @param {Object|Array|Date|string|number} obj Input to be serialized into JSON.
777777
* @param {boolean=} pretty If set to true, the JSON output will contain newlines and whitespace.
778-
* @returns {string} Jsonified string representing `obj`.
778+
* @returns {string|undefined} Jsonified string representing `obj`.
779779
*/
780780
function toJson(obj, pretty) {
781+
if (typeof obj === 'undefined') return undefined;
781782
return JSON.stringify(obj, toJsonReplacer, pretty ? ' ' : null);
782783
}
783784

test/AngularSpec.js

+4
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,10 @@ describe('angular', function() {
909909
it('should not serialize scope instances', inject(function($rootScope) {
910910
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
911911
}));
912+
913+
it('should serialize undefined as undefined', function() {
914+
expect(toJson(undefined)).toEqual(undefined);
915+
});
912916
});
913917

914918
});

0 commit comments

Comments
 (0)