Skip to content

Squash JSON-Patch operations patch into a mathematically equivalent yet smaller patch

Notifications You must be signed in to change notification settings

alshakero/json-squash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎃 json-squash

Squash JSON-Patch operations patch into a mathematically equivalent smaller patch. Shines the most for array splice and shift patches. Removing a single element from an array, whererver it is in the array, will produce a single operation 🙌

Build Status

Installation

Using npm:

npm install json-squash --save

Using yarn:

yarn add json-squash

Usage:

const squash = require('json-squash');
// or
import squash from 'json-squash';

const patch = [
  { "op": "add", "path": "/a/b/c", "value": 1},
  { "op": "replace", "path": "/a/b/c", "value": 12 },
  { "op": "replace", "path": "/a/b/c", "value": 123 },
  { "op": "replace", "path": "/a/b/c", "value": 1234 },
  { "op": "move", "from": "/a/b/c", "path": "/a/b/d" },
  { "op": "replace", "path": "/a/b/d", "value": 12345 },
];

const squashed = squash(patch);
// ==> [{ "op": "add", "path": "/a/b/d", "value": 12345 }]


// array shift
const obj = {arr: [1, 2, 3, 4, 5, 6]};
obj.arr.shift();

// fast-json-patch diff result
const patch = [
  {op: "replace", path: "/arr/0", value: 2},
  {op: "replace", path: "/arr/1", value: 3},
  {op: "replace", path: "/arr/2", value: 4},
  {op: "replace", path: "/arr/3", value: 5},
  {op: "replace", path: "/arr/4", value: 6},
  {op: "remove", path: "/arr/5"}
];

const squashed = squash(patch);
// ==> [{ "op": "remove", "path": "arr/0" }]

Testing

json-squash uses ava test runner. To test,

git clone https://github.com/alshakero/json-squash.git
cd json-squash
npm install
npm test

Contributing

  • Fork this repo.
  • Run npm install.
  • Run tests before any modifications to make sure they run.
  • Modify.
  • Test again. Please add suites if your modifications add new functionality.
  • Send a PR request.
  • Receive big thanks!

Author

Omar Alshaker

License

MIT 2017

About

Squash JSON-Patch operations patch into a mathematically equivalent yet smaller patch

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published