Skip to content

Commit

Permalink
refactor: move options stuff to its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Stevens committed Nov 17, 2020
1 parent 221e9fd commit 316c459
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 29 deletions.
30 changes: 1 addition & 29 deletions src/deepmerge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isPlainObj from 'is-plain-obj'
import { getFullOptions } from './options'
import {
cloneUnlessOtherwiseSpecified,
getKeys,
Expand All @@ -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)) {
Expand Down Expand Up @@ -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))
}
Expand Down
31 changes: 31 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
@@ -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
};
}

0 comments on commit 316c459

Please sign in to comment.