https://github.com/momsfriendlydevco/patch
Simple object patching
https://github.com/momsfriendlydevco/patch
Last synced: over 1 year ago
JSON representation
Simple object patching
- Host: GitHub
- URL: https://github.com/momsfriendlydevco/patch
- Owner: MomsFriendlyDevCo
- License: mit
- Created: 2022-11-16T23:33:08.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-11-17T00:12:52.000Z (over 3 years ago)
- Last Synced: 2025-03-25T12:50:55.712Z (over 1 year ago)
- Language: JavaScript
- Size: 39.1 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
@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
```javascript
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 |