From: Jérôme Benoit Date: Mon, 11 May 2026 19:20:30 +0000 (+0200) Subject: refactor: consolidate object-check utilities to eliminate duplication X-Git-Tag: cli@v4.7.3~12 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=2990944535b2e13826897657d526da65b9d17ec4;p=e-mobility-charging-stations-simulator.git refactor: consolidate object-check utilities to eliminate duplication Replace type() + isObject with a single isPlainObject helper. Make isJsonObject and assertIsJsonObject delegate through it instead of duplicating the check logic independently. --- diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index eb2de87a..1ade0f7a 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -59,20 +59,12 @@ export const has = (property: PropertyKey, object: unknown): boolean => { return Object.hasOwn(object, property) } -const type = (value: unknown): string => { - if (value === null) return 'Null' - if (value === undefined) return 'Undefined' - if (Number.isNaN(value)) return 'NaN' - if (Array.isArray(value)) return 'Array' - return Object.prototype.toString.call(value).slice(8, -1) -} - -const isObject = (value: unknown): value is object => { - return type(value) === 'Object' +const isPlainObject = (value: unknown): value is object => { + return Object.prototype.toString.call(value) === '[object Object]' } export const isJsonObject = (value: unknown): value is JsonObject => { - return value != null && typeof value === 'object' && !Array.isArray(value) + return isPlainObject(value) } /** @@ -85,7 +77,7 @@ export function assertIsJsonObject ( value: unknown, error?: Error | string ): asserts value is JsonObject { - if (value == null || typeof value !== 'object' || Array.isArray(value)) { + if (!isJsonObject(value)) { if (error instanceof Error) { throw error } @@ -109,7 +101,7 @@ export const isEmpty = (value: unknown): boolean => { if (Array.isArray(value)) return value.length === 0 if (value instanceof Map) return value.size === 0 if (value instanceof Set) return value.size === 0 - if (isObject(value)) return Object.keys(value).length === 0 + if (isPlainObject(value)) return Object.keys(value).length === 0 return false } @@ -117,11 +109,11 @@ export const isEmpty = (value: unknown): boolean => { export const mergeDeepRight = (target: T, source: S): T => { const output: Record = { ...(target as Record) } - if (isObject(target) && isObject(source)) { + if (isPlainObject(target) && isPlainObject(source)) { Object.keys(source).forEach(key => { const sourceValue = (source as Record)[key] const targetValue = (target as Record)[key] - if (isObject(sourceValue) && isObject(targetValue)) { + if (isPlainObject(sourceValue) && isPlainObject(targetValue)) { output[key] = mergeDeepRight( targetValue as Record, sourceValue as Record