Repository navigation
Expand file tree
/
Copy pathplain.js
More file actions
154 lines (154 loc) · 3.65 KB
/
Copy pathplain.js
File metadata and controls
154 lines (154 loc) · 3.65 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
exports.__esModule = true;
var JSONDiffPatch = __importStar(require("jsondiffpatch"));
var diff_1 = require("./diff");
/**
* @description
* Returns the changed value of the object.
*
* @param left any
* @param right any
*/
exports.diff = function (left, right) {
return diff_1.CustomizedDiffPatch.diff(left, right);
};
/**
* @description
* Returns the changed values of the object.
*
* @param object any
*/
exports.diffs = function (object) {
var diffs = [];
for (var i = 0; i < (object.length - 1); i++) {
var left = object[i];
var right = object[i + 1];
var diffObject = diff_1.CustomizedDiffPatch.diff(left, right);
if (diffObject)
diffs.push(diffObject);
}
return diffs;
};
/**
* @description
* Returns the changed content in
* JSON PATCH (RFC 6902) format.
*
* @param diff
* @param original any
* @returns It returns JSON PATCH (RFC 6902)
*/
exports.changelogs = function (diff, original) {
try {
// @ts-ignore
var result = diff_1.CustomizedDiffPatch.formatters.jsonpatch.format(diff, original);
if (!result)
throw new Error();
return result;
}
catch (e) {
return undefined;
}
};
/**
* @description
* Returns the changed content in
* human readable log form.
*
* @param option
* @returns string
*/
exports.changelogsFormatted = function (option) {
var result = undefined;
try {
switch (option.format) {
case 'annotated':
result = JSONDiffPatch.formatters.annotated.format(option.diff, option.original);
break;
case 'console':
result = JSONDiffPatch.formatters.console.format(option.diff, option.original);
break;
case 'html':
result = JSONDiffPatch.formatters.html.format(option.diff, option.original);
break;
}
}
catch (e) { }
return result;
};
/**
* @description
* Applies changes to objects.
*
* @param left any
* @param diff
*/
exports.patch = function (left, diff) {
return diff_1.CustomizedDiffPatch.patch(left, diff);
};
/**
* @description
* Applies multiple changes to objects.
*
* @param left any
* @param diffs
*/
exports.patches = function (object, diffs) {
var patched = object;
try {
for (var _i = 0, diffs_1 = diffs; _i < diffs_1.length; _i++) {
var diff_2 = diffs_1[_i];
patched = diff_1.CustomizedDiffPatch.patch(patched, diff_2);
}
}
catch (e) {
return undefined;
}
return patched;
};
/**
* @description
* Exclude changes from objects.
*
* @param right any
* @param diff
*/
exports.unpatch = function (right, diff) {
return diff_1.CustomizedDiffPatch.unpatch(right, diff);
};
/**
* @description
* Exclude multiple changes from objects.
*
* @param right any
* @param diff
*/
exports.unpatches = function (object, diffs) {
var patched = undefined;
try {
for (var _i = 0, diffs_2 = diffs; _i < diffs_2.length; _i++) {
var diff_3 = diffs_2[_i];
patched = diff_1.CustomizedDiffPatch.unpatch(patched, diff_3);
}
}
catch (e) {
return undefined;
}
return patched;
};
/**
* @description
* Reverse the diff order.
*
* @param diff
*/
exports.reverse = function (diff) {
return diff_1.CustomizedDiffPatch.reverse(diff);
};