Skip to content
/ sdelta Public

No dependencies pure JS delta generator and merger

License

Notifications You must be signed in to change notification settings

liz3/sdelta

Repository files navigation

sDelta

sDelta is a 127 lines pure js delta generator and merger. It has two functions

  • diffGenerator - takes 2 arguments which can be anything valid in js and it will generate a diff betewen the two, favoring the second or return undefined if they are 1:1 the same, for objects and arrays this is a deep delta.
  • merge - takes two arguments, the first is a target instance and the second should be the diff generated by diffGenerator, this will apply the changes from the patch to the object passed in the first argument.

Arguments - ATTENTION

To both functions there can be passed a third argument which is a options object, these are:

  • metaKey: string - this is important! this is a property which will be used internally on the delta object for things like removing and adding properties to objects/arrays because null is a valid value, the default is _dm_(short for diff meta). do not set this property on your object as it can be fatal and lead to unexpected behaviour, the diff generator does checks for this but still keep this is mind!!!
  • fullArrays: boolean - when theres a difference in arrays, rather then only diffing them, the entire array is included in the diff object, this can be actually more efficient in small arrays, if there is deltaying of larger arrays, this should be left of, the default is false.

Installation

NPM:

npm i --save sdelta

Yarn:

yarn add sdelta

Example

import { diffGenerator, merge } from "sdelta";

const left = { a: { b: { c: 1 }, d: 2 } };

const right = { a: { b: { c: 3 }, d: 2 } };

const diff = diffGenerator(left, right); //  { a: { b: { c: 3 } } }

const merged = merge(left, diff); // === right

LICENSE

This is free software under GPL 2.0

About

No dependencies pure JS delta generator and merger

Topics

Resources

License

Stars

Watchers

Forks