Skip to content

MomsFriendlyDevCo/Patch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@MomsFriendlyDevCo/Patch

Simple object patching.

This module is designed to compare two objects and return the differences between them in a third, patchable, object.

Features:

  • Simple, fast, ES6 compatible object patching
  • Arrays are treated as atomic - any change within them copies the entire array
  • Various complex built-in types can be treated as simple scalars (Dates, Sets etc.)
  • Functions for comparison or deep cloning can be customized
  • Does not mutate A or B
import {patch} from '@momsfriendlydevco/patch';

let a = {
    foo: 'Foo!',
    bar: 'Bar!',
    quz: [1, 2, 3],
    quark: {one: 1, two: 2},
    plugh: '123', // Wont be picked up as we're patching A against B
};

let b = {
    foo: 'Foo!',
    quz: [1, 2, 3, 4],
    quark: {two: 'two'},
    flarp: {uno: 1},
};

console.log( patch(a, b) ) //=
let c = {
    quz: [1, 2, 3, 4],
    quark: {two: 'two'},
    flarp: {uno: 1},
}

API

patch(a, b, options)

Return a third, patch, object for the differences of B against A.

Options can be:

Option Type Default Description
default * {} Value to return if the source object is exactly the same
scalars Array [Buffer, Date, Map, Set] Meta objects to treat as a single value
isEqual Function _.isEqual Equality tester
cloneDeep Function _.cloneDeep Deep cloner used for arrays

About

Simple object patching

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published