@fastify/deepmerge
Advanced tools
+9
-0
@@ -86,2 +86,4 @@ 'use strict' | ||
| const onlyDefinedProperties = options?.onlyDefinedProperties === true | ||
| function isPrimitive (value) { | ||
@@ -128,2 +130,5 @@ return typeof value !== 'object' || value === null | ||
| } else { | ||
| if (onlyDefinedProperties && typeof source[key] === 'undefined') { | ||
| continue | ||
| } | ||
| result[key] = clone(source[key]) | ||
@@ -136,2 +141,6 @@ } | ||
| function _deepmerge (target, source) { | ||
| if (onlyDefinedProperties && typeof source === 'undefined') { | ||
| return clone(target) | ||
| } | ||
| const sourceIsArray = Array.isArray(source) | ||
@@ -138,0 +147,0 @@ const targetIsArray = Array.isArray(target) |
+3
-4
| MIT License | ||
| Copyright (c) The Fastify Team | ||
| Copyright (c) 2022-present The Fastify team | ||
| The Fastify team members are listed at https://github.com/fastify/fastify#team | ||
| and in the README file. | ||
| The Fastify team members are listed at https://github.com/fastify/fastify#team. | ||
@@ -24,2 +23,2 @@ Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. | ||
| SOFTWARE. |
+2
-2
| { | ||
| "name": "@fastify/deepmerge", | ||
| "version": "3.1.0", | ||
| "version": "3.2.0", | ||
| "description": "Merges the enumerable properties of two or more objects deeply.", | ||
@@ -59,3 +59,3 @@ "main": "index.js", | ||
| "c8": "^10.1.3", | ||
| "tsd": "^0.31.1" | ||
| "tsd": "^0.33.0" | ||
| }, | ||
@@ -62,0 +62,0 @@ "files": [ |
+9
-0
@@ -31,2 +31,3 @@ # @fastify/deepmerge | ||
| - `isMergeableObject` (`function`, optional) - provide a function, which must return true if the object should be merged, default is `isMergeableObject` from this module | ||
| - `onlyDefinedProperties` (`boolean`, optional) - if `true`, properties with `undefined` in the source will not overwrite existing values from the target, default is `false` | ||
@@ -45,2 +46,10 @@ ```js | ||
| Example with `onlyDefinedProperties` option: | ||
| ```js | ||
| const deepmerge = require('@fastify/deepmerge')({ onlyDefinedProperties: true }) | ||
| const result = deepmerge({ a: 1, b: null }, { a: undefined, b: undefined }) | ||
| console.log(result) // { a: 1, b: null } | ||
| ``` | ||
| #### mergeArray | ||
@@ -47,0 +56,0 @@ |
+7
-0
@@ -71,2 +71,3 @@ type DeepMergeFn = <T1, T2>(target: T1, source: T2) => DeepMerge<T1, T2> | ||
| type CloneProtoObjectFn = (value: any) => any | ||
| type MergeArrayFn = (options: MergeArrayFnOptions) => (target: any[], source: any[]) => any[] | ||
@@ -80,2 +81,7 @@ | ||
| all?: boolean; | ||
| /** | ||
| * If true, ignore undefined values from source and keep existing target values. | ||
| * Defaults to false. | ||
| */ | ||
| onlyDefinedProperties?: boolean; | ||
| } | ||
@@ -94,4 +100,5 @@ | ||
| declare function deepmerge (options: Options & { all: true }): DeepMergeAllFn | ||
| declare function deepmerge (options?: Options): DeepMergeFn | ||
| export = deepmerge |
17932
4.99%240
5.26%194
4.86%