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