Skip to content

Commit

Permalink
refactor: upgrade code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Stevens committed Nov 13, 2020
1 parent bc0bb4b commit b785437
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 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 isPlainObj from 'is-plain-obj'

function defaultIsMergeable(value) {
return Array.isArray(value) || isPlainObj(value)
Expand All @@ -15,24 +15,24 @@ function cloneUnlessOtherwiseSpecified(value, options) {
}

function defaultArrayMerge(target, source, options) {
return target.concat(source).map(function(element) {
return cloneUnlessOtherwiseSpecified(element, options)
})
return target.concat(source).map((element) =>
cloneUnlessOtherwiseSpecified(element, options)
)
}

function getMergeFunction(key, options) {
if (!options.customMerge) {
return deepmerge
}
var customMerge = options.customMerge(key)
const customMerge = options.customMerge(key)
return typeof customMerge === 'function' ? customMerge : deepmerge
}

function getEnumerableOwnPropertySymbols(target) {
return Object.getOwnPropertySymbols
? Object.getOwnPropertySymbols(target).filter(function(symbol) {
return target.propertyIsEnumerable(symbol)
})
? Object.getOwnPropertySymbols(target).filter((symbol) =>
target.propertyIsEnumerable(symbol)
)
: []
}

Expand All @@ -56,13 +56,13 @@ function propertyIsUnsafe(target, key) {
}

function mergeObject(target, source, options) {
var destination = {}
const destination = {}
if (options.isMergeable(target)) {
getKeys(target).forEach(function(key) {
getKeys(target).forEach((key) => {
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options)
})
}
getKeys(source).forEach(function(key) {
getKeys(source).forEach((key) => {
if (propertyIsUnsafe(target, key)) {
return
}
Expand All @@ -77,16 +77,16 @@ function mergeObject(target, source, options) {
}

export default function deepmerge(target, source, options) {
options = Object.assign({
options = {
arrayMerge: defaultArrayMerge,
isMergeable: defaultIsMergeable
}, options, {
isMergeable: defaultIsMergeable,
...options,
cloneUnlessOtherwiseSpecified: cloneUnlessOtherwiseSpecified
})
}

var sourceIsArray = Array.isArray(source)
var targetIsArray = Array.isArray(target)
var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray
const sourceIsArray = Array.isArray(source)
const targetIsArray = Array.isArray(target)
const sourceAndTargetTypesMatch = sourceIsArray === targetIsArray

if (!sourceAndTargetTypesMatch) {
return cloneUnlessOtherwiseSpecified(source, options)
Expand All @@ -102,7 +102,5 @@ export function deepmergeAll(array, options) {
throw new Error('first argument should be an array')
}

return array.reduce(function(prev, next) {
return deepmerge(prev, next, options)
}, {})
return array.reduce((prev, next) => deepmerge(prev, next, options), {})
}

0 comments on commit b785437

Please sign in to comment.