@@ -16,8 +16,14 @@ export const lazy = fn =>
1616 * @param {* } value - The value to check
1717 * @returns {boolean } True if the value is a plain object, false otherwise
1818 */
19- export const isPlainObject = value =>
20- value !== null && typeof value === 'object' && ! Array . isArray ( value ) ;
19+ export const isPlainObject = value => {
20+ if ( value === null || typeof value !== 'object' ) {
21+ return false ;
22+ }
23+
24+ const prototype = Object . getPrototypeOf ( value ) ;
25+ return prototype === Object . prototype || prototype === null ;
26+ } ;
2127
2228/**
2329 * Checks if a value is an async generator/iterable.
@@ -41,32 +47,26 @@ export const omitKeys = (obj, keys = []) =>
4147 ) ;
4248
4349/**
44- * Recursively merges multiple objects deeply .
50+ * Recursively merges plain objects from left to right .
4551 * @template T
4652 * @param {...T } objects - Any number of objects to merge
4753 * @returns {T } A new object containing the deep merge of all provided objects
4854 */
4955export const deepMerge = ( ...objects ) => {
50- const base = objects . pop ( ) ;
51-
5256 return objects . reduce ( ( result , source ) => {
53- for ( const key in source ) {
54- if ( Object . prototype . hasOwnProperty . call ( source , key ) ) {
55- const sourceValue = source [ key ] ;
56- const isSourcePlainObject = isPlainObject ( sourceValue ) ;
57-
58- const targetValue = result [ key ] ;
59- const baseValue = base [ key ] ;
60-
61- result [ key ] =
62- isSourcePlainObject && isPlainObject ( targetValue )
63- ? deepMerge ( targetValue , sourceValue , baseValue ?? { } )
64- : isSourcePlainObject && isPlainObject ( baseValue )
65- ? deepMerge ( sourceValue , baseValue )
66- : ( sourceValue ?? baseValue ) ;
57+ for ( const [ key , sourceValue ] of Object . entries ( source ) ) {
58+ if ( sourceValue === undefined ) {
59+ continue ;
6760 }
61+
62+ const targetValue = result [ key ] ;
63+ result [ key ] = isPlainObject ( sourceValue )
64+ ? deepMerge ( isPlainObject ( targetValue ) ? targetValue : { } , sourceValue )
65+ : Array . isArray ( sourceValue )
66+ ? sourceValue . slice ( )
67+ : sourceValue ;
6868 }
6969
7070 return result ;
71- } , base ) ;
71+ } , { } ) ;
7272} ;
0 commit comments