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

@argentumcode/jsonpatcherproxy-commonjs

v0.1.0-0-3

Published

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.

Downloads

10

Readme

JSONPatcherProxy


ES6 proxy powered JSON Object observer that emits JSON patches when changes occur to your object tree.

Build Status

With JSONPatcherProxy, you don't need polling or dirty checking. Any changes to the object are synchronously emitted.

Why you should use JSONPatcherProxy

JSON-Patch (RFC6902) is a standard format that allows you to update a JSON document by sending the changes rather than the whole document. JSONPatcherProxy plays well with the HTTP PATCH verb (method) and REST style programming.

Mark Nottingham has a nice blog about it.

Footprint

  • 1.30 KB minified and gzipped (3.25 KB minified)

Features

  • Allows you to watch an object and record all patches for later usage.
  • Allows you to watch an object and handle individual patches synchronously through a callback.
  • Tested in Edge, Firefox, Chrome, Safari and Node.js.

Install

Install the current version (and save it as a dependency):

npm

$ npm install jsonpatcherproxy --save

Download as ZIP

Adding to your project

In a web browser

Load the bundled distribution script:

<script src="dist/jsonpatcherproxy.min.js"></script>

In browsers that support ECMAScript modules, the below code uses this library as a module:

<script type="module">
  import { JSONPatcherProxy } from 'jsonpatcherproxy/src/jsonpatcherproxy.mjs';
</script>

You can use rawgit.com as a CDN.

In Node.js

import { JSONPatcherProxy } from 'jsonpatcherproxy';

Usage

Generating patches:

var myObj = { firstName:"Joachim", lastName:"Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var jsonPatcherProxy = new JSONPatcherProxy( myObj );
var observedObject = jsonPatcherProxy.observe(true);
observedObject.firstName = "Albert";
observedObject.contactDetails.phoneNumbers[0].number = "123";
observedObject.contactDetails.phoneNumbers.push({number:"456"});
var patches = jsonPatcherProxy.generate();
// patches  == [
//   { op:"replace", path="/firstName", value:"Albert"},
//   { op:"replace", path="/contactDetails/phoneNumbers/0/number", value:"123"},
//   { op:"add", path="/contactDetails/phoneNumbers/1", value:{number:"456"}}];

Receiving patches in a callback:

var myObj = { firstName:"Joachim", lastName:"Wester", contactDetails: { phoneNumbers: [ { number:"555-123" }] } };
var jsonPatcherProxy = new JSONPatcherProxy( myObj );
var observedObject = jsonPatcherProxy.observe(true, function(patch) {
    // patch == { op:"replace", path="/firstName", value:"Albert"}
});
observedObject.firstName = "Albert";

API

Object observing

constructor: JSONPatcherProxy( root Object, [showDetachedWarning Boolean = true] ): JSONPatcherProxy

Creates an instance of JSONPatcherProxy around your object of interest root, for later observe, unobserve, pause, resume calls. Returns JSONPatcherProxy.

root: The object tree you want to observe

showDetachedWarning: Modifying a child object that is detached from the parent tree is not recommended, because the object will continue to be a Proxy. That's why JSONPatcherProxy warns when a detached proxy is accessed. You can set this to false to disable those warnings.

JSONPatcherProxy#observe(record Boolean, [callback Function]): Proxy

Sets up a deep proxy observer on root that listens for changes in the tree. When changes are detected, the optional callback is called with the generated single patch as the parameter.

record: if set to false, all changes are will be pass through the callback and no history will be kept. If set to true patches history will be kept until you call generate, this will return several patches and deletes them from history.

Returns a Proxy mirror of your object.

  • Note 1: you must either set record to true or pass a callback.
  • Note 2: you have to use the return value of this function as your object of interest. Changes to the original object will go unnoticed.
  • Note 3: please make sure to call JSONPatcherProxy#generate often if you choose to record. Because the patches will accumulate if you don't.

🚨 Generated patches are not immutable. See "Limitations" below.

JSONPatcherProxy#generate () : Array

It returns the changes of your object since the last time it's called. You have to be recording (by setting record to true) when calling JSONPatcherProxy#observe.

If there are no pending changes in root, returns an empty array (length 0).

🚨 Generated patches are not immutable. See "Limitations" below.

JSONPatcherProxy#pause () : void

Disables patches emitting (to both callback and patches array). However, the object will be updated if you change it.

JSONPatcherProxy#resume () : void

Enables patches emitting (to both callback and patches array). Starting from the moment you call it.

JSONPatcherProxy#revoke () : void

De-proxifies (revokes) all the proxies that were created either in #observe call or by adding sub-objects to the tree in runtime.

JSONPatcherProxy#disableTraps () : void

Turns the proxified object into a forward-proxy object; doesn't emit any patches anymore, like a normal object.

undefineds (JS to JSON projection)

As undefined type does not exist in JSON, it's also not a valid value of JSON Patch operation. Therefore JSONPatcherProxy will not generate JSON Patches that sets anything to undefined.

Whenever a value is set to undefined in JS, JSONPatcherProxy method generate and compare will treat it similarly to how JavaScript method JSON.stringify (MDN) treats them:

If undefined (...) is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).

See the ECMAScript spec for details.

Limitations

  1. It mutates your original object: During proxification, JSONPatcherProxy mutates the object you pass to it. Because it doesn't deep-clone for better performance. If you want your original object to remain untouched, please deep-clone before you pass it to the constructor.

  2. It does not support multi-level proxification: During proxification, it recursively traverses the object and sets each property to its new proxified version. This means you can't proxify an already proxified object because the original proxy will record proxification as a series of changes. And the emitted patches are unpredictable. Also, when you change a property from either one of the proxies, both of the proxies will emit patches in an undefined manner.

  3. Generated patches are not immutable. The patches generated by JSONPatcherProxy contain reference to the profixied object as the patch value. You should either serialize the patch to JSON immediately or deep clone the value it if you want to process it later. If you don't do that, you run the risk that reading the patch will contain changes added after the patch was generated.

  4. JSONPatcherProxy might not generate patches for changes of integers above Number.MAX_SAFE_INTEGER, or any other changes beyond the precision of JavaScript double-precision floating-point. To reduce patch generation noise, JSONPatcherProxy compares if the new value is the same (===) as the previous one. If so, then no patch is generated. Therefore, a change from Number.MAX_SAFE_INTEGER + 1 to Number.MAX_SAFE_INTEGER + 2, or from 0.02 to 0.020000000000000001 will not be effectively observed.

Specs/tests

In browser

Go to /test

In Node

Run:

npm test

Benchmarking

When you run npm run bench, few things happen:

  1. Five benchmark specs defined in proxyBenchmark.js are executed. This might take a minute.
  2. The results are appended to the benchmark.tsv file, including the following information for each spec:
    • current Git commit SHA
    • number of operations per second
    • name of the spec
  3. At the end, in your console you will see the detailed information about the current benchmark, and the comparison of all data found in benchmark.tsv relatively to the first results for each given spec name, e.g.:
Observe and generate, small object (JSONPatcherProxy)
 ec7b9bf: 136720 Ops/sec
 92da649: 136351 Ops/sec (0.3% worse)
Observe and generate (JSONPatcherProxy)
 ec7b9bf: 2762 Ops/sec
 92da649: 2793 Ops/sec (1.1% better)
Primitive mutation (JSONPatcherProxy)
 ec7b9bf: 781852 Ops/sec
 92da649: 781270 Ops/sec (0.1% worse)
Complex mutation (JSONPatcherProxy)
 ec7b9bf: 11808 Ops/sec
 92da649: 10692 Ops/sec (9.5% worse)
Serialization (JSONPatcherProxy)
 ec7b9bf: 1719 Ops/sec
 92da649: 1719 Ops/sec (no difference)

Contributing

  • Fork it.
  • Clone it locally.
  • Run npm install.
  • Run npm test to make sure tests pass before touching the code.
  • Modify, test again, push, and send a PR!

Bumping the version

Version bumping is automated. Just use npm version command and all files will be updated with the new version.

License

MIT