X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=ae27035ff079c028f30bee9884d5b2de5521c18a;hb=55ae7b758f478a2beb4557bbc96363fb913dcc73;hp=9d2b92fa4239856b0c064d062168dc5cb121a93d;hpb=79fd697f69253d44e4b3df2b1edd7a73338d12b2;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 9d2b92fa..ae27035f 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -14,7 +14,11 @@ import { } from 'date-fns' import { Constants } from './Constants.js' -import { type TimestampedData, WebSocketCloseEventStatusString } from '../types/index.js' +import { + type EmptyObject, + type TimestampedData, + WebSocketCloseEventStatusString +} from '../types/index.js' export const logPrefix = (prefixString = ''): string => { return `${new Date().toLocaleString()}${prefixString}` @@ -236,7 +240,7 @@ const deepClone = ( return clone as O } if (value instanceof Date) { - return new Date(value.valueOf()) as O + return new Date(value.getTime()) as O } if (typeof value !== 'object' || value === null) { return value as unknown as O @@ -257,11 +261,22 @@ export const clone = (object: T): T => { return deepClone(object as CloneableData) as T } +/** + * Detects whether the given value is an asynchronous function or not. + * + * @param fn - Unknown value. + * @returns `true` if `fn` was an asynchronous function, otherwise `false`. + * @internal + */ +export const isAsyncFunction = (fn: unknown): fn is (...args: unknown[]) => Promise => { + return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction' +} + export const isObject = (value: unknown): value is object => { return value != null && typeof value === 'object' && !Array.isArray(value) } -export const isEmptyObject = (object: object): object is Record => { +export const isEmptyObject = (object: object): object is EmptyObject => { if (object.constructor !== Object) { return false }