feat(lib/utils/objects): add isObject and mergeDeep

This commit is contained in:
PalmDevs 2025-05-22 01:09:04 +07:00
parent fe43fd14d4
commit 4df920009e
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA

View file

@ -1,3 +1,5 @@
import type { AnyObject } from './types'
// Retain reference to original functions
export const objectFreeze = Object.freeze
export const objectDefineProperty = Object.defineProperty
@ -64,3 +66,31 @@ export function interceptProperty(prop: PropertyKey, callback: (target: object,
_intercepting.delete(prop)
}
}
/**
* Simple check if to see if value is an object.
*
* @param val The value to check.
*/
export function isObject(val: any): val is AnyObject {
return val && typeof val === 'object' && !Array.isArray(val)
}
/**
* Deep merge two objects.
*
* @param target The object to merge into.
* @param source The object to merge from.
*
* @returns The merged target.
*/
export function mergeDeep(target: AnyObject, source: AnyObject) {
if (isObject(target) && isObject(source))
for (const key in source)
if (isObject(source[key])) {
if (!target[key]) Object.assign(target, { [key]: {} })
mergeDeep(target[key], source[key])
} else Object.assign(target, { [key]: source[key] })
return target
}