|
| 1 | +# merge |
| 2 | + |
| 3 | +The `merge` function is a powerful, flexible tool for merging objects, arrays, and their nested properties. |
| 4 | +It supports deep merging, merging multiple sources, property descriptor cloning, array merging strategies, |
| 5 | +and custom filtering logic. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- **Deep Merging**: Option to deeply merge objects and arrays. |
| 10 | +- **Multiple Sources**: Merge an array of objects into a target object. |
| 11 | +- **Merge Arrays**: Choose between overwriting, appending, or merging unique array elements. |
| 12 | +- **Property Descriptors**: Clone getter/setter and other property descriptors. |
| 13 | +- **Custom Filters**: Exclude specific properties based on custom logic. |
| 14 | +- **Security**: Built-in protection against prototype pollution (`__proto__`, `constructor`). |
| 15 | +- **TypeScript Ready**: Full type safety and intelligent merging of source types. |
| 16 | + |
| 17 | +## Basic Usage |
| 18 | + |
| 19 | +```typescript |
| 20 | +import { merge } from '@jsopen/objects'; |
| 21 | + |
| 22 | +const target = { a: 1, b: 2 }; |
| 23 | +const source = { b: 3, c: 4 }; |
| 24 | + |
| 25 | +merge(target, source); |
| 26 | +// target is now: { a: 1, b: 3, c: 4 } |
| 27 | +``` |
| 28 | + |
| 29 | +### Merging Multiple Sources |
| 30 | + |
| 31 | +```typescript |
| 32 | +const target = { a: 1 }; |
| 33 | +const sources = [{ b: 2 }, { c: 3 }]; |
| 34 | + |
| 35 | +merge(target, sources); |
| 36 | +// target is now: { a: 1, b: 2, c: 3 } |
| 37 | +``` |
| 38 | + |
| 39 | +## Options |
| 40 | + |
| 41 | +The `merge` function accepts an optional `Options` object as its third argument. |
| 42 | + |
| 43 | +| Option | Type | Default | Description | |
| 44 | +|:------------------|:------------------------------------|:--------|:--------------------------------------------------------------------------------------| |
| 45 | +| `deep` | `boolean \| 'full' \| CallbackFn` | `false` | Enables deep merging. `true` only for plain objects/arrays, `'full'` for all objects. | |
| 46 | +| `mergeArrays` | `boolean \| 'unique' \| CallbackFn` | `false` | Controls how arrays are merged. | |
| 47 | +| `keepExisting` | `boolean \| CallbackFn` | `false` | If true, does not overwrite existing properties in the target. | |
| 48 | +| `copyDescriptors` | `boolean` | `false` | If true, copies property descriptors (getters, setters, etc.). | |
| 49 | +| `symbolKeys` | `boolean` | `true` | Whether to include properties with Symbol keys. | |
| 50 | +| `ignoreUndefined` | `boolean` | `true` | If true, `undefined` values in source will be ignored. | |
| 51 | +| `ignoreNulls` | `boolean` | `false` | If true, `null` values in source will be ignored. | |
| 52 | +| `ignoreSource` | `CallbackFn` | - | Callback to ignore specific source values. | |
| 53 | +| `filter` | `CallbackFn` | - | Callback to exclude properties from both target and source. | |
| 54 | + |
| 55 | +### Deep Merging |
| 56 | + |
| 57 | +- `true`: Merges plain objects and arrays recursively. Non-plain objects (class instances) are assigned by reference. |
| 58 | +- `'full'`: Merges all objects including class instances (excluding built-in types like `Date`, `RegExp`, etc.). |
| 59 | +- `CallbackFn`: A function `(value, context) => boolean` to decide whether to go deep for a specific path. |
| 60 | + |
| 61 | +```typescript |
| 62 | +merge(target, source, { deep: true }); |
| 63 | + |
| 64 | +// Using a callback to deep merge only specific paths |
| 65 | +merge(target, source, { |
| 66 | + deep: (val, { path }) => path.startsWith('metadata') |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +### Array Merging |
| 71 | + |
| 72 | +- `true`: Appends source array elements to the target array. |
| 73 | +- `'unique'`: Appends source array elements and ensures the resulting array has unique values. |
| 74 | +- `CallbackFn`: A function `(value, context) => boolean` to decide whether to merge arrays for a specific path. |
| 75 | + |
| 76 | +```typescript |
| 77 | +const target = { tags: ['js'] }; |
| 78 | +const source = { tags: ['ts', 'js'] }; |
| 79 | + |
| 80 | +merge(target, source, { mergeArrays: 'unique' }); |
| 81 | +// target.tags is now: ['js', 'ts'] |
| 82 | +``` |
| 83 | + |
| 84 | +### Property Descriptors |
| 85 | + |
| 86 | +By default, `merge` copies values. Enable `copyDescriptors` to preserve getters, setters, and other attributes. |
| 87 | + |
| 88 | +```typescript |
| 89 | +const source = { |
| 90 | + get id() { |
| 91 | + return Math.random(); |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +merge(target, source, { copyDescriptors: true }); |
| 96 | +// target now has the 'id' getter, not just a static value. |
| 97 | +``` |
| 98 | + |
| 99 | +### Filtering and Ignoring |
| 100 | + |
| 101 | +```typescript |
| 102 | +merge(target, source, { |
| 103 | + // Ignore source values that are booleans |
| 104 | + ignoreSource: (val) => typeof val === 'boolean', |
| 105 | + |
| 106 | + // Ignore specific keys |
| 107 | + filter: (val, { key }) => key !== 'internalSecret', |
| 108 | + |
| 109 | + // Keep target's value if it already exists |
| 110 | + keepExisting: true |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +## Security |
| 115 | + |
| 116 | +The function automatically skips keys that could lead to prototype pollution: |
| 117 | + |
| 118 | +- `__proto__` |
| 119 | +- `constructor` |
| 120 | + |
| 121 | +## Type Definitions |
| 122 | + |
| 123 | +```typescript |
| 124 | +type CallbackFn = (value: any, ctx: CallbackContext) => boolean; |
| 125 | + |
| 126 | +interface CallbackContext { |
| 127 | + source: any; |
| 128 | + target: any; |
| 129 | + key: string | symbol | number; |
| 130 | + path: string; // Dot-separated path, e.g., "user.profile.id" |
| 131 | +} |
| 132 | + |
| 133 | +interface Options { |
| 134 | + deep?: boolean | 'full' | CallbackFn; |
| 135 | + symbolKeys?: boolean; |
| 136 | + mergeArrays?: boolean | 'unique' | CallbackFn; |
| 137 | + keepExisting?: boolean | CallbackFn; |
| 138 | + copyDescriptors?: boolean; |
| 139 | + ignoreSource?: CallbackFn; |
| 140 | + ignoreUndefined?: boolean; |
| 141 | + ignoreNulls?: boolean; |
| 142 | + filter?: CallbackFn; |
| 143 | +} |
| 144 | +``` |
0 commit comments