@dabble/patches 0.4.5 → 0.4.6
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/dist/algorithms/client/applyCommittedChanges.d.ts +8 -2
- package/dist/algorithms/client/applyCommittedChanges.js +30 -38
- package/dist/algorithms/client/batching.d.ts +8 -2
- package/dist/algorithms/client/batching.js +38 -37
- package/dist/algorithms/client/breakChange.d.ts +8 -2
- package/dist/algorithms/client/breakChange.js +191 -240
- package/dist/algorithms/client/createStateFromSnapshot.d.ts +8 -2
- package/dist/algorithms/client/createStateFromSnapshot.js +7 -8
- package/dist/algorithms/client/getJSONByteSize.d.ts +3 -1
- package/dist/algorithms/client/getJSONByteSize.js +12 -11
- package/dist/algorithms/client/makeChange.d.ts +8 -2
- package/dist/algorithms/client/makeChange.js +28 -36
- package/dist/algorithms/server/commitChanges.d.ts +9 -3
- package/dist/algorithms/server/commitChanges.js +69 -78
- package/dist/algorithms/server/createVersion.d.ts +9 -3
- package/dist/algorithms/server/createVersion.js +21 -27
- package/dist/algorithms/server/getSnapshotAtRevision.d.ts +9 -3
- package/dist/algorithms/server/getSnapshotAtRevision.js +27 -28
- package/dist/algorithms/server/getStateAtRevision.d.ts +9 -3
- package/dist/algorithms/server/getStateAtRevision.js +13 -17
- package/dist/algorithms/server/handleOfflineSessionsAndBatches.d.ts +9 -3
- package/dist/algorithms/server/handleOfflineSessionsAndBatches.js +60 -77
- package/dist/algorithms/server/transformIncomingChanges.d.ts +8 -2
- package/dist/algorithms/server/transformIncomingChanges.js +27 -39
- package/dist/algorithms/shared/applyChanges.d.ts +8 -2
- package/dist/algorithms/shared/applyChanges.js +11 -16
- package/dist/algorithms/shared/rebaseChanges.d.ts +8 -2
- package/dist/algorithms/shared/rebaseChanges.js +30 -49
- package/dist/chunk-IZ2YBCUP.js +56 -0
- package/dist/client/InMemoryStore.d.ts +9 -3
- package/dist/client/InMemoryStore.js +92 -101
- package/dist/client/IndexedDBStore.d.ts +9 -3
- package/dist/client/IndexedDBStore.js +378 -491
- package/dist/client/Patches.d.ts +18 -13
- package/dist/client/Patches.js +152 -207
- package/dist/client/PatchesDoc.d.ts +14 -8
- package/dist/client/PatchesDoc.js +147 -154
- package/dist/client/PatchesHistoryClient.d.ts +12 -5
- package/dist/client/PatchesHistoryClient.js +110 -117
- package/dist/client/PatchesStore.d.ts +9 -3
- package/dist/client/PatchesStore.js +0 -1
- package/dist/client/index.d.ts +12 -6
- package/dist/client/index.js +5 -5
- package/dist/data/change.d.ts +9 -3
- package/dist/data/change.js +23 -15
- package/dist/data/version.d.ts +9 -3
- package/dist/data/version.js +11 -15
- package/dist/event-signal.d.ts +7 -6
- package/dist/event-signal.js +24 -39
- package/dist/index-CvQws3AB.d.ts +36 -0
- package/dist/index.d.ts +27 -5
- package/dist/index.js +10 -4
- package/dist/json-patch/JSONPatch.d.ts +9 -5
- package/dist/json-patch/JSONPatch.js +175 -183
- package/dist/json-patch/applyPatch.d.ts +5 -2
- package/dist/json-patch/applyPatch.js +27 -35
- package/dist/json-patch/composePatch.d.ts +5 -2
- package/dist/json-patch/composePatch.js +34 -34
- package/dist/json-patch/createJSONPatch.d.ts +7 -2
- package/dist/json-patch/createJSONPatch.js +11 -38
- package/dist/json-patch/index.d.ts +14 -6
- package/dist/json-patch/index.js +20 -9
- package/dist/json-patch/invertPatch.d.ts +5 -2
- package/dist/json-patch/invertPatch.js +31 -30
- package/dist/json-patch/ops/add.d.ts +5 -2
- package/dist/json-patch/ops/add.js +53 -51
- package/dist/json-patch/ops/bitmask.d.ts +8 -5
- package/dist/json-patch/ops/bitmask.js +41 -44
- package/dist/json-patch/ops/copy.d.ts +5 -2
- package/dist/json-patch/ops/copy.js +32 -33
- package/dist/json-patch/ops/increment.d.ts +5 -2
- package/dist/json-patch/ops/increment.js +21 -20
- package/dist/json-patch/ops/index.d.ts +10 -21
- package/dist/json-patch/ops/index.js +34 -24
- package/dist/json-patch/ops/move.d.ts +5 -2
- package/dist/json-patch/ops/move.js +132 -198
- package/dist/json-patch/ops/remove.d.ts +5 -2
- package/dist/json-patch/ops/remove.js +33 -30
- package/dist/json-patch/ops/replace.d.ts +5 -2
- package/dist/json-patch/ops/replace.js +45 -43
- package/dist/json-patch/ops/test.d.ts +5 -2
- package/dist/json-patch/ops/test.js +25 -21
- package/dist/json-patch/ops/text.d.ts +5 -2
- package/dist/json-patch/ops/text.js +54 -54
- package/dist/json-patch/pathProxy.d.ts +9 -3
- package/dist/json-patch/pathProxy.js +27 -48
- package/dist/json-patch/state.d.ts +5 -2
- package/dist/json-patch/state.js +11 -7
- package/dist/json-patch/transformPatch.d.ts +6 -2
- package/dist/json-patch/transformPatch.js +21 -24
- package/dist/json-patch/types.d.ts +9 -7
- package/dist/json-patch/types.js +0 -1
- package/dist/json-patch/utils/deepEqual.d.ts +3 -1
- package/dist/json-patch/utils/deepEqual.js +32 -28
- package/dist/json-patch/utils/exit.d.ts +5 -2
- package/dist/json-patch/utils/exit.js +7 -3
- package/dist/json-patch/utils/get.d.ts +5 -2
- package/dist/json-patch/utils/get.js +8 -4
- package/dist/json-patch/utils/getOpData.d.ts +5 -2
- package/dist/json-patch/utils/getOpData.js +12 -9
- package/dist/json-patch/utils/getType.d.ts +6 -3
- package/dist/json-patch/utils/getType.js +9 -4
- package/dist/json-patch/utils/index.d.ts +15 -14
- package/dist/json-patch/utils/index.js +14 -14
- package/dist/json-patch/utils/log.d.ts +4 -2
- package/dist/json-patch/utils/log.js +8 -3
- package/dist/json-patch/utils/ops.d.ts +8 -5
- package/dist/json-patch/utils/ops.js +83 -100
- package/dist/json-patch/utils/paths.d.ts +12 -9
- package/dist/json-patch/utils/paths.js +54 -51
- package/dist/json-patch/utils/pluck.d.ts +8 -5
- package/dist/json-patch/utils/pluck.js +32 -26
- package/dist/json-patch/utils/shallowCopy.d.ts +3 -1
- package/dist/json-patch/utils/shallowCopy.js +22 -18
- package/dist/json-patch/utils/softWrites.d.ts +6 -3
- package/dist/json-patch/utils/softWrites.js +17 -16
- package/dist/json-patch/utils/toArrayIndex.d.ts +3 -1
- package/dist/json-patch/utils/toArrayIndex.js +14 -10
- package/dist/json-patch/utils/toKeys.d.ts +3 -1
- package/dist/json-patch/utils/toKeys.js +15 -11
- package/dist/json-patch/utils/updateArrayIndexes.d.ts +5 -2
- package/dist/json-patch/utils/updateArrayIndexes.js +33 -37
- package/dist/json-patch/utils/updateArrayPath.d.ts +5 -2
- package/dist/json-patch/utils/updateArrayPath.js +29 -42
- package/dist/net/PatchesClient.d.ts +128 -0
- package/dist/net/PatchesClient.js +161 -0
- package/dist/net/PatchesSync.d.ts +19 -9
- package/dist/net/PatchesSync.js +291 -386
- package/dist/net/error.d.ts +3 -1
- package/dist/net/error.js +9 -6
- package/dist/net/http/FetchTransport.d.ts +21 -0
- package/dist/net/http/FetchTransport.js +34 -0
- package/dist/net/index.d.ts +26 -12
- package/dist/net/index.js +12 -10
- package/dist/net/protocol/JSONRPCClient.d.ts +11 -4
- package/dist/net/protocol/JSONRPCClient.js +95 -103
- package/dist/net/protocol/JSONRPCServer.d.ts +15 -8
- package/dist/net/protocol/JSONRPCServer.js +101 -123
- package/dist/net/protocol/types.d.ts +21 -15
- package/dist/net/protocol/types.js +0 -1
- package/dist/net/protocol/utils.d.ts +12 -0
- package/dist/net/protocol/utils.js +15 -0
- package/dist/net/types.d.ts +4 -2
- package/dist/net/types.js +0 -1
- package/dist/net/webrtc/WebRTCAwareness.d.ts +14 -4
- package/dist/net/webrtc/WebRTCAwareness.js +111 -120
- package/dist/net/webrtc/WebRTCTransport.d.ts +16 -8
- package/dist/net/webrtc/WebRTCTransport.js +149 -157
- package/dist/net/webrtc/index.d.ts +10 -2
- package/dist/net/webrtc/index.js +2 -2
- package/dist/net/websocket/AuthorizationProvider.d.ts +7 -5
- package/dist/net/websocket/AuthorizationProvider.js +12 -17
- package/dist/net/websocket/PatchesWebSocket.d.ts +14 -109
- package/dist/net/websocket/PatchesWebSocket.js +37 -184
- package/dist/net/websocket/RPCServer.d.ts +19 -10
- package/dist/net/websocket/RPCServer.js +190 -192
- package/dist/net/websocket/SignalingService.d.ts +12 -32
- package/dist/net/websocket/SignalingService.js +126 -133
- package/dist/net/websocket/WebSocketServer.d.ts +17 -4
- package/dist/net/websocket/WebSocketServer.js +64 -72
- package/dist/net/websocket/WebSocketTransport.d.ts +13 -5
- package/dist/net/websocket/WebSocketTransport.js +178 -207
- package/dist/net/websocket/onlineState.d.ts +6 -3
- package/dist/net/websocket/onlineState.js +25 -21
- package/dist/server/PatchesBranchManager.d.ts +12 -5
- package/dist/server/PatchesBranchManager.js +132 -142
- package/dist/server/PatchesHistoryManager.d.ts +11 -3
- package/dist/server/PatchesHistoryManager.js +81 -84
- package/dist/server/PatchesServer.d.ts +16 -10
- package/dist/server/PatchesServer.js +131 -137
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.js +9 -3
- package/dist/server/types.d.ts +9 -3
- package/dist/server/types.js +0 -1
- package/dist/types.d.ts +38 -19
- package/dist/types.js +1 -1
- package/dist/utils/concurrency.d.ts +7 -5
- package/dist/utils/concurrency.js +43 -53
- package/dist/utils/deferred.d.ts +4 -2
- package/dist/utils/deferred.js +25 -21
- package/package.json +5 -7
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import "../chunk-IZ2YBCUP.js";
|
|
1
2
|
/*!
|
|
2
3
|
* Based on work from
|
|
3
4
|
* https://github.com/mohayonao/json-touch-patch
|
|
@@ -10,189 +11,180 @@
|
|
|
10
11
|
* WARNING: using /array/- syntax to indicate the end of the array makes it impossible to transform arrays correctly in
|
|
11
12
|
* all situaions. Please avoid using this syntax when using Operational Transformations.
|
|
12
13
|
*/
|
|
13
|
-
import { Delta } from
|
|
14
|
-
import { applyPatch } from
|
|
15
|
-
import { composePatch } from
|
|
16
|
-
import { invertPatch } from
|
|
17
|
-
import { bitmask } from
|
|
18
|
-
import { transformPatch } from
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
* Returns an array of patch operations.
|
|
182
|
-
*/
|
|
183
|
-
toJSON() {
|
|
184
|
-
return this.ops.slice();
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Create a new JSONPatch with the provided JSON patch operations.
|
|
188
|
-
*/
|
|
189
|
-
static fromJSON(ops, types) {
|
|
190
|
-
return new this(ops, types);
|
|
191
|
-
}
|
|
14
|
+
import { Delta } from "@dabble/delta";
|
|
15
|
+
import { applyPatch } from "./applyPatch.js";
|
|
16
|
+
import { composePatch } from "./composePatch.js";
|
|
17
|
+
import { invertPatch } from "./invertPatch.js";
|
|
18
|
+
import { bitmask } from "./ops/bitmask.js";
|
|
19
|
+
import { transformPatch } from "./transformPatch.js";
|
|
20
|
+
class JSONPatch {
|
|
21
|
+
ops;
|
|
22
|
+
custom;
|
|
23
|
+
/**
|
|
24
|
+
* Create a new JSONPatch, optionally with an existing array of operations.
|
|
25
|
+
*/
|
|
26
|
+
constructor(ops = [], custom = {}) {
|
|
27
|
+
this.ops = ops;
|
|
28
|
+
this.custom = custom;
|
|
29
|
+
}
|
|
30
|
+
op(op, path, value, from, soft) {
|
|
31
|
+
path = checkPath(path);
|
|
32
|
+
if (from !== void 0) {
|
|
33
|
+
from = checkPath(from);
|
|
34
|
+
}
|
|
35
|
+
const patchOp = from ? { op, from, path } : { op, path };
|
|
36
|
+
if (value !== void 0) patchOp.value = value;
|
|
37
|
+
if (soft) patchOp.soft = soft;
|
|
38
|
+
this.ops.push(patchOp);
|
|
39
|
+
return this;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Tests a value exists. If it doesn't, the patch is not applied.
|
|
43
|
+
*/
|
|
44
|
+
test(path, value) {
|
|
45
|
+
return this.op("test", path, value);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Adds the value to an object or array, inserted before the given index.
|
|
49
|
+
*/
|
|
50
|
+
add(path, value, options) {
|
|
51
|
+
if (value && value.toJSON) value = value.toJSON();
|
|
52
|
+
return this.op("add", path, value, void 0, options?.soft);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Deletes the value at the given path or removes it from an array.
|
|
56
|
+
*/
|
|
57
|
+
remove(path) {
|
|
58
|
+
return this.op("remove", path);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Replaces a value (same as remove+add).
|
|
62
|
+
*/
|
|
63
|
+
replace(path, value, options) {
|
|
64
|
+
if (value && value.toJSON) value = value.toJSON();
|
|
65
|
+
return this.op("replace", path, value, void 0, options?.soft);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Copies the value at `from` to `path`.
|
|
69
|
+
*/
|
|
70
|
+
copy(from, to, options) {
|
|
71
|
+
return this.op("copy", to, void 0, from, options?.soft);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Moves the value at `from` to `path`.
|
|
75
|
+
*/
|
|
76
|
+
move(from, to) {
|
|
77
|
+
if (from === to) return this;
|
|
78
|
+
return this.op("move", to, void 0, from);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Increments a numeric value by 1 or the given amount.
|
|
82
|
+
*/
|
|
83
|
+
increment(path, value = 1) {
|
|
84
|
+
return this.op("@inc", path, value);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Decrements a numeric value by 1 or the given amount.
|
|
88
|
+
*/
|
|
89
|
+
decrement(path, value = 1) {
|
|
90
|
+
return this.op("@inc", path, -value);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Flips a bit at the given index in a bitmask to the given value.
|
|
94
|
+
*/
|
|
95
|
+
bit(path, index, on) {
|
|
96
|
+
return this.op("@bit", path, bitmask(index, on));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Applies a delta to a text document.
|
|
100
|
+
*/
|
|
101
|
+
text(path, value) {
|
|
102
|
+
if (Array.isArray(value)) {
|
|
103
|
+
value = new Delta(value);
|
|
104
|
+
} else if (!(value instanceof Delta) && Array.isArray(value?.ops)) {
|
|
105
|
+
value = new Delta(value.ops);
|
|
106
|
+
} else if (!(value instanceof Delta)) {
|
|
107
|
+
throw new Error("Invalid Delta");
|
|
108
|
+
}
|
|
109
|
+
return this.op("@txt", path, value);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Creates a patch from an object partial, updating each field. Set a field to undefined to delete it.
|
|
113
|
+
*/
|
|
114
|
+
addUpdates(updates, path = "/") {
|
|
115
|
+
path = checkPath(path);
|
|
116
|
+
if (path[path.length - 1] !== "/") path += "/";
|
|
117
|
+
Object.keys(updates).forEach((key) => {
|
|
118
|
+
const value = updates[key];
|
|
119
|
+
if (value == void 0) {
|
|
120
|
+
this.remove(path + key);
|
|
121
|
+
} else {
|
|
122
|
+
this.replace(path + key, value);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Apply this patch to an object, returning a new object with the applied changes (or the same object if nothing
|
|
129
|
+
* changed in the patch). Optionally apply the page at the given path prefix.
|
|
130
|
+
*/
|
|
131
|
+
apply(obj, options) {
|
|
132
|
+
return applyPatch(obj, this.ops, options, this.custom);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Transform the given patch against this one. This patch is considered to have happened first. Optionally provide
|
|
136
|
+
* the object these operations are being applied to if available to know for sure if a numerical path is an array
|
|
137
|
+
* index or object key. Otherwise, all numerical paths are treated as array indexes.
|
|
138
|
+
*/
|
|
139
|
+
transform(patch, obj) {
|
|
140
|
+
const JSONPatch2 = this.constructor;
|
|
141
|
+
return new JSONPatch2(
|
|
142
|
+
transformPatch(obj, this.ops, Array.isArray(patch) ? patch : patch.ops, this.custom),
|
|
143
|
+
this.custom
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Create a patch which can reverse what this patch does. Because JSON Patches do not store previous values, you
|
|
148
|
+
* must provide the previous object to create a reverse patch.
|
|
149
|
+
*/
|
|
150
|
+
invert(obj) {
|
|
151
|
+
const JSONPatch2 = this.constructor;
|
|
152
|
+
return new JSONPatch2(invertPatch(obj, this.ops, this.custom), this.custom);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Compose/collapse patches into fewer operations.
|
|
156
|
+
*/
|
|
157
|
+
compose(patch) {
|
|
158
|
+
const JSONPatch2 = this.constructor;
|
|
159
|
+
let ops = this.ops;
|
|
160
|
+
if (patch) ops = ops.concat(Array.isArray(patch) ? patch : patch.ops);
|
|
161
|
+
return new JSONPatch2(composePatch(ops), this.custom);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Add two patches together.
|
|
165
|
+
*/
|
|
166
|
+
concat(patch) {
|
|
167
|
+
const JSONPatch2 = this.constructor;
|
|
168
|
+
return new JSONPatch2(this.ops.concat(Array.isArray(patch) ? patch : patch.ops), this.custom);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Returns an array of patch operations.
|
|
172
|
+
*/
|
|
173
|
+
toJSON() {
|
|
174
|
+
return this.ops.slice();
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Create a new JSONPatch with the provided JSON patch operations.
|
|
178
|
+
*/
|
|
179
|
+
static fromJSON(ops, types) {
|
|
180
|
+
return new this(ops, types);
|
|
181
|
+
}
|
|
192
182
|
}
|
|
193
183
|
function checkPath(path) {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
return path;
|
|
184
|
+
path = path.toString();
|
|
185
|
+
if (path.length && path[0] !== "/") path = `/${path}`;
|
|
186
|
+
return path;
|
|
198
187
|
}
|
|
188
|
+
export {
|
|
189
|
+
JSONPatch
|
|
190
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { JSONPatchOp, ApplyJSONPatchOptions, JSONPatchOpHandlerMap } from './types.js';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Applies a sequence of JSON patch operations to an object.
|
|
4
5
|
*
|
|
@@ -8,4 +9,6 @@ import type { ApplyJSONPatchOptions, JSONPatchOp, JSONPatchOpHandlerMap } from '
|
|
|
8
9
|
* @param custom - Custom patch operation handlers
|
|
9
10
|
* @returns The object after applying the patches
|
|
10
11
|
*/
|
|
11
|
-
|
|
12
|
+
declare function applyPatch(object: any, patches: JSONPatchOp[], opts?: ApplyJSONPatchOptions, custom?: JSONPatchOpHandlerMap): any;
|
|
13
|
+
|
|
14
|
+
export { applyPatch };
|
|
@@ -1,37 +1,29 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import "../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { getTypes } from "./ops/index.js";
|
|
3
|
+
import { runWithObject } from "./state.js";
|
|
4
|
+
import { exit } from "./utils/exit.js";
|
|
5
|
+
import { getType } from "./utils/getType.js";
|
|
6
|
+
function applyPatch(object, patches, opts = {}, custom) {
|
|
7
|
+
if (patches.length === 0) {
|
|
8
|
+
return object;
|
|
9
|
+
}
|
|
10
|
+
if (opts.atPath) {
|
|
11
|
+
patches = patches.map((op) => ({ ...op, path: opts.atPath + op.path }));
|
|
12
|
+
}
|
|
13
|
+
const types = getTypes(custom);
|
|
14
|
+
return runWithObject(object, types, patches.length > 1, (state) => {
|
|
15
|
+
for (let i = 0, imax = patches.length; i < imax; i++) {
|
|
16
|
+
const patch = patches[i];
|
|
17
|
+
const handler = getType(state, patch)?.apply;
|
|
18
|
+
const error = handler ? handler(state, "" + patch.path, patch.from || patch.value) : `[op:${patch.op}] unknown`;
|
|
19
|
+
if (error) {
|
|
20
|
+
if (!opts.silent && !opts.strict || opts.silent === false) console.error(error, patch);
|
|
21
|
+
if (opts.strict) throw new TypeError(error);
|
|
22
|
+
if (opts.rigid) return exit(state, object, patch, opts);
|
|
23
|
+
}
|
|
17
24
|
}
|
|
18
|
-
|
|
19
|
-
patches = patches.map(op => ({ ...op, path: opts.atPath + op.path }));
|
|
20
|
-
}
|
|
21
|
-
const types = getTypes(custom);
|
|
22
|
-
return runWithObject(object, types, patches.length > 1, state => {
|
|
23
|
-
for (let i = 0, imax = patches.length; i < imax; i++) {
|
|
24
|
-
const patch = patches[i];
|
|
25
|
-
const handler = getType(state, patch)?.apply;
|
|
26
|
-
const error = handler ? handler(state, '' + patch.path, patch.from || patch.value) : `[op:${patch.op}] unknown`;
|
|
27
|
-
if (error) {
|
|
28
|
-
if ((!opts.silent && !opts.strict) || opts.silent === false)
|
|
29
|
-
console.error(error, patch);
|
|
30
|
-
if (opts.strict)
|
|
31
|
-
throw new TypeError(error);
|
|
32
|
-
if (opts.rigid)
|
|
33
|
-
return exit(state, object, patch, opts);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
});
|
|
25
|
+
});
|
|
37
26
|
}
|
|
27
|
+
export {
|
|
28
|
+
applyPatch
|
|
29
|
+
};
|
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import { JSONPatchOp, JSONPatchOpHandlerMap } from './types.js';
|
|
2
|
+
|
|
3
|
+
declare function composePatch(patches: JSONPatchOp[], custom?: JSONPatchOpHandlerMap): JSONPatchOp[];
|
|
4
|
+
|
|
5
|
+
export { composePatch };
|
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
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
|
-
opsByPath.clear();
|
|
31
|
-
}
|
|
32
|
-
return op;
|
|
33
|
-
});
|
|
1
|
+
import "../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { getTypes } from "./ops/index.js";
|
|
3
|
+
import { runWithObject } from "./state.js";
|
|
4
|
+
import { getType } from "./utils/getType.js";
|
|
5
|
+
import { mapAndFilterOps } from "./utils/ops.js";
|
|
6
|
+
import { getValue } from "./utils/pluck.js";
|
|
7
|
+
function composePatch(patches, custom = {}) {
|
|
8
|
+
const types = getTypes(custom);
|
|
9
|
+
const opsByPath = /* @__PURE__ */ new Map();
|
|
10
|
+
return runWithObject(null, types, patches.length > 1, (state) => {
|
|
11
|
+
return mapAndFilterOps(patches, (op) => {
|
|
12
|
+
const type = getType(state, op);
|
|
13
|
+
const handler = type?.compose;
|
|
14
|
+
if (handler) {
|
|
15
|
+
const lastOp = opsByPath.get(op.path);
|
|
16
|
+
if (lastOp && match(lastOp, op)) {
|
|
17
|
+
lastOp.value = handler(state, lastOp.value, op.value);
|
|
18
|
+
return null;
|
|
19
|
+
} else {
|
|
20
|
+
const prefix = `${op.path}/`;
|
|
21
|
+
for (const path of opsByPath.keys()) {
|
|
22
|
+
if (path.startsWith(prefix)) opsByPath.delete(path);
|
|
23
|
+
}
|
|
24
|
+
opsByPath.set(op.path, op = getValue(state, op));
|
|
25
|
+
}
|
|
26
|
+
} else {
|
|
27
|
+
opsByPath.clear();
|
|
28
|
+
}
|
|
29
|
+
return op;
|
|
34
30
|
});
|
|
31
|
+
});
|
|
35
32
|
}
|
|
36
33
|
function match(op1, op2) {
|
|
37
|
-
|
|
34
|
+
return op1 && op2 && op1.op === op2.op && op1.path === op2.path;
|
|
38
35
|
}
|
|
36
|
+
export {
|
|
37
|
+
composePatch
|
|
38
|
+
};
|
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ChangeMutator } from '../types.js';
|
|
2
2
|
import { JSONPatch } from './JSONPatch.js';
|
|
3
|
+
import './types.js';
|
|
4
|
+
import '@dabble/delta';
|
|
5
|
+
|
|
3
6
|
/**
|
|
4
7
|
* Creates a `JSONPatch` instance using a path-only proxy for type-safe operation generation.
|
|
5
8
|
*
|
|
@@ -30,4 +33,6 @@ import { JSONPatch } from './JSONPatch.js';
|
|
|
30
33
|
* // ]
|
|
31
34
|
* ```
|
|
32
35
|
*/
|
|
33
|
-
|
|
36
|
+
declare function createJSONPatch<T>(mutator: ChangeMutator<T>): JSONPatch;
|
|
37
|
+
|
|
38
|
+
export { createJSONPatch };
|
|
@@ -1,39 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
*
|
|
10
|
-
* @template T The type of the target object.
|
|
11
|
-
* @param target The initial state of the object (used for type inference only).
|
|
12
|
-
* @param mutator A function that receives a JSONPatch instance and a PathProxy.
|
|
13
|
-
* @returns A `JSONPatch` instance containing the operations generated within the mutator.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* const myObj = { name: { first: 'Alice' }, age: 30, tags: ['a'] };
|
|
18
|
-
*
|
|
19
|
-
* const patch = createJSONPatch(myObj, (patch, path) => {
|
|
20
|
-
* patch.replace(path.name.first, 'Bob'); // Type-safe path creation
|
|
21
|
-
* patch.increment(path.age, 1); // Explicit operations only
|
|
22
|
-
* patch.add(path.tags[1], 'b'); // Array path handling
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* console.log(patch.ops);
|
|
26
|
-
* // [
|
|
27
|
-
* // { op: 'replace', path: '/name/first', value: 'Bob' },
|
|
28
|
-
* // { op: 'increment', path: '/age', value: 1 },
|
|
29
|
-
* // { op: 'add', path: '/tags/1', value: 'b' }
|
|
30
|
-
* // ]
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export function createJSONPatch(mutator) {
|
|
34
|
-
const patch = new JSONPatch();
|
|
35
|
-
// Create path-only proxy for type-safe path generation
|
|
36
|
-
const pathProxy = createPathProxy();
|
|
37
|
-
mutator(patch, pathProxy);
|
|
38
|
-
return patch;
|
|
1
|
+
import "../chunk-IZ2YBCUP.js";
|
|
2
|
+
import { JSONPatch } from "./JSONPatch.js";
|
|
3
|
+
import { createPathProxy } from "./pathProxy.js";
|
|
4
|
+
function createJSONPatch(mutator) {
|
|
5
|
+
const patch = new JSONPatch();
|
|
6
|
+
const pathProxy = createPathProxy();
|
|
7
|
+
mutator(patch, pathProxy);
|
|
8
|
+
return patch;
|
|
39
9
|
}
|
|
10
|
+
export {
|
|
11
|
+
createJSONPatch
|
|
12
|
+
};
|
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
export { applyPatch } from './applyPatch.js';
|
|
2
2
|
export { composePatch } from './composePatch.js';
|
|
3
3
|
export { invertPatch } from './invertPatch.js';
|
|
4
|
-
export { applyBitmask, bitmask, combineBitmasks } from './ops/bitmask.js';
|
|
5
|
-
export
|
|
6
|
-
export
|
|
4
|
+
export { applyBitmask, bit, bitmask, combineBitmasks } from './ops/bitmask.js';
|
|
5
|
+
export { i as defaultOps, g as getTypes } from '../index-CvQws3AB.js';
|
|
6
|
+
export { createPathProxy, pathProxy } from './pathProxy.js';
|
|
7
7
|
export { transformPatch } from './transformPatch.js';
|
|
8
|
-
export
|
|
9
|
-
export
|
|
10
|
-
export
|
|
8
|
+
export { JSONPatch, PathLike, WriteOptions } from './JSONPatch.js';
|
|
9
|
+
export { ApplyJSONPatchOptions, JSONPatchOpHandlerMap as JSONPatchCustomTypes, JSONPatchOp } from './types.js';
|
|
10
|
+
export { add } from './ops/add.js';
|
|
11
|
+
export { copy } from './ops/copy.js';
|
|
12
|
+
export { increment } from './ops/increment.js';
|
|
13
|
+
export { move } from './ops/move.js';
|
|
14
|
+
export { remove } from './ops/remove.js';
|
|
15
|
+
export { replace } from './ops/replace.js';
|
|
16
|
+
export { test } from './ops/test.js';
|
|
17
|
+
import '../types.js';
|
|
18
|
+
import '@dabble/delta';
|