add isPlainObject method
This commit is contained in:
10
packages/core/src/utilities/isPlainObject.ts
Normal file
10
packages/core/src/utilities/isPlainObject.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
// see: https://github.com/mesqueeb/is-what/blob/88d6e4ca92fb2baab6003c54e02eedf4e729e5ab/src/index.ts
|
||||||
|
|
||||||
|
function getType(payload: any): string {
|
||||||
|
return Object.prototype.toString.call(payload).slice(8, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function isPlainObject(payload: any): payload is Record<string, any> {
|
||||||
|
if (getType(payload) !== 'Object') return false
|
||||||
|
return payload.constructor === Object && Object.getPrototypeOf(payload) === Object.prototype
|
||||||
|
}
|
||||||
@@ -1,12 +1,12 @@
|
|||||||
import { AnyObject } from '../types'
|
import { AnyObject } from '../types'
|
||||||
import isObject from './isObject'
|
import isPlainObject from './isPlainObject'
|
||||||
|
|
||||||
export default function mergeDeep(target: AnyObject, source: AnyObject): AnyObject {
|
export default function mergeDeep(target: AnyObject, source: AnyObject): AnyObject {
|
||||||
const output = { ...target }
|
const output = { ...target }
|
||||||
|
|
||||||
if (isObject(target) && isObject(source)) {
|
if (isPlainObject(target) && isPlainObject(source)) {
|
||||||
Object.keys(source).forEach(key => {
|
Object.keys(source).forEach(key => {
|
||||||
if (isObject(source[key])) {
|
if (isPlainObject(source[key])) {
|
||||||
if (!(key in target)) {
|
if (!(key in target)) {
|
||||||
Object.assign(output, { [key]: source[key] })
|
Object.assign(output, { [key]: source[key] })
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user