-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
90 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { mergeWith } from './mergeWith' | ||
|
||
export function merge<T extends Record<string, any>, K extends Record<string, any>>(object: T, source: K) { | ||
return mergeWith(object, source, () => undefined) | ||
export function merge<T extends Record<string, any>, K extends Record<string, any>>(object: T, ...sources: K[]) { | ||
return mergeWith(object, ...sources, () => undefined) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
import { hasOwn, isArray, isObject } from '../general' | ||
import { at } from '../array' | ||
import { hasOwn, isArray, isFunction, isObject } from '../general' | ||
|
||
type Fn = (objValue: any, srcValue: any, key: string | number | symbol, object?: any, source?: any) => any | ||
|
||
export function mergeWith<T extends Record<string, any>, K extends Record<string, any>>( | ||
object: T, | ||
source: K, | ||
fn: (objValue: any, srcValue: any, key: string | number | symbol, object?: T, source?: K) => any, | ||
...sources: [...K[], fn: Fn] | ||
): T & K { | ||
function baseMerge(target: any, src: any): any { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const key in src) { | ||
if (hasOwn(src, key)) { | ||
const srcValue = src[key] | ||
const targetValue = target[key] | ||
|
||
const customResult = fn(targetValue, srcValue, key, object, source) | ||
|
||
if (customResult !== undefined) { | ||
target[key] = customResult | ||
} else if (isObject(srcValue)) { | ||
if (isObject(targetValue)) { | ||
target[key] = baseMerge(targetValue, srcValue) | ||
const fn = at(sources, -1) as Fn | ||
const targets = [object, ...sources.slice(0, -1)] as (T & K)[] | ||
|
||
let len = targets.length - 1 | ||
let result = targets[len] | ||
|
||
while (len) { | ||
result = baseMergeWith(targets[len - 1], result, fn) | ||
len-- | ||
} | ||
|
||
function baseMergeWith<T extends Record<string, any>, K extends Record<string, any>>( | ||
object: T, | ||
source: K, | ||
fn: (objValue: any, srcValue: any, key: string | number | symbol, object?: T, source?: K) => any, | ||
): T & K { | ||
function baseMerge(target: any, src: any): any { | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const key in src) { | ||
if (hasOwn(src, key)) { | ||
const srcValue = src[key] | ||
const targetValue = target[key] | ||
|
||
const customResult = fn(targetValue, srcValue, key, object, source) | ||
|
||
if (customResult !== undefined) { | ||
target[key] = customResult | ||
} else if (isObject(srcValue)) { | ||
if (isObject(targetValue)) { | ||
target[key] = baseMerge(targetValue, srcValue) | ||
} else { | ||
target[key] = baseMerge(isArray(srcValue) ? [] : {}, srcValue) | ||
} | ||
} else { | ||
target[key] = baseMerge(isArray(srcValue) ? [] : {}, srcValue) | ||
target[key] = srcValue | ||
} | ||
} else { | ||
target[key] = srcValue | ||
} | ||
} | ||
return target | ||
} | ||
return target | ||
|
||
return baseMerge(object as any, source as any) as T & K | ||
} | ||
|
||
return baseMerge(object as any, source as any) as T & K | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters