json-patch-utils
v0.7.1
Published
Utility functions for implementing JSON Patch according to [RFC 6902]
Downloads
23
Maintainers
Readme
JSON Patch utils
Helpers for client/server applications using and implementing JSON Patch [RFC 6902]
See documentation
Install:
$ npm install --save-dev json-patch-utils
Usage:
createPatch
Creates a JSON Patches given an operator, path and a value
import { createPatch } from 'json-patch-utils'
createPatch('add', '/foo', 123) // { op: 'add', path: '/foo', value: 123 }
createPatch('move', 'foo', 'bar') // [Error: The path "foo" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi,The path "bar" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi]
isPatch
Tests if a given object is a valid JSON Patch object according to https://tools.ietf.org/html/rfc6902
import { isPatch } from 'json-patch-utils'
let validPatch = { op: 'add', path: '/foo/bar', value: 'baz' }
let invalidPatch = { op: 'foo', path: 'bar' }
if(isPatch(validPatch) === true) {
// Patch can be safely applied
}
isPatch(invalidPatch) // [
// 'The path "bar" did not match the encoded path regexp: /^(\\/[a-z0-9~\\\\\\-%^|"\\ ]*)*$/gi',
// 'The operation name is not among the valid names add, replace, test, remove, move, copy'
// ]
isPath
Tests if a given string is a valid JSON Pointer according to https://tools.ietf.org/html/rfc6901
import { isPath } from 'json-patch-utils'
isPath('/foo/bar') // true
isPath('foo') // The path "foo" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi
isOperation
Tests if a given string is a valid JSON Patch operation name
import { isOperation } from 'json-patch-utils'
isOperation('add') // true
isOperation('foo') // The operation name is not among the valid names add, replace, test, remove, move, copy
isValueForOperation
Tests if a given value corresponds to the given operation
import { isValueForOperation } from 'json-patch-utils'
isValueForOperation('move', '/foo') // true
isValueForOperation('add', undefined) // The value or from path provided must be different than undefined
isValueForOperation('copy', 'foo') // The path "foo" did not match the encoded path regexp: /^(\/[a-z0-9~\\\-%^|"\ ]*)*$/gi
decodePath
Decodes a path according to https://tools.ietf.org/html/rfc6901 by replacing special symbols
import { decodePath } from 'json-patch-utils'
decodePath('/foo/bar') // '/foo/bar'
decodePath('/foo~0bar~1') // '/foo/bar~'
listPath
Convert a path into a list of strings. Useful when traversing a path on a JSON Document
import { listPath } from 'json-patch-utils'
listPath('/foo/bar/') // ['foo', 'bar']
listPath('/foo~0bar~1') // ['foo', 'bar~']
Apply patch functions for various state/data structure libraries
BaobabJS
import Baobab from 'baobab'
import { applyBaobabJsPatch, createPatch } from 'json-patch-utils'
let tree = new Baobab({
foo: {
bar: 123
}
})
let patch = createPatch('replace', '/foo/bar', 1000)
applyBaobabJsPatch(tree, patch)
tree.select('foo', 'bar').get() // 1000
Credits
https://github.com/Starcounter-Jack/JSON-Patch - for the tests battery in test/tests.json and test/spec_tests.json
Contributing:
Feel free to open issues to propose stuff and participate. Pull requests are also welcome.