1.2.1 • Published 6 years ago

@jherault/patchjs v1.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 years ago

PatchJS

A lite library (a single class) to manage JSON merge PATCH of an object by saving state changes.

You use a REST API, the PATCH HTTP verb, but you always send the full data? maybe you would like to send only the changes?

Installation

npm install --save @jherault/patchjs

Repo

Github

Usage

import PatchJS from '@jherault/patchjs';

//...

let myObject = {a: 0, b: "aString"}; // a pure object with no function 

let myObjectWithPatchOption = PatchJS.observe(myObject); //yes the name of this variable is a bit longer but...

//do some work on the object
myObjectWithPatchOption.a = 15;
myObjectWithPatchOption.c = {d: 10};

//and send a JSON merge patch to the backend
let headers = new Headers();
headers.append('Content-Type', 'application/merge-patch+json; charset=UTF-8'); //or 'applciation/json' if you prefer but 'merge-patch+json' is the right subtype 

fetch('anUrl', { 
    method: 'PATCH', 
    headers: headers, 
    body: JSON.stringify(myObjectWithPatchOption.mergePatch()) // --> {"a":15,"c":{"d":10}}
    }).then(...);

//get unproxied object with changes
let unproxiedObject = myObjectWithPatchOption.get();

//look for changes
console.log(myObjectWithPatchOption.mergeInfos()); // --> {a: {original: 0, current: 15}, c: {original: null, current: {d: 10}}}

//revert changes to original values
myObjectWithPatchOption = myObjectWithPatchOption.revert();

//revert changes to original values and then get unproxied object
let revertedUnproxiedObject = myObjectWithPatchOption.revert().get();

Note

PatchJS.observe takes an object and an optional boolean typeChecking (default true) that prevent type changes by assignment. If your types are dynamic, set this boolean to false.

let o = PatchJS.observe({}, false);

TODO

what about arrays... if an item is pushed/poped/updated...

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago