0.9.7 • Published 6 months ago

datum-merge v0.9.7

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

datum-merge

datum-merge is a modern typescript library that simplifies merge and diff operations for deeply nested objects.

Source Code : https://github.com/therohk/datum-merge

NPM library : https://www.npmjs.com/package/datum-merge

npm.io npm.io


Sample Usage

Merge with default config:

import { merge, customMerge, UpdateCode } from "datum-merge";
const changed = merge(target, source, UpdateCode.I, UpdateCode.XM, UpdateCode.B);
//same as
const diff = customMerge(target, source, {
    scalar: UpdateCode.I,
    vector: UpdateCode.XM,
    nested: UpdateCode.B,
});

Exact nestable config that ignores all other fields:

import { detailMerge, UpdateCode } from "datum-merge";
const changed = detailMerge(target, source, {
    mykey: UpdateCode.I,
    myarr: UpdateCode.XM,
    anobj: UpdateCode.B,
    myobj: { myid: UpdateCode.I },
});

Deep merge with generic config patterns:

import { customMerge, MergeConfig, UpdateCode } from "datum-merge";
const conf: MergeConfig = {
    "*_id": UpdateCode.I,
    scalar: UpdateCode.B,
    field1: UpdateCode.D,
    "arr*": UpdateCode.XM,
    nested: UpdatedCode.N,
    obj1: {
        scalar: UpdateCode.B,
        vector: UpdateCode.XM,
    },
};
const diff = customMerge(target, source, conf);

Upcoming Features

  1. inline the unmaintained deep-diff library which contains serious bugs. (available)

  2. formalize config schema for deeply nested objects (for v1).

  3. option to ignore errors for datatype mismatch during merge.

  4. support merging for top level arrays or primitives.

  5. accept a subset of json-patch operations for merge.

Code contributions are welcome via issues and pull requests.


Merge Strategy

This string code describes how modifications to an attribute for a PUT/UPDATE operation should be handled. It decides whether a change to the value of the field is allowed during a merge between two entities.

Strategy Codes

The same field within a source and target object is represented by s and t respectively. Whether the strategy requires data to be present for the field, is shown by { 0=no, 1=yes, X=irrelavant }. The value is migrated from the source field to the target field only if the predicate passes.

CodePredicateMeaning
Cn/aalways create new instance
Tn/atouch datum ; empty merge
N0reject any change ; skip merge
YsX & tXaccept any change ; bypass merge
Bs1 & tXinsert or update, no delete
Hs1 & t1update only if exists
UsX & t1update or delete only, no insert
IsX & t0insert only, no update or delete
Ds0 & tXdelete only, no update or insert
XRsX & tXfull vector replacement
XMs ∪ tset union, vector merge
XDt - sset difference, delete given values
XIs ∩ tset intersection, delete missing values
XSt + spreserve order insert (allows dupes)
XFs + tinsert from start (allows dupes)

Diff Codes

Applying the merge results in one of these transitions per primitive value in the target object.

Patch OpMeaningRev CodeTransitions
addnew / insertInull <-- non-null
replaceedit / updateHnon-null <-- non-null
removeunset / deleteDnon-null <-- null
testnoop / skip / ignore (tbd)Nnull <-- null or non-null == non-null

0.9.7

6 months ago

0.9.4

7 months ago

0.9.3

7 months ago

0.9.5

7 months ago

0.3.9

11 months ago

0.9.0

11 months ago

0.5.4

11 months ago

0.5.3

11 months ago

0.9.1

11 months ago

0.5.1

11 months ago

0.3.1-alpha

11 months ago