New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@fsoft/diff-patch

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fsoft/diff-patch

Deep compare two objects and produces a diffData to transform object1 to object2

latest
Source
npmnpm
Version
0.1.9
Version published
Maintainers
1
Created
Source

diff-patch

Introduction

diff-patch is a library that exposes two functions, diff and patch.

The library does not have any dependencies.

The diff function takes two objects (obj1 and obj2) and deep compares them, returning an object that represents the difference between the two (the diffData). diffData is an object that can be used to apply the diff to obj1 to get an object that is the same as obj2.

The patch function takes an obj and a diffData object and applies the diff to the object, returning a new object.

The diff and patch functions are implemented in the following languages:

  • TypeScript
  • Python3

The diffData object can be used interchangeably between the languages.
This makes this library very useful for networked applications, where the client and server are written in different languages.

diffData can be serialized to JSON and deserialized back to an object. It aims to be as small as possible, and is designed to be used in a networked environment.

More languages will be added in the future, contributions are welcome.

Usage

TypeScript

To install the TypeScript version of diff-patch, run:

npm install @fsoft/diff-patch

Then, in your TypeScript code:

import { diff, patch } from '@fsoft/diff-patch';

const obj1 = {
  a: 1,
  b: 2,
  c: {
    d: 3,
    e: 4,
  },
};

const obj2 = {
  a: 1,
  b: 2,
  c: {
    d: 3,
    e: 5,   // changed
  },
};

// create the diff data
const diffData = diff(obj1, obj2);

// show the diff data for debugging
console.log( "diffData: ", diffData);

// apply the diff data to obj1 to get an object that is the same as obj2
const obj3 = patch(obj1, diffData);

// show the result for debugging
console.log( "obj3: ", obj3);

Contributors

This library was created by Fabio Rotondo.

New language implementations are more than welcome.
Please open an issue or a pull request if you want to contribute.

Collaborators:

The official repository for this library is here.

License

This library is licensed under the MIT License.
See the LICENSE file for details.

Keywords

diff

FAQs

Package last updated on 21 Jun 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts