Skip to content

Commit

Permalink
Replaced angular.toJson with Utilities.toJson [#CanConHackathon] (#7924)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew-Wise authored Apr 10, 2020
1 parent 1e851b6 commit 76e4b6b
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ angular.module("umbraco.directives")
function jsonToString(object) {
// better than JSON.stringify(), because it formats + filters $$hashKey etc.
// NOTE that this will remove all $-prefixed values
return angular.toJson(object, true);
return Utilities.toJson(object, true);
}

function isValidJson(model) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function authResource($q, $http, umbRequestHelper, angularHelper) {
umbRequestHelper.getApiUrl(
"authenticationApiBaseUrl",
"PostSend2FACode"),
angular.toJson(provider)),
Utilities.toJson(provider)),
'Could not send code');
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function currentUserResource($q, $http, umbRequestHelper, umbDataFormatter) {
umbRequestHelper.getApiUrl(
"currentUserApiBaseUrl",
"PostSetInvitedUserPassword"),
angular.toJson(newPassword)),
Utilities.toJson(newPassword)),
'Failed to change password');
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function macroService() {
}
else {
//if it's not a string we'll send it through the json serializer
var json = angular.toJson(val);
var json = Utilities.toJson(val);
//then we need to url encode it so that it's safe
var encoded = encodeURIComponent(json);
keyVal = key + "=\"" + encoded + "\" ";
Expand Down Expand Up @@ -142,7 +142,7 @@ function macroService() {

if (item.value !== null && item.value !== undefined && !_.isString(item.value)) {
try {
val = angular.toJson(val);
val = Utilities.toJson(val);
}
catch (e) {
// not json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,11 @@ function umbRequestHelper($http, $q, notificationsService, eventsService, formHe
//add the json data
if (angular.isArray(data)) {
_.each(data, function(item) {
formData.append(item.key, !angular.isString(item.value) ? angular.toJson(item.value) : item.value);
formData.append(item.key, !angular.isString(item.value) ? Utilities.toJson(item.value) : item.value);
});
}
else {
formData.append(data.key, !angular.isString(data.value) ? angular.toJson(data.value) : data.value);
formData.append(data.key, !angular.isString(data.value) ? Utilities.toJson(data.value) : data.value);
}

//call the callback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function umbSessionStorage($window) {
},

set: function (key, value) {
storage["umb_" + key] = angular.toJson(value);
storage["umb_" + key] = Utilities.toJson(value);
}

};
Expand Down
31 changes: 30 additions & 1 deletion src/Umbraco.Web.UI.Client/src/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@
*/
const isObject = val => val !== null && typeof val === 'object';

const isWindow = obj => obj && obj.window === obj;

const isScope = obj => obj && obj.$evalAsync && obj.$watch;

const toJsonReplacer = (key, value) => {
var val = value;
if (typeof key === 'string' && key.charAt(0) === '$' && key.charAt(1) === '$') {
val = undefined;
} else if (isWindow(value)) {
val = '$WINDOW';
} else if (value && window.document === value) {
val = '$DOCUMENT';
} else if (isScope(value)) {
val = '$SCOPE';
}
return val;
}
/**
* Equivalent to angular.toJson
*/
const toJson = (obj, pretty) => {
if (isUndefined(obj)) return undefined;
if (!isNumber(pretty)) {
pretty = pretty ? 2 : null;
}
return JSON.stringify(obj, toJsonReplacer, pretty);
}

let _utilities = {
noop: noop,
copy: copy,
Expand All @@ -77,7 +105,8 @@
isDefined: isDefined,
isString: isString,
isNumber: isNumber,
isObject: isObject
isObject: isObject,
toJson: toJson
};

if (typeof (window.Utilities) === 'undefined') {
Expand Down
55 changes: 55 additions & 0 deletions src/Umbraco.Web.UI.Client/test/unit/utilities.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
(function () {
describe("Utilities", function () {
describe("toJson", function () {
it("should delegate to JSON.stringify", function () {
var spy = spyOn(JSON, "stringify").and.callThrough();

expect(Utilities.toJson({})).toEqual("{}");
expect(spy).toHaveBeenCalled();
});

it("should format objects pretty", function () {
expect(Utilities.toJson({ a: 1, b: 2 }, true)).toBe(
'{\n "a": 1,\n "b": 2\n}'
);
expect(Utilities.toJson({ a: { b: 2 } }, true)).toBe(
'{\n "a": {\n "b": 2\n }\n}'
);
expect(Utilities.toJson({ a: 1, b: 2 }, false)).toBe('{"a":1,"b":2}');
expect(Utilities.toJson({ a: 1, b: 2 }, 0)).toBe('{"a":1,"b":2}');
expect(Utilities.toJson({ a: 1, b: 2 }, 1)).toBe(
'{\n "a": 1,\n "b": 2\n}'
);
expect(Utilities.toJson({ a: 1, b: 2 }, {})).toBe(
'{\n "a": 1,\n "b": 2\n}'
);
});

it("should not serialize properties starting with $$", function () {
expect(Utilities.toJson({ $$some: "value" }, false)).toEqual("{}");
});

it("should serialize properties starting with $", function () {
expect(Utilities.toJson({ $few: "v" }, false)).toEqual('{"$few":"v"}');
});

it("should not serialize $window object", function () {
expect(Utilities.toJson(window)).toEqual('"$WINDOW"');
});

it("should not serialize $document object", function () {
expect(Utilities.toJson(document)).toEqual('"$DOCUMENT"');
});

it("should not serialize scope instances", inject(function (
$rootScope
) {
expect(Utilities.toJson({ key: $rootScope })).toEqual('{"key":"$SCOPE"}');
}));

it("should serialize undefined as undefined", function () {
expect(Utilities.toJson(undefined)).toEqual(undefined);
});
});
});
})();

0 comments on commit 76e4b6b

Please sign in to comment.