npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-patch-utils

v0.7.1

Published

Utility functions for implementing JSON Patch according to [RFC 6902]

Downloads

23

Readme

JSON Patch utils

Helpers for client/server applications using and implementing JSON Patch [RFC 6902]

Build Status npm version dependencies

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.

Licence:

MIT