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

jsong-patch

v0.1.0

Published

JSON Pointer/Patch implementation for JavaScript

Downloads

3

Readme

JSONG

build status

JSONG passes the entire json-patch-tests suite; see Tests

npm install jsong-patch
// Node.js / io.js / webpack / browserify / ...
var JSONG = require('jsong-patch');

// browsers
// <script src="node_modules/jsong-patch/JSONG.js">
var JSONG = window.JSONG;

For performance concerns JSONG mutates documents; if you'd like JSONG to work on its own shallow copy of your document:

doc = JSONG.clone(doc)

See Clone.

JSON Patch

http://jsonpatch.com/

var doc = {
  "name": "John Doe",
  "address": {
    "street": "...",
    "number": 42,
    "city": "Neverland"
  },
  "tags": ["friends", "coworker"],
  "age": 25,
  "starred": true,
  "height": null
});

Patch

doc = JSONG.patch(doc, [
  { "op": "add",     "path": "/tags/0",        "value": "family"          },
  { "op": "remove",  "path": "/height"                                    },
  { "op": "replace", "path": "/age",           "value": "26"              },
  { "op": "move",    "from": "/address/city",  "path": "/address/country" },
  { "op": "copy",    "from": "/starred",       "to":    "bookmarked"      },
  { "op": "test",    "path": "/starred",       "value": true              }
]);

JSONG.patch returns a document because the JSON Patch specification states that an operation can replace the original document.

JSONG.patch is atomic, if any operation fails, the document will be restored to its original state and an error will be thrown.

Apply

JSONG.apply is an alias to JSON.patch and behave exactly the same.

Revert

If JSONG.patch is passed with a third argument {revert: true} it will return an array [doc, items].

The items object can be used to revert a patch on a document.

// apply the patch
var patchResult = JSONG.patch(doc, patch, {revert: true});
doc = patchResult[0];

// revert the patch
doc = JSONG.revert(doc, patchResult[1]);
// doc is strictly identical (in the JSON sense) to the original

Operations

add, copy, replace, move, remove, test operations return an array of the form [document, previous, idx]

The first argument is returned for the same reason JSONG.apply returns a document see Patch

The second argument is the previous value at the specified destination if any, undefined otherwise.

The third argument is the index of the new element. For add operation only using the JSON Pointer '-' token to push an item at the end of an array.

Add

doc = JSONG.add(doc, '/foo', 'foo')[0]

Remove

doc = JSONG.remove(doc, '/foo')[0];

Replace

doc = JSONG.replace(doc, '/foo', 'foo')[0];

Move

doc = JSONG.move(doc, '/foo', '/bar')[0];

Copy

doc = JSONG.copy(doc, '/foo', '/bar')[0];

Test

doc = JSONG.test(doc, '/foo', 'bar')[0];

Extra operations

Get

JSONG.get(doc, '/foo');
// returns value at /foo

Has

JSONG.has(doc, '/foo');
// returns true if foo property exists, false otherwise

JSONG.Document

JSONG.Document is a class that abstracts JSON document It adds many benefit over using JSONG against native object and array.

Per RFC an operation can replace the original document. JSONG.Document keeps a reference to the document so you don't need to re-declare it for each operation.

var JSONGDoc = new JSONG.Document(doc);
//JSONGDoc.doc === doc

var newDoc = {"something": "else"};
JSONGDoc.replace('', newDoc)
//JSONGDoc.doc === newDoc

Most JSONG methods are available on JSONG.Document objects; they behave the same except for you don't need to pass a document object and they do not return a document object.

patch, revert, apply

Do not return anything.

add, remove, replace, move, copy, test

Return the previous value at the location if any, undefined otherwise.

get, has

Same return value as their JSONG counterparts.

JSON Pointer

http://tools.ietf.org/html/rfc6901

Parse

JSONG.pointer.parse('/foo/bar/hello');
// returns ['foo', 'bar', 'hello'];

Serialize

JSONG.pointer.serialize(['foo', 'bar', 'hello']);
// returns ('/foo/bar/hello');

JSON Merge Patch

https://tools.ietf.org/html/rfc7396

doc = JSONG.mergePatch(doc, {"name": "Jeanette doe"});

JSON

Clone

Deep clone a JSON object or array

var doc = {
  "foo": {
    "bar": {}
  }
}

var copy = JSONG.clone(doc)
console.log(copy)
//{
//  "foo": {
//    "bar": {}
//  }
//}

copy === doc
//false

Equal

Test for JSON equality between two objects or arrays.

JSONG.equal({foo: 'bar', bar: 'foo'}, {bar: 'foo', foo: 'bar'})
// true

Tests

git submodule update --init --recursive
npm install -g webpack eslint mocha
npm test