From 4df920009eaabd5dc003b0a47432875f10f93755 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Thu, 22 May 2025 01:09:04 +0700 Subject: [PATCH] feat(lib/utils/objects): add `isObject` and `mergeDeep` --- lib/utils/src/objects.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lib/utils/src/objects.ts b/lib/utils/src/objects.ts index c2f54b9..dd68e53 100644 --- a/lib/utils/src/objects.ts +++ b/lib/utils/src/objects.ts @@ -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 +}