Apply a list with JSON Patch operations to a JSON document.
declare function immutableJSONPatch<T, U = unknown> (document: T, operations: JSONPatchDocument, options?: JSONPatchOptions) : U
Where:
document: T is a JSON document
operations: JSONPatchDocument is an array with JSONPatch operations
options: JSONPatchOptions is an optional object allowing passing hooks before and after. With those hooks it is possible to alter the JSON document and/or applied operation before and after this is applied. This allows for example to instantiate classes or additional data structures when applying a JSON patch operation. Or you can keep certain data stats up to date. For example, it is possible to have an array with Customer class instances, and instantiate a new Customer when an add operation is performed. And in this library itself, the before callback is used to create inverse operations whilst applying the actual operations on the document.
operations: JSONPatchDocument is an array with JSONPatch operations
options: JSONPatchOptions is an optional object allowing passing a hook before. With this hook it is possible to alter the JSON document and/or generated reverseOperations before this is applied.
The function returns a list with the reverse JSON Patch operations. These operations can be applied to the updated JSON document (the output of immutableJSONPatch) to restore the original JSON document.
util functions
The library exposes a set of utility functions and typeguards to work with JSON pointers and to do immutable operations on JSON data:
declare function parsePath<T>(document: T, path: JSONPointer): JSONPathdeclare function parseFrom(path: JSONPointer): JSONPathdeclare function parseJSONPointer (pointer: JSONPointer) : JSONPathdeclare function compileJSONPointer (path: JSONPath) : JSONPointerdeclare function compileJSONPointerProp (pathProp: string | number) : JSONPointerdeclare function appendToJSONPointer (pointer: JSONPointer, pathProp: string
Develop
To build the library (ESM, CommonJs, and UMD output in the folder lib):
$ npm install $ npm run build
To run the unit tests:
$ npm test
To run the linter (biomejs):
$ npm run lint
To run the linter, build all, and run unit tests and integration tests:
When the before or after callback returns an object with altered document, this will be used to apply the operation. When and altered operation is returned from before in an object, this operation will be applied instead of the original operation.
|
number
)
:
JSONPointer
declare function startsWithJSONPointer (pointer: JSONPointer, searchPointer: JSONPointer) : boolean
declare function isJSONPatchOperation(operation: unknown): operation is JSONPatchOperation
declare function isJSONPatchAdd(operation: unknown): operation is JSONPatchAdd
declare function isJSONPatchRemove(operation: unknown): operation is JSONPatchRemove
declare function isJSONPatchReplace(operation: unknown): operation is JSONPatchReplace
declare function isJSONPatchCopy(operation: unknown): operation is JSONPatchCopy
declare function isJSONPatchMove(operation: unknown): operation is JSONPatchMove
declare function isJSONPatchTest(operation: unknown): operation is JSONPatchTest
declare function getIn<T, U = unknown>(object: U, path: JSONPath) : T | undefined
declare function setIn<T, U = unknown, V = unknown> (object: U, path: JSONPath, value: V, createPath = false) : T
declare function updateIn<T, U = unknown, V = unknown> (object: T, path: JSONPath, transform: (value: U) => V) : T
declare function deleteIn<T, U = unknown> (object: U, path: JSONPath) : T
declare function existsIn<T> (document: T, path: JSONPath) : boolean
declare function insertAt<T, U = unknown> (document: T, path: JSONPath, value: U) : T
declare function transform <T, U = unknown, V = unknown, W = unknown> (