From 837387974ecc094a924864aeccec6967b195e9e4 Mon Sep 17 00:00:00 2001 From: Rebecca Stevens Date: Fri, 13 Nov 2020 16:06:19 +1300 Subject: [PATCH] refactor: move options stuff to its own file --- src/deepmerge.ts | 30 +----------------------------- src/options.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 29 deletions(-) create mode 100644 src/options.ts diff --git a/src/deepmerge.ts b/src/deepmerge.ts index b813efd..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,24 +42,6 @@ export function deepmergeImpl(target, source, options) { } } -function getFullOptions(options) { - const overrides = - options === undefined - ? undefined - : (Object.fromEntries( - // Filter out keys explicitly set to undefined. - Object.entries(options).filter(([key, value]) => value !== undefined) - )) - - return { - arrayMerge: defaultArrayMerge, - isMergeable: defaultIsMergeable, - clone: true, - ...overrides, - 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..7712c06 --- /dev/null +++ b/src/options.ts @@ -0,0 +1,31 @@ +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) { + const overrides = + options === undefined + ? undefined + : (Object.fromEntries( + // Filter out keys explicitly set to undefined. + Object.entries(options).filter(([key, value]) => value !== undefined) + )) + + return { + arrayMerge: defaultArrayMerge, + isMergeable: defaultIsMergeable, + clone: true, + ...overrides, + cloneUnlessOtherwiseSpecified + }; +}