cdk8s 2.3.9 → 2.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +3 -3
- package/changelog.md +6 -1
- package/lib/api-object.js +1 -1
- package/lib/app.js +1 -1
- package/lib/chart.js +1 -1
- package/lib/dependency.js +2 -2
- package/lib/duration.js +1 -1
- package/lib/helm.js +1 -1
- package/lib/include.js +1 -1
- package/lib/json-patch.js +4 -4
- package/lib/lazy.js +1 -1
- package/lib/metadata.js +1 -1
- package/lib/names.js +1 -1
- package/lib/size.js +1 -1
- package/lib/testing.js +1 -1
- package/lib/yaml.js +1 -1
- package/node_modules/fast-json-patch/LICENSE.txt +1 -1
- package/node_modules/fast-json-patch/README.md +183 -164
- package/node_modules/fast-json-patch/{lib → commonjs}/core.d.ts +4 -23
- package/node_modules/fast-json-patch/{lib → commonjs}/core.js +78 -33
- package/node_modules/fast-json-patch/commonjs/duplex.d.ts +23 -0
- package/node_modules/fast-json-patch/{lib → commonjs}/duplex.js +16 -56
- package/node_modules/fast-json-patch/{lib → commonjs}/helpers.d.ts +2 -2
- package/node_modules/fast-json-patch/{lib → commonjs}/helpers.js +9 -9
- package/node_modules/fast-json-patch/dist/fast-json-patch.js +121 -161
- package/node_modules/fast-json-patch/dist/fast-json-patch.min.js +6 -6
- package/node_modules/fast-json-patch/index.d.ts +34 -0
- package/node_modules/fast-json-patch/index.js +11 -0
- package/node_modules/fast-json-patch/index.mjs +29 -0
- package/node_modules/fast-json-patch/index.ts +31 -0
- package/node_modules/fast-json-patch/jasmine-run.mjs +23 -0
- package/node_modules/fast-json-patch/module/core.d.ts +111 -0
- package/node_modules/fast-json-patch/module/core.mjs +433 -0
- package/node_modules/fast-json-patch/module/duplex.d.ts +23 -0
- package/node_modules/fast-json-patch/module/duplex.mjs +176 -0
- package/node_modules/fast-json-patch/module/helpers.d.ts +41 -0
- package/node_modules/fast-json-patch/module/helpers.mjs +171 -0
- package/node_modules/fast-json-patch/package.json +63 -67
- package/node_modules/fast-json-patch/tsc-to-mjs.sh +10 -0
- package/node_modules/fast-json-patch/webpack.config.js +2 -3
- package/package.json +3 -3
- package/releasetag.txt +1 -1
- package/version.txt +1 -1
- package/node_modules/fast-json-patch/lib/duplex.d.ts +0 -63
- package/node_modules/fast-json-patch/node_modules/fast-deep-equal/LICENSE +0 -21
- package/node_modules/fast-json-patch/node_modules/fast-deep-equal/README.md +0 -58
- package/node_modules/fast-json-patch/node_modules/fast-deep-equal/index.d.ts +0 -4
- package/node_modules/fast-json-patch/node_modules/fast-deep-equal/index.js +0 -55
- package/node_modules/fast-json-patch/node_modules/fast-deep-equal/package.json +0 -59
|
@@ -6,14 +6,12 @@ JSON-Patch
|
|
|
6
6
|
[](https://travis-ci.org/Starcounter-Jack/JSON-Patch)
|
|
7
7
|
|
|
8
8
|
With JSON-Patch, you can:
|
|
9
|
-
- **
|
|
10
|
-
- **applyOperation** to apply single operations
|
|
9
|
+
- **apply** patches (arrays) and single operations on JS object
|
|
11
10
|
- **validate** a sequence of patches
|
|
12
|
-
- **observe** for changes
|
|
13
|
-
- **compare** two objects
|
|
11
|
+
- **observe** for changes and **generate** patches when a change is detected
|
|
12
|
+
- **compare** two objects to obtain the difference
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
[](https://travis-ci.org/Starcounter-Jack/JSON-Patch)
|
|
14
|
+
Tested in Firefox, Chrome, Edge, Safari, IE11, Deno and Node.js
|
|
17
15
|
|
|
18
16
|
|
|
19
17
|
## Why you should use JSON-Patch
|
|
@@ -24,195 +22,67 @@ JSON Patch plays well with the HTTP PATCH verb (method) and REST style programmi
|
|
|
24
22
|
|
|
25
23
|
Mark Nottingham has a [nice blog]( http://www.mnot.net/blog/2012/09/05/patch) about it.
|
|
26
24
|
|
|
27
|
-
## Footprint
|
|
28
|
-
4 KB minified and gzipped (12 KB minified)
|
|
29
|
-
|
|
30
|
-
## Performance
|
|
31
|
-
|
|
32
|
-
##### [`add` benchmark](https://run.perf.zone/view/JSON-Patch-Add-Operation-1535541298893)
|
|
33
|
-
|
|
34
|
-

|
|
35
|
-
|
|
36
|
-
##### [`replace` benchmark](https://run.perf.zone/view/JSON-Patch-Replace-Operation-1535540952263)
|
|
37
|
-
|
|
38
|
-

|
|
39
|
-
|
|
40
|
-
Tested on 29.08.2018. Compared libraries:
|
|
41
|
-
|
|
42
|
-
- [Starcounter-Jack/JSON-Patch](https://www.npmjs.com/package/fast-json-patch) 2.0.6
|
|
43
|
-
- [bruth/jsonpatch-js](https://www.npmjs.com/package/json-patch) 0.7.0
|
|
44
|
-
- [dharmafly/jsonpatch.js](https://www.npmjs.com/package/jsonpatch) 3.0.1
|
|
45
|
-
- [jiff](https://www.npmjs.com/package/jiff) 0.7.3
|
|
46
|
-
- [RFC6902](https://www.npmjs.com/package/rfc6902) 2.4.0
|
|
47
|
-
|
|
48
|
-
We aim the tests to be fair. Our library puts performance as the #1 priority, while other libraries can have different priorities. If you'd like to update the benchmarks or add a library, please fork the [perf.zone](https://perf.zone) benchmarks linked above and open an issue to include new results.
|
|
49
|
-
|
|
50
|
-
## Features
|
|
51
|
-
* Allows you to apply patches on object trees for incoming traffic.
|
|
52
|
-
* Allows you to freely manipulate object trees and then generate patches for outgoing traffic.
|
|
53
|
-
* Tested in IE11, Firefox, Chrome, Safari and Node.js
|
|
54
|
-
|
|
55
25
|
|
|
56
26
|
## Install
|
|
57
27
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
### npm
|
|
61
|
-
|
|
62
|
-
```sh
|
|
63
|
-
$ npm install fast-json-patch --save
|
|
64
|
-
```
|
|
65
|
-
### bower
|
|
28
|
+
[Download as ZIP](https://github.com/Starcounter-Jack/JSON-Patch/archive/master.zip) or install the current version using a package manager (and save it as a dependency):
|
|
66
29
|
|
|
67
30
|
```sh
|
|
68
|
-
|
|
31
|
+
# NPM
|
|
32
|
+
npm install fast-json-patch --save
|
|
69
33
|
```
|
|
70
34
|
|
|
71
|
-
### [download as ZIP](https://github.com/Starcounter-Jack/JSON-Patch/archive/master.zip)
|
|
72
|
-
|
|
73
35
|
|
|
74
36
|
## Adding to your project
|
|
75
37
|
|
|
76
38
|
### In a web browser
|
|
77
39
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
### In Node.js
|
|
81
|
-
|
|
82
|
-
Call require to get the instance:
|
|
40
|
+
Load the bundled distribution script:
|
|
83
41
|
|
|
84
|
-
```
|
|
85
|
-
|
|
42
|
+
```html
|
|
43
|
+
<script src="dist/fast-json-patch.min.js"></script>
|
|
86
44
|
```
|
|
87
45
|
|
|
88
|
-
|
|
46
|
+
In [browsers that support ECMAScript modules](https://caniuse.com/#feat=es6-module), the below code uses this library as a module:
|
|
89
47
|
|
|
90
|
-
```
|
|
91
|
-
|
|
48
|
+
```html
|
|
49
|
+
<script type="module">
|
|
50
|
+
import * as jsonpatch from 'fast-json-patch/index.mjs';
|
|
51
|
+
import { applyOperation } from 'fast-json-patch/index.mjs';
|
|
52
|
+
</script>
|
|
92
53
|
```
|
|
93
54
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
```js
|
|
97
|
-
const { applyOperation } = require('fast-json-patch');
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
## Usage
|
|
101
|
-
|
|
102
|
-
#### Applying patches:
|
|
103
|
-
|
|
104
|
-
```js
|
|
105
|
-
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
|
|
106
|
-
var patch = [
|
|
107
|
-
{ op: "replace", path: "/firstName", value: "Joachim" },
|
|
108
|
-
{ op: "add", path: "/lastName", value: "Wester" },
|
|
109
|
-
{ op: "add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
|
|
110
|
-
];
|
|
111
|
-
document = jsonpatch.applyPatch(document, patch).newDocument;
|
|
112
|
-
// document == { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [{number:"555-123"}] } };
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
##### For apply individual operations you can use `applyOperation`
|
|
116
|
-
|
|
117
|
-
`jsonpatch.applyOperation` accepts a single operation object instead of a sequence, and returns the object after applying the operation. It works with all the standard JSON patch operations (`add, replace, move, test, remove and copy`).
|
|
118
|
-
|
|
119
|
-
```js
|
|
120
|
-
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
|
|
121
|
-
var operation = { op: "replace", path: "/firstName", value: "Joachim" };
|
|
122
|
-
document = jsonpatch.applyOperation(document, operation).newDocument;
|
|
123
|
-
// document == { firstName: "Joachim", contactDetails: { phoneNumbers: [] }}
|
|
124
|
-
```
|
|
125
|
-
|
|
126
|
-
#### Using `applyReducer` with `reduce`
|
|
127
|
-
|
|
128
|
-
If you have an array of operations, you can simple reduce them using `applyReducer` as your reducer:
|
|
129
|
-
|
|
130
|
-
```js
|
|
131
|
-
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [ ] } };
|
|
132
|
-
var patch = [
|
|
133
|
-
{ op:"replace", path: "/firstName", value: "Joachim" },
|
|
134
|
-
{ op:"add", path: "/lastName", value: "Wester" },
|
|
135
|
-
{ op:"add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
|
|
136
|
-
];
|
|
137
|
-
var updatedDocument = patch.reduce(applyReducer, document);
|
|
138
|
-
// updatedDocument == { firstName:"Joachim", lastName:"Wester", contactDetails:{ phoneNumbers[ {number:"555-123"} ] } };
|
|
139
|
-
```
|
|
55
|
+
### In Node.js
|
|
140
56
|
|
|
141
|
-
|
|
57
|
+
In Node 12+ with `--experimental-modules` flag, the below code uses this library as an ECMAScript module:
|
|
142
58
|
|
|
143
59
|
```js
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
document.firstName = "Albert";
|
|
147
|
-
document.contactDetails.phoneNumbers[0].number = "123";
|
|
148
|
-
document.contactDetails.phoneNumbers.push({ number:"456" });
|
|
149
|
-
var patch = jsonpatch.generate(observer);
|
|
150
|
-
// patch == [
|
|
151
|
-
// { op: "replace", path: "/firstName", value: "Albert"},
|
|
152
|
-
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
|
|
153
|
-
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
|
|
154
|
-
// ];
|
|
60
|
+
import * as jsonpatch from 'fast-json-patch/index.mjs';
|
|
61
|
+
import { applyOperation } from 'fast-json-patch/index.mjs';
|
|
155
62
|
```
|
|
156
63
|
|
|
157
|
-
|
|
64
|
+
In Webpack (and most surely other bundlers based on Babel), the below code uses this library as an ECMAScript module:
|
|
158
65
|
|
|
159
66
|
```js
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
document.firstName = "Albert";
|
|
163
|
-
document.contactDetails.phoneNumbers[0].number = "123";
|
|
164
|
-
document.contactDetails.phoneNumbers.push({ number:"456" });
|
|
165
|
-
var patch = jsonpatch.generate(observer, true);
|
|
166
|
-
// patch == [
|
|
167
|
-
// { op: "test", path: "/firstName", value: "Joachim"},
|
|
168
|
-
// { op: "replace", path: "/firstName", value: "Albert"},
|
|
169
|
-
// { op: "test", path: "/contactDetails/phoneNumbers/0/number", value: "555-123" },
|
|
170
|
-
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
|
|
171
|
-
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
|
|
172
|
-
// ];
|
|
67
|
+
import * as jsonpatch from 'fast-json-patch';
|
|
68
|
+
import { applyOperation } from 'fast-json-patch';
|
|
173
69
|
```
|
|
174
70
|
|
|
175
|
-
|
|
71
|
+
In standard Node, the below code uses this library as a CommonJS module:
|
|
176
72
|
|
|
177
73
|
```js
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
var diff = jsonpatch.compare(documentA, documentB);
|
|
181
|
-
//diff == [{op: "replace", path: "/user/lastName", value: "Collins"}]
|
|
74
|
+
const { applyOperation } = require('fast-json-patch');
|
|
75
|
+
const applyOperation = require('fast-json-patch').applyOperation;
|
|
182
76
|
```
|
|
183
77
|
|
|
184
|
-
|
|
78
|
+
## Directories
|
|
185
79
|
|
|
186
|
-
|
|
187
|
-
var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
|
|
188
|
-
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
|
|
189
|
-
var diff = jsonpatch.compare(documentA, documentB, true);
|
|
190
|
-
//diff == [
|
|
191
|
-
// {op: "test", path: "/user/lastName", value: "Einstein"},
|
|
192
|
-
// {op: "replace", path: "/user/lastName", value: "Collins"}
|
|
193
|
-
// ];
|
|
194
|
-
```
|
|
195
|
-
|
|
196
|
-
Validating a sequence of patches:
|
|
80
|
+
Directories used in this package:
|
|
197
81
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (errors.length == 0) {
|
|
203
|
-
//there are no errors!
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
for (var i=0; i < errors.length; i++) {
|
|
207
|
-
if (!errors[i]) {
|
|
208
|
-
console.log("Valid patch at index", i, patches[i]);
|
|
209
|
-
}
|
|
210
|
-
else {
|
|
211
|
-
console.error("Invalid patch at index", i, errors[i], patches[i]);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
```
|
|
82
|
+
- `dist/` - contains ES5 files for a Web browser
|
|
83
|
+
- `commonjs/` - contains CommonJS module and typings
|
|
84
|
+
- `module/` - contains ECMAScript module and typings
|
|
85
|
+
- `src/` - contains TypeScript source files
|
|
216
86
|
|
|
217
87
|
## API
|
|
218
88
|
|
|
@@ -243,6 +113,19 @@ Returns an array of [`OperationResult`](#operationresult-type) objects - one ite
|
|
|
243
113
|
|
|
244
114
|
- See [Validation notes](#validation-notes).
|
|
245
115
|
|
|
116
|
+
Example:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
|
|
120
|
+
var patch = [
|
|
121
|
+
{ op: "replace", path: "/firstName", value: "Joachim" },
|
|
122
|
+
{ op: "add", path: "/lastName", value: "Wester" },
|
|
123
|
+
{ op: "add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
|
|
124
|
+
];
|
|
125
|
+
document = jsonpatch.applyPatch(document, patch).newDocument;
|
|
126
|
+
// document == { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [{number:"555-123"}] } };
|
|
127
|
+
```
|
|
128
|
+
|
|
246
129
|
#### `function applyOperation<T>(document: T, operation: Operation, validateOperation: boolean | Validator<T> = false, mutateDocument: boolean = true, banPrototypeModifications: boolean = true, index: number = 0): OperationResult<T>`
|
|
247
130
|
|
|
248
131
|
Applies single operation object `operation` on `document`.
|
|
@@ -264,6 +147,15 @@ Returns an [`OperationResult`](#operationresult-type) object `{newDocument: any,
|
|
|
264
147
|
|
|
265
148
|
- See [Validation notes](#validation-notes).
|
|
266
149
|
|
|
150
|
+
Example:
|
|
151
|
+
|
|
152
|
+
```js
|
|
153
|
+
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [] } };
|
|
154
|
+
var operation = { op: "replace", path: "/firstName", value: "Joachim" };
|
|
155
|
+
document = jsonpatch.applyOperation(document, operation).newDocument;
|
|
156
|
+
// document == { firstName: "Joachim", contactDetails: { phoneNumbers: [] }}
|
|
157
|
+
```
|
|
158
|
+
|
|
267
159
|
#### `jsonpatch.applyReducer<T>(document: T, operation: Operation, index: number): T`
|
|
268
160
|
|
|
269
161
|
**Ideal for `patch.reduce(jsonpatch.applyReducer, document)`**.
|
|
@@ -274,6 +166,19 @@ Returns the a modified document.
|
|
|
274
166
|
|
|
275
167
|
Note: It throws `TEST_OPERATION_FAILED` error if `test` operation fails.
|
|
276
168
|
|
|
169
|
+
Example:
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
var document = { firstName: "Albert", contactDetails: { phoneNumbers: [ ] } };
|
|
173
|
+
var patch = [
|
|
174
|
+
{ op:"replace", path: "/firstName", value: "Joachim" },
|
|
175
|
+
{ op:"add", path: "/lastName", value: "Wester" },
|
|
176
|
+
{ op:"add", path: "/contactDetails/phoneNumbers/0", value: { number: "555-123" } }
|
|
177
|
+
];
|
|
178
|
+
var updatedDocument = patch.reduce(applyReducer, document);
|
|
179
|
+
// updatedDocument == { firstName:"Joachim", lastName:"Wester", contactDetails:{ phoneNumbers[ {number:"555-123"} ] } };
|
|
180
|
+
```
|
|
181
|
+
|
|
277
182
|
#### `jsonpatch.deepClone(value: any): any`
|
|
278
183
|
|
|
279
184
|
Returns deeply cloned value.
|
|
@@ -306,18 +211,88 @@ method, it will be triggered synchronously as well. If `invertible` is true, the
|
|
|
306
211
|
|
|
307
212
|
If there are no pending changes in `obj`, returns an empty array (length 0).
|
|
308
213
|
|
|
309
|
-
|
|
214
|
+
Example:
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
var document = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
|
|
218
|
+
var observer = jsonpatch.observe(document);
|
|
219
|
+
document.firstName = "Albert";
|
|
220
|
+
document.contactDetails.phoneNumbers[0].number = "123";
|
|
221
|
+
document.contactDetails.phoneNumbers.push({ number:"456" });
|
|
222
|
+
var patch = jsonpatch.generate(observer);
|
|
223
|
+
// patch == [
|
|
224
|
+
// { op: "replace", path: "/firstName", value: "Albert"},
|
|
225
|
+
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
|
|
226
|
+
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
|
|
227
|
+
// ];
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
Example of generating patches with test operations for values in the first object:
|
|
231
|
+
|
|
232
|
+
```js
|
|
233
|
+
var document = { firstName: "Joachim", lastName: "Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
|
|
234
|
+
var observer = jsonpatch.observe(document);
|
|
235
|
+
document.firstName = "Albert";
|
|
236
|
+
document.contactDetails.phoneNumbers[0].number = "123";
|
|
237
|
+
document.contactDetails.phoneNumbers.push({ number:"456" });
|
|
238
|
+
var patch = jsonpatch.generate(observer, true);
|
|
239
|
+
// patch == [
|
|
240
|
+
// { op: "test", path: "/firstName", value: "Joachim"},
|
|
241
|
+
// { op: "replace", path: "/firstName", value: "Albert"},
|
|
242
|
+
// { op: "test", path: "/contactDetails/phoneNumbers/0/number", value: "555-123" },
|
|
243
|
+
// { op: "replace", path: "/contactDetails/phoneNumbers/0/number", value: "123" },
|
|
244
|
+
// { op: "add", path: "/contactDetails/phoneNumbers/1", value: {number:"456"}}
|
|
245
|
+
// ];
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
#### `jsonpatch.unobserve(document, observer)`
|
|
249
|
+
```typescript
|
|
250
|
+
jsonpatch.unobserve(document: any, observer: Observer): void
|
|
251
|
+
|
|
252
|
+
type JsonableObj = { [key:string]: Jsonable };
|
|
253
|
+
type JsonableArr = Jsonable[];
|
|
254
|
+
type Jsonable = JsonableArr | JsonableObj | string | number | boolean | null;
|
|
255
|
+
```
|
|
310
256
|
|
|
311
257
|
Destroys the observer set up on `document`.
|
|
312
258
|
|
|
313
259
|
Any remaining changes are delivered synchronously (as in `jsonpatch.generate`). Note: this is different that ES6/7 `Object.unobserve`, which delivers remaining changes asynchronously.
|
|
314
260
|
|
|
315
|
-
#### `jsonpatch.compare(document1
|
|
261
|
+
#### `jsonpatch.compare(document1, document2, invertible)`
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
jsonpatch.compare(document1: Jsonable, document2: Jsonable, invertible = false): Operation[]
|
|
265
|
+
|
|
266
|
+
type JsonableObj = { [key:string]: Jsonable };
|
|
267
|
+
type JsonableArr = Jsonable[];
|
|
268
|
+
type Jsonable = JsonableArr | JsonableObj | string | number | boolean | null;
|
|
269
|
+
```
|
|
316
270
|
|
|
317
271
|
Compares object trees `document1` and `document2` and returns the difference relative to `document1` as a patches array. If `invertible` is true, then each change will be preceded by a test operation of the value in `document1`.
|
|
318
272
|
|
|
319
273
|
If there are no differences, returns an empty array (length 0).
|
|
320
274
|
|
|
275
|
+
Example:
|
|
276
|
+
|
|
277
|
+
```js
|
|
278
|
+
var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
|
|
279
|
+
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
|
|
280
|
+
var diff = jsonpatch.compare(documentA, documentB);
|
|
281
|
+
//diff == [{op: "replace", path: "/user/lastName", value: "Collins"}]
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
Example of comparing two object trees with test operations for values in the first object:
|
|
285
|
+
|
|
286
|
+
```js
|
|
287
|
+
var documentA = {user: {firstName: "Albert", lastName: "Einstein"}};
|
|
288
|
+
var documentB = {user: {firstName: "Albert", lastName: "Collins"}};
|
|
289
|
+
var diff = jsonpatch.compare(documentA, documentB, true);
|
|
290
|
+
//diff == [
|
|
291
|
+
// {op: "test", path: "/user/lastName", value: "Einstein"},
|
|
292
|
+
// {op: "replace", path: "/user/lastName", value: "Collins"}
|
|
293
|
+
// ];
|
|
294
|
+
```
|
|
295
|
+
|
|
321
296
|
#### `jsonpatch.validate(patch: Operation[], document?: any, validator?: Function): JsonPatchError`
|
|
322
297
|
|
|
323
298
|
See [Validation notes](#validation-notes)
|
|
@@ -350,6 +325,27 @@ OPERATION_PATH_ILLEGAL_ARRAY_INDEX | Expected an unsigned base-10 integer value,
|
|
|
350
325
|
OPERATION_VALUE_OUT_OF_BOUNDS | The specified index MUST NOT be greater than the number of elements in the array
|
|
351
326
|
TEST_OPERATION_FAILED | When operation is `test` and the test fails, applies to `applyReducer`.
|
|
352
327
|
|
|
328
|
+
Example:
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
var obj = {user: {firstName: "Albert"}};
|
|
332
|
+
var patches = [{op: "replace", path: "/user/firstName", value: "Albert"}, {op: "replace", path: "/user/lastName", value: "Einstein"}];
|
|
333
|
+
var errors = jsonpatch.validate(patches, obj);
|
|
334
|
+
if (errors.length == 0) {
|
|
335
|
+
//there are no errors!
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
for (var i=0; i < errors.length; i++) {
|
|
339
|
+
if (!errors[i]) {
|
|
340
|
+
console.log("Valid patch at index", i, patches[i]);
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
console.error("Invalid patch at index", i, errors[i], patches[i]);
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
```
|
|
348
|
+
|
|
353
349
|
## `OperationResult` Type
|
|
354
350
|
|
|
355
351
|
Functions `applyPatch` and `applyOperation` both return `OperationResult` object. This object is:
|
|
@@ -404,6 +400,29 @@ See the [ECMAScript spec](http://www.ecma-international.org/ecma-262/6.0/index.h
|
|
|
404
400
|
|
|
405
401
|
To see the list of recent changes, see [Releases](https://github.com/Starcounter-Jack/JSON-Patch/releases).
|
|
406
402
|
|
|
403
|
+
## Footprint
|
|
404
|
+
4 KB minified and gzipped (12 KB minified)
|
|
405
|
+
|
|
406
|
+
## Performance
|
|
407
|
+
|
|
408
|
+
##### [`add` benchmark](https://run.perf.zone/view/JSON-Patch-Add-Operation-1535541298893)
|
|
409
|
+
|
|
410
|
+

|
|
411
|
+
|
|
412
|
+
##### [`replace` benchmark](https://run.perf.zone/view/JSON-Patch-Replace-Operation-1535540952263)
|
|
413
|
+
|
|
414
|
+

|
|
415
|
+
|
|
416
|
+
Tested on 29.08.2018. Compared libraries:
|
|
417
|
+
|
|
418
|
+
- [Starcounter-Jack/JSON-Patch](https://www.npmjs.com/package/fast-json-patch) 2.0.6
|
|
419
|
+
- [bruth/jsonpatch-js](https://www.npmjs.com/package/json-patch) 0.7.0
|
|
420
|
+
- [dharmafly/jsonpatch.js](https://www.npmjs.com/package/jsonpatch) 3.0.1
|
|
421
|
+
- [jiff](https://www.npmjs.com/package/jiff) 0.7.3
|
|
422
|
+
- [RFC6902](https://www.npmjs.com/package/rfc6902) 2.4.0
|
|
423
|
+
|
|
424
|
+
We aim the tests to be fair. Our library puts performance as the #1 priority, while other libraries can have different priorities. If you'd like to update the benchmarks or add a library, please fork the [perf.zone](https://perf.zone) benchmarks linked above and open an issue to include new results.
|
|
425
|
+
|
|
407
426
|
## License
|
|
408
427
|
|
|
409
428
|
MIT
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { PatchError, _deepClone } from './helpers';
|
|
1
|
+
import { PatchError, _deepClone } from './helpers.js';
|
|
2
2
|
export declare const JsonPatchError: typeof PatchError;
|
|
3
3
|
export declare const deepClone: typeof _deepClone;
|
|
4
4
|
export declare type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> | MoveOperation | CopyOperation | TestOperation<any> | GetOperation<any>;
|
|
@@ -43,12 +43,6 @@ export interface GetOperation<T> extends BaseOperation {
|
|
|
43
43
|
export interface PatchResult<T> extends Array<OperationResult<T>> {
|
|
44
44
|
newDocument: T;
|
|
45
45
|
}
|
|
46
|
-
export interface Observer<T> {
|
|
47
|
-
object: T;
|
|
48
|
-
patches: Operation[];
|
|
49
|
-
unobserve: () => void;
|
|
50
|
-
callback: (patches: Operation[]) => void;
|
|
51
|
-
}
|
|
52
46
|
/**
|
|
53
47
|
* Retrieves a value from a JSON document by a JSON pointer.
|
|
54
48
|
* Returns the value.
|
|
@@ -87,7 +81,7 @@ export declare function applyOperation<T>(document: T, operation: Operation, val
|
|
|
87
81
|
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
|
|
88
82
|
* @return An array of `{newDocument, result}` after the patch
|
|
89
83
|
*/
|
|
90
|
-
export declare function applyPatch<T>(document: T, patch: Operation
|
|
84
|
+
export declare function applyPatch<T>(document: T, patch: ReadonlyArray<Operation>, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
|
|
91
85
|
/**
|
|
92
86
|
* Apply a single JSON Patch Operation on a JSON document.
|
|
93
87
|
* Returns the updated document.
|
|
@@ -113,18 +107,5 @@ export declare function validator(operation: Operation, index: number, document?
|
|
|
113
107
|
* @param document
|
|
114
108
|
* @returns {JsonPatchError|undefined}
|
|
115
109
|
*/
|
|
116
|
-
export declare function validate<T>(sequence: Operation
|
|
117
|
-
declare
|
|
118
|
-
JsonPatchError: typeof PatchError;
|
|
119
|
-
deepClone: typeof _deepClone;
|
|
120
|
-
getValueByPointer: typeof getValueByPointer;
|
|
121
|
-
applyOperation: typeof applyOperation;
|
|
122
|
-
applyPatch: typeof applyPatch;
|
|
123
|
-
applyReducer: typeof applyReducer;
|
|
124
|
-
validator: typeof validator;
|
|
125
|
-
validate: typeof validate;
|
|
126
|
-
};
|
|
127
|
-
/**
|
|
128
|
-
* Default export for backwards compat
|
|
129
|
-
*/
|
|
130
|
-
export default _default;
|
|
110
|
+
export declare function validate<T>(sequence: ReadonlyArray<Operation>, document?: T, externalValidator?: Validator<T>): PatchError;
|
|
111
|
+
export declare function _areEquals(a: any, b: any): boolean;
|