diff --git a/src/deepmerge.ts b/src/deepmerge.ts index fae2e24..c5ac725 100644 --- a/src/deepmerge.ts +++ b/src/deepmerge.ts @@ -1,4 +1,4 @@ -import isPlainObj from 'is-plain-obj' +import { getFullOptions } from './options' import { cloneUnlessOtherwiseSpecified, getKeys, @@ -7,16 +7,6 @@ import { propertyIsUnsafe } from './utils' -function defaultIsMergeable(value) { - return Array.isArray(value) || isPlainObj(value) -} - -function defaultArrayMerge(target, source, options) { - return target.concat(source).map((element) => - cloneUnlessOtherwiseSpecified(element, options) - ) -} - function mergeObject(target, source, options) { const destination = {} if (options.isMergeable(target)) { @@ -52,15 +42,6 @@ export function deepmergeImpl(target, source, options) { } } -function getFullOptions(options) { - return { - arrayMerge: defaultArrayMerge, - isMergeable: defaultIsMergeable, - ...options, - cloneUnlessOtherwiseSpecified: cloneUnlessOtherwiseSpecified - }; -} - export default function deepmerge(target, source, options) { return deepmergeImpl(target, source, getFullOptions(options)) } diff --git a/src/options.ts b/src/options.ts new file mode 100644 index 0000000..29dffc8 --- /dev/null +++ b/src/options.ts @@ -0,0 +1,23 @@ +import isPlainObj from "is-plain-obj" + +import { cloneUnlessOtherwiseSpecified } from "./utils" + +function defaultIsMergeable(value) { + return Array.isArray(value) || isPlainObj(value) +} + +function defaultArrayMerge(target, source, options) { + return [...target, ...source].map((element) => + cloneUnlessOtherwiseSpecified(element, options) + ) +} + +export function getFullOptions(options) { + return { + arrayMerge: defaultArrayMerge, + isMergeable: defaultIsMergeable, + clone: true, + ...options, + cloneUnlessOtherwiseSpecified: cloneUnlessOtherwiseSpecified, + } +}