@fastify/deepmerge
Advanced tools
+14
-8
@@ -9,2 +9,8 @@ 'use strict' | ||
| function defaultIsMergeableObjectFactory () { | ||
| return function defaultIsMergeableObject (value) { | ||
| return typeof value === 'object' && value !== null && !(value instanceof RegExp) && !(value instanceof Date) | ||
| } | ||
| } | ||
| function deepmergeConstructor (options) { | ||
@@ -77,5 +83,5 @@ function isNotPrototypeKey (value) { | ||
| function isMergeableObject (value) { | ||
| return typeof value === 'object' && value !== null && !(value instanceof RegExp) && !(value instanceof Date) | ||
| } | ||
| const isMergeableObject = typeof options?.isMergeableObject === 'function' | ||
| ? options.isMergeableObject | ||
| : defaultIsMergeableObjectFactory() | ||
@@ -86,6 +92,2 @@ function isPrimitive (value) { | ||
| const isPrimitiveOrBuiltIn = typeof Buffer !== 'undefined' | ||
| ? (value) => typeof value !== 'object' || value === null || value instanceof RegExp || value instanceof Date || value instanceof Buffer | ||
| : (value) => typeof value !== 'object' || value === null || value instanceof RegExp || value instanceof Date | ||
| const mergeArray = options && typeof options.mergeArray === 'function' | ||
@@ -140,3 +142,3 @@ ? options.mergeArray({ clone, deepmerge: _deepmerge, getKeys, isMergeableObject }) | ||
| return source | ||
| } else if (isPrimitiveOrBuiltIn(target)) { | ||
| } else if (!isMergeableObject(target)) { | ||
| return clone(source) | ||
@@ -176,1 +178,5 @@ } else if (sourceIsArray && targetIsArray) { | ||
| module.exports.deepmerge = deepmergeConstructor | ||
| Object.defineProperty(module.exports, 'isMergeableObject', { | ||
| get: defaultIsMergeableObjectFactory | ||
| }) |
+1
-1
| { | ||
| "name": "@fastify/deepmerge", | ||
| "version": "3.0.0", | ||
| "version": "3.1.0", | ||
| "description": "Merges the enumerable properties of two or more objects deeply.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+22
-0
@@ -30,2 +30,3 @@ # @fastify/deepmerge | ||
| - `cloneProtoObject` (`function`, optional) - provide a function, which must return a clone of the object with the prototype of the object | ||
| - `isMergeableObject` (`function`, optional) - provide a function, which must return true if the object should be merged, default is `isMergeableObject` from this module | ||
@@ -137,2 +138,23 @@ ```js | ||
| #### isMergeableObject | ||
| By default, `@fastify/deepmerge` merges all objects except native `Date` and `RegExp` objects. To exclude certain objects from being merged, you can provide a custom function to the `isMergeableObject` option. | ||
| The default function is exported by this module as `isMergeableObject`. | ||
| The following example shows how to extend the default function to exclude globally defined `FormData` objects from being identified as mergeable objects. | ||
| ```js | ||
| const { isMergeableObject: defaultIsMergeableObject } = require('@fastify/deepmerge') | ||
| function customIsMergeableObject (source) { | ||
| return defaultIsMergeableObject(source) && !(source instanceof FormData) | ||
| } | ||
| const deepmergeWithCustomMergeableObject = require('@fastify/deepmerge')({ | ||
| isMergeableObject: customIsMergeableObject | ||
| }) | ||
| ``` | ||
| ## Benchmarks | ||
@@ -139,0 +161,0 @@ |
+3
-0
@@ -76,2 +76,3 @@ type DeepMergeFn = <T1, T2>(target: T1, source: T2) => DeepMerge<T1, T2> | ||
| mergeArray?: MergeArrayFn; | ||
| isMergeableObject?: (value: any) => boolean; | ||
| symbols?: boolean; | ||
@@ -87,2 +88,4 @@ all?: boolean; | ||
| export { deepmerge as default } | ||
| export const isMergeableObject: (value: any) => boolean | ||
| } | ||
@@ -89,0 +92,0 @@ |
17079
6.76%228
3.17%185
13.5%