This repository has been archived by the owner on Jun 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
demo.js
68 lines (58 loc) · 2.01 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Angular JS code
(function () {
'use-strict';
angular.module('demoApp', ['ds.objectDiff'])
.config([
'$interpolateProvider',
function ($interpolateProvider) {
return $interpolateProvider.startSymbol('{(').endSymbol(')}');
}
])
.controller('DemoController', DemoController);
DemoController.$inject = ['$scope', 'ObjectDiff'];
function DemoController($scope, ObjectDiff) {
$scope.objectOne = "{\n" +
" a: {\n" +
" b: 1,\n" +
" c: [1, 2]\n" +
" },\n" +
" \"2b\": {\n" +
" foo: 'bar'\n" +
" }\n" +
" }";
$scope.objectTwo = "{\n" +
" a: { \n" +
" b: 2,\n" +
" c: [1, 2, 3]\n" +
" },\n" +
" x: 1\n" +
" }";
function makeDiff() {
var objectOne, objectTwo, diff;
try {
$scope.errorA = false;
objectOne = eval('(' + $scope.objectOne + ')'); //JSON.parse($scope.objectOne);
} catch (err) {
$scope.errorA = true;
}
try {
$scope.errorB = false;
objectTwo = eval('(' + $scope.objectTwo + ')'); //JSON.parse($scope.objectTwo);
} catch (err) {
$scope.errorB = true;
}
// you can directly diff your objects if they are not string
diff = ObjectDiff.diffOwnProperties(objectOne, objectTwo);
$scope.diffValue = ObjectDiff.toJsonView(diff);
$scope.diffValueChanges = ObjectDiff.toJsonDiffView(diff);
$scope.yourObjectOne = objectOne;
$scope.yourObjectTwo = objectTwo;
}
$scope.$watch('objectOne', function (newValue, oldValue) {
makeDiff();
});
$scope.$watch('objectTwo', function (newValue, oldValue) {
makeDiff();
});
}
})();