@capture.dev/fast-json-patch 3.2.0
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/LICENSE.txt +23 -0
- package/README.md +428 -0
- package/dist/index.cjs +766 -0
- package/dist/index.d.cts +161 -0
- package/dist/index.d.ts +161 -0
- package/dist/index.js +726 -0
- package/package.json +71 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deeply clone the object.
|
|
3
|
+
* https://jsperf.com/deep-copy-vs-json-stringify-json-parse/25 (recursiveDeepCopy)
|
|
4
|
+
* @param {any} obj value to clone
|
|
5
|
+
* @return {any} cloned obj
|
|
6
|
+
*/
|
|
7
|
+
declare function _deepClone(obj: any): any;
|
|
8
|
+
/**
|
|
9
|
+
* Escapes a json pointer path
|
|
10
|
+
* @param path The raw pointer
|
|
11
|
+
* @return the Escaped path
|
|
12
|
+
*/
|
|
13
|
+
declare function escapePathComponent(path: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Unescapes a json pointer path
|
|
16
|
+
* @param path The escaped pointer
|
|
17
|
+
* @return The unescaped path
|
|
18
|
+
*/
|
|
19
|
+
declare function unescapePathComponent(path: string): string;
|
|
20
|
+
type JsonPatchErrorName = "SEQUENCE_NOT_AN_ARRAY" | "OPERATION_NOT_AN_OBJECT" | "OPERATION_OP_INVALID" | "OPERATION_PATH_INVALID" | "OPERATION_FROM_REQUIRED" | "OPERATION_VALUE_REQUIRED" | "OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED" | "OPERATION_PATH_CANNOT_ADD" | "OPERATION_PATH_UNRESOLVABLE" | "OPERATION_FROM_UNRESOLVABLE" | "OPERATION_PATH_ILLEGAL_ARRAY_INDEX" | "OPERATION_VALUE_OUT_OF_BOUNDS" | "TEST_OPERATION_FAILED";
|
|
21
|
+
declare class PatchError extends Error {
|
|
22
|
+
name: JsonPatchErrorName;
|
|
23
|
+
index?: number;
|
|
24
|
+
operation?: any;
|
|
25
|
+
tree?: any;
|
|
26
|
+
constructor(message: string, name: JsonPatchErrorName, index?: number, operation?: any, tree?: any);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> | MoveOperation | CopyOperation | TestOperation<any> | GetOperation<any>;
|
|
30
|
+
interface Validator<T> {
|
|
31
|
+
(operation: Operation, index: number, document: T, existingPathFragment: string): void;
|
|
32
|
+
}
|
|
33
|
+
interface OperationResult<T> {
|
|
34
|
+
removed?: any;
|
|
35
|
+
test?: boolean;
|
|
36
|
+
newDocument: T;
|
|
37
|
+
}
|
|
38
|
+
interface BaseOperation {
|
|
39
|
+
path: string;
|
|
40
|
+
}
|
|
41
|
+
interface AddOperation<T> extends BaseOperation {
|
|
42
|
+
op: "add";
|
|
43
|
+
value: T;
|
|
44
|
+
}
|
|
45
|
+
interface RemoveOperation extends BaseOperation {
|
|
46
|
+
op: "remove";
|
|
47
|
+
}
|
|
48
|
+
interface ReplaceOperation<T> extends BaseOperation {
|
|
49
|
+
op: "replace";
|
|
50
|
+
value: T;
|
|
51
|
+
}
|
|
52
|
+
interface MoveOperation extends BaseOperation {
|
|
53
|
+
op: "move";
|
|
54
|
+
from: string;
|
|
55
|
+
}
|
|
56
|
+
interface CopyOperation extends BaseOperation {
|
|
57
|
+
op: "copy";
|
|
58
|
+
from: string;
|
|
59
|
+
}
|
|
60
|
+
interface TestOperation<T> extends BaseOperation {
|
|
61
|
+
op: "test";
|
|
62
|
+
value: T;
|
|
63
|
+
}
|
|
64
|
+
interface GetOperation<T> extends BaseOperation {
|
|
65
|
+
op: "_get";
|
|
66
|
+
value: T;
|
|
67
|
+
}
|
|
68
|
+
interface PatchResult<T> extends Array<OperationResult<T>> {
|
|
69
|
+
newDocument: T;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves a value from a JSON document by a JSON pointer.
|
|
73
|
+
* Returns the value.
|
|
74
|
+
*
|
|
75
|
+
* @param document The document to get the value from
|
|
76
|
+
* @param pointer an escaped JSON pointer
|
|
77
|
+
* @return The retrieved value
|
|
78
|
+
*/
|
|
79
|
+
declare function getValueByPointer(document: any, pointer: string): any;
|
|
80
|
+
/**
|
|
81
|
+
* Apply a single JSON Patch Operation on a JSON document.
|
|
82
|
+
* Returns the {newDocument, result} of the operation.
|
|
83
|
+
* It modifies the `document` and `operation` objects - it gets the values by reference.
|
|
84
|
+
* If you would like to avoid touching your values, clone them:
|
|
85
|
+
* `jsonpatch.applyOperation(document, jsonpatch._deepClone(operation))`.
|
|
86
|
+
*
|
|
87
|
+
* @param document The document to patch
|
|
88
|
+
* @param operation The operation to apply
|
|
89
|
+
* @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
|
|
90
|
+
* @param mutateDocument Whether to mutate the original document or clone it before applying
|
|
91
|
+
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
|
|
92
|
+
* @return `{newDocument, result}` after the operation
|
|
93
|
+
*/
|
|
94
|
+
declare function applyOperation<T>(document: T, operation: Operation, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean, index?: number): OperationResult<T>;
|
|
95
|
+
/**
|
|
96
|
+
* Apply a full JSON Patch array on a JSON document.
|
|
97
|
+
* Returns the {newDocument, result} of the patch.
|
|
98
|
+
* It modifies the `document` object and `patch` - it gets the values by reference.
|
|
99
|
+
* If you would like to avoid touching your values, clone them:
|
|
100
|
+
* `jsonpatch.applyPatch(document, jsonpatch._deepClone(patch))`.
|
|
101
|
+
*
|
|
102
|
+
* @param document The document to patch
|
|
103
|
+
* @param patch The patch to apply
|
|
104
|
+
* @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
|
|
105
|
+
* @param mutateDocument Whether to mutate the original document or clone it before applying
|
|
106
|
+
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
|
|
107
|
+
* @return An array of `{newDocument, result}` after the patch
|
|
108
|
+
*/
|
|
109
|
+
declare function applyPatch<T>(document: T, patch: ReadonlyArray<Operation>, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
|
|
110
|
+
/**
|
|
111
|
+
* Apply a single JSON Patch Operation on a JSON document.
|
|
112
|
+
* Returns the updated document.
|
|
113
|
+
* Suitable as a reducer.
|
|
114
|
+
*
|
|
115
|
+
* @param document The document to patch
|
|
116
|
+
* @param operation The operation to apply
|
|
117
|
+
* @return The updated document
|
|
118
|
+
*/
|
|
119
|
+
declare function applyReducer<T>(document: T, operation: Operation, index: number): T;
|
|
120
|
+
/**
|
|
121
|
+
* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
|
|
122
|
+
* @param {object} operation - operation object (patch)
|
|
123
|
+
* @param {number} index - index of operation in the sequence
|
|
124
|
+
* @param {object} [document] - object where the operation is supposed to be applied
|
|
125
|
+
* @param {string} [existingPathFragment] - comes along with `document`
|
|
126
|
+
*/
|
|
127
|
+
declare function validator(operation: Operation, index: number, document?: any, existingPathFragment?: string): void;
|
|
128
|
+
/**
|
|
129
|
+
* Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document.
|
|
130
|
+
* If error is encountered, returns a JsonPatchError object
|
|
131
|
+
* @param sequence
|
|
132
|
+
* @param document
|
|
133
|
+
* @returns {JsonPatchError|undefined}
|
|
134
|
+
*/
|
|
135
|
+
declare function validate<T>(sequence: ReadonlyArray<Operation>, document?: T, externalValidator?: Validator<T>): PatchError;
|
|
136
|
+
declare function _areEquals(a: any, b: any): boolean;
|
|
137
|
+
|
|
138
|
+
interface Observer<T> {
|
|
139
|
+
object: T;
|
|
140
|
+
patches: Operation[];
|
|
141
|
+
unobserve: () => void;
|
|
142
|
+
callback: (patches: Operation[]) => void;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Detach an observer from an object
|
|
146
|
+
*/
|
|
147
|
+
declare function unobserve<T>(root: T, observer: Observer<T>): void;
|
|
148
|
+
/**
|
|
149
|
+
* Observes changes made to an object, which can then be retrieved using generate
|
|
150
|
+
*/
|
|
151
|
+
declare function observe<T>(obj: Object | Array<T>, callback?: (patches: Operation[]) => void): Observer<T>;
|
|
152
|
+
/**
|
|
153
|
+
* Generate an array of patches from an observer
|
|
154
|
+
*/
|
|
155
|
+
declare function generate<T>(observer: Observer<Object>, invertible?: boolean): Operation[];
|
|
156
|
+
/**
|
|
157
|
+
* Create an array of patches from the differences in two objects
|
|
158
|
+
*/
|
|
159
|
+
declare function compare(tree1: Object | Array<any>, tree2: Object | Array<any>, invertible?: boolean): Operation[];
|
|
160
|
+
|
|
161
|
+
export { type AddOperation, type BaseOperation, type CopyOperation, type GetOperation, PatchError as JsonPatchError, type MoveOperation, type Observer, type Operation, type OperationResult, type PatchResult, type RemoveOperation, type ReplaceOperation, type TestOperation, type Validator, _areEquals, applyOperation, applyPatch, applyReducer, compare, _deepClone as deepClone, escapePathComponent, generate, getValueByPointer, observe, unescapePathComponent, unobserve, validate, validator };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deeply clone the object.
|
|
3
|
+
* https://jsperf.com/deep-copy-vs-json-stringify-json-parse/25 (recursiveDeepCopy)
|
|
4
|
+
* @param {any} obj value to clone
|
|
5
|
+
* @return {any} cloned obj
|
|
6
|
+
*/
|
|
7
|
+
declare function _deepClone(obj: any): any;
|
|
8
|
+
/**
|
|
9
|
+
* Escapes a json pointer path
|
|
10
|
+
* @param path The raw pointer
|
|
11
|
+
* @return the Escaped path
|
|
12
|
+
*/
|
|
13
|
+
declare function escapePathComponent(path: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Unescapes a json pointer path
|
|
16
|
+
* @param path The escaped pointer
|
|
17
|
+
* @return The unescaped path
|
|
18
|
+
*/
|
|
19
|
+
declare function unescapePathComponent(path: string): string;
|
|
20
|
+
type JsonPatchErrorName = "SEQUENCE_NOT_AN_ARRAY" | "OPERATION_NOT_AN_OBJECT" | "OPERATION_OP_INVALID" | "OPERATION_PATH_INVALID" | "OPERATION_FROM_REQUIRED" | "OPERATION_VALUE_REQUIRED" | "OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED" | "OPERATION_PATH_CANNOT_ADD" | "OPERATION_PATH_UNRESOLVABLE" | "OPERATION_FROM_UNRESOLVABLE" | "OPERATION_PATH_ILLEGAL_ARRAY_INDEX" | "OPERATION_VALUE_OUT_OF_BOUNDS" | "TEST_OPERATION_FAILED";
|
|
21
|
+
declare class PatchError extends Error {
|
|
22
|
+
name: JsonPatchErrorName;
|
|
23
|
+
index?: number;
|
|
24
|
+
operation?: any;
|
|
25
|
+
tree?: any;
|
|
26
|
+
constructor(message: string, name: JsonPatchErrorName, index?: number, operation?: any, tree?: any);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> | MoveOperation | CopyOperation | TestOperation<any> | GetOperation<any>;
|
|
30
|
+
interface Validator<T> {
|
|
31
|
+
(operation: Operation, index: number, document: T, existingPathFragment: string): void;
|
|
32
|
+
}
|
|
33
|
+
interface OperationResult<T> {
|
|
34
|
+
removed?: any;
|
|
35
|
+
test?: boolean;
|
|
36
|
+
newDocument: T;
|
|
37
|
+
}
|
|
38
|
+
interface BaseOperation {
|
|
39
|
+
path: string;
|
|
40
|
+
}
|
|
41
|
+
interface AddOperation<T> extends BaseOperation {
|
|
42
|
+
op: "add";
|
|
43
|
+
value: T;
|
|
44
|
+
}
|
|
45
|
+
interface RemoveOperation extends BaseOperation {
|
|
46
|
+
op: "remove";
|
|
47
|
+
}
|
|
48
|
+
interface ReplaceOperation<T> extends BaseOperation {
|
|
49
|
+
op: "replace";
|
|
50
|
+
value: T;
|
|
51
|
+
}
|
|
52
|
+
interface MoveOperation extends BaseOperation {
|
|
53
|
+
op: "move";
|
|
54
|
+
from: string;
|
|
55
|
+
}
|
|
56
|
+
interface CopyOperation extends BaseOperation {
|
|
57
|
+
op: "copy";
|
|
58
|
+
from: string;
|
|
59
|
+
}
|
|
60
|
+
interface TestOperation<T> extends BaseOperation {
|
|
61
|
+
op: "test";
|
|
62
|
+
value: T;
|
|
63
|
+
}
|
|
64
|
+
interface GetOperation<T> extends BaseOperation {
|
|
65
|
+
op: "_get";
|
|
66
|
+
value: T;
|
|
67
|
+
}
|
|
68
|
+
interface PatchResult<T> extends Array<OperationResult<T>> {
|
|
69
|
+
newDocument: T;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves a value from a JSON document by a JSON pointer.
|
|
73
|
+
* Returns the value.
|
|
74
|
+
*
|
|
75
|
+
* @param document The document to get the value from
|
|
76
|
+
* @param pointer an escaped JSON pointer
|
|
77
|
+
* @return The retrieved value
|
|
78
|
+
*/
|
|
79
|
+
declare function getValueByPointer(document: any, pointer: string): any;
|
|
80
|
+
/**
|
|
81
|
+
* Apply a single JSON Patch Operation on a JSON document.
|
|
82
|
+
* Returns the {newDocument, result} of the operation.
|
|
83
|
+
* It modifies the `document` and `operation` objects - it gets the values by reference.
|
|
84
|
+
* If you would like to avoid touching your values, clone them:
|
|
85
|
+
* `jsonpatch.applyOperation(document, jsonpatch._deepClone(operation))`.
|
|
86
|
+
*
|
|
87
|
+
* @param document The document to patch
|
|
88
|
+
* @param operation The operation to apply
|
|
89
|
+
* @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
|
|
90
|
+
* @param mutateDocument Whether to mutate the original document or clone it before applying
|
|
91
|
+
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
|
|
92
|
+
* @return `{newDocument, result}` after the operation
|
|
93
|
+
*/
|
|
94
|
+
declare function applyOperation<T>(document: T, operation: Operation, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean, index?: number): OperationResult<T>;
|
|
95
|
+
/**
|
|
96
|
+
* Apply a full JSON Patch array on a JSON document.
|
|
97
|
+
* Returns the {newDocument, result} of the patch.
|
|
98
|
+
* It modifies the `document` object and `patch` - it gets the values by reference.
|
|
99
|
+
* If you would like to avoid touching your values, clone them:
|
|
100
|
+
* `jsonpatch.applyPatch(document, jsonpatch._deepClone(patch))`.
|
|
101
|
+
*
|
|
102
|
+
* @param document The document to patch
|
|
103
|
+
* @param patch The patch to apply
|
|
104
|
+
* @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
|
|
105
|
+
* @param mutateDocument Whether to mutate the original document or clone it before applying
|
|
106
|
+
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
|
|
107
|
+
* @return An array of `{newDocument, result}` after the patch
|
|
108
|
+
*/
|
|
109
|
+
declare function applyPatch<T>(document: T, patch: ReadonlyArray<Operation>, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
|
|
110
|
+
/**
|
|
111
|
+
* Apply a single JSON Patch Operation on a JSON document.
|
|
112
|
+
* Returns the updated document.
|
|
113
|
+
* Suitable as a reducer.
|
|
114
|
+
*
|
|
115
|
+
* @param document The document to patch
|
|
116
|
+
* @param operation The operation to apply
|
|
117
|
+
* @return The updated document
|
|
118
|
+
*/
|
|
119
|
+
declare function applyReducer<T>(document: T, operation: Operation, index: number): T;
|
|
120
|
+
/**
|
|
121
|
+
* Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
|
|
122
|
+
* @param {object} operation - operation object (patch)
|
|
123
|
+
* @param {number} index - index of operation in the sequence
|
|
124
|
+
* @param {object} [document] - object where the operation is supposed to be applied
|
|
125
|
+
* @param {string} [existingPathFragment] - comes along with `document`
|
|
126
|
+
*/
|
|
127
|
+
declare function validator(operation: Operation, index: number, document?: any, existingPathFragment?: string): void;
|
|
128
|
+
/**
|
|
129
|
+
* Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document.
|
|
130
|
+
* If error is encountered, returns a JsonPatchError object
|
|
131
|
+
* @param sequence
|
|
132
|
+
* @param document
|
|
133
|
+
* @returns {JsonPatchError|undefined}
|
|
134
|
+
*/
|
|
135
|
+
declare function validate<T>(sequence: ReadonlyArray<Operation>, document?: T, externalValidator?: Validator<T>): PatchError;
|
|
136
|
+
declare function _areEquals(a: any, b: any): boolean;
|
|
137
|
+
|
|
138
|
+
interface Observer<T> {
|
|
139
|
+
object: T;
|
|
140
|
+
patches: Operation[];
|
|
141
|
+
unobserve: () => void;
|
|
142
|
+
callback: (patches: Operation[]) => void;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Detach an observer from an object
|
|
146
|
+
*/
|
|
147
|
+
declare function unobserve<T>(root: T, observer: Observer<T>): void;
|
|
148
|
+
/**
|
|
149
|
+
* Observes changes made to an object, which can then be retrieved using generate
|
|
150
|
+
*/
|
|
151
|
+
declare function observe<T>(obj: Object | Array<T>, callback?: (patches: Operation[]) => void): Observer<T>;
|
|
152
|
+
/**
|
|
153
|
+
* Generate an array of patches from an observer
|
|
154
|
+
*/
|
|
155
|
+
declare function generate<T>(observer: Observer<Object>, invertible?: boolean): Operation[];
|
|
156
|
+
/**
|
|
157
|
+
* Create an array of patches from the differences in two objects
|
|
158
|
+
*/
|
|
159
|
+
declare function compare(tree1: Object | Array<any>, tree2: Object | Array<any>, invertible?: boolean): Operation[];
|
|
160
|
+
|
|
161
|
+
export { type AddOperation, type BaseOperation, type CopyOperation, type GetOperation, PatchError as JsonPatchError, type MoveOperation, type Observer, type Operation, type OperationResult, type PatchResult, type RemoveOperation, type ReplaceOperation, type TestOperation, type Validator, _areEquals, applyOperation, applyPatch, applyReducer, compare, _deepClone as deepClone, escapePathComponent, generate, getValueByPointer, observe, unescapePathComponent, unobserve, validate, validator };
|