Coder Social home page Coder Social logo

ken107 / jsonpatch-observe Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 0.0 696 KB

Observe objects recursively for changes and generate JSON Patches

License: MIT License

TypeScript 100.00%
json-patch harmony-proxies json-rpc json data-binding firebase

jsonpatch-observe's Introduction

Build Status

Observe an object tree for changes and generate JSON Patches (RFC 6902). Uses Harmony Proxy, available in NodeJS version 6.4 and above.

Usage

const {observe} = require("jsonpatch-observe");

let observable = observe({});
observable.$subscribe(patch => console.log(patch));
observable.a = {b:1};		//prints {op:"add", path:"a", value:{b:1}}
observable.a.b = 2;		//prints {op:"add", path:"a/b", value:2}
delete observable.a;		//prints {op:"remove", path:"a"}

Note that the properties of an Observable are also Observables. This is how it's able to detect when you do observable.a.b = 2.

Unobserved Properties

You can exclude certain properties from observe as follows:

require("jsonpatch-observe").config.excludeProperty = function(obj, prop) {
	//return true to exclude the property
}

Splice Patch

The JSONPatch standard does not specify a "splice" operation. Without splice, Array changes are represented as a series of individual "add", "replace", and "remove" operations, which can be quite inefficient to apply.

This module supports generating the splice patch. Enable it as follows:

require("jsonpatch-observe").config.enableSplice = true;

The splice patch has the following format:

{
	op: "splice",
	path: "/myarr/3",		//path to array index
	remove: 2,			//number of elements removed
	add: ['a','b','c']		//elements added
}

I created a fork of Starcounter-Jack JSONPatch library capable of consuming this non-standard splice patch.

jsonpatch-observe's People

Contributors

ken107 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.