X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=ae27035ff079c028f30bee9884d5b2de5521c18a;hb=e3fbf1aff8018d664908321682f051d49c972734;hp=95c36bf93c53b147cf77d2f29756f6891da529b0;hpb=40615072ef972a19ec60372066d384f4e859a1c7;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 95c36bf9..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}` @@ -77,9 +81,9 @@ export const isValidDate = (date: Date | number | undefined): date is Date | num export const convertToDate = ( value: Date | string | number | undefined | null -): Date | undefined | null => { +): Date | undefined => { if (value == null) { - return value + return undefined } if (isDate(value)) { return value @@ -206,10 +210,6 @@ export const extractTimeSeriesValues = (timeSeries: TimestampedData[]): number[] return timeSeries.map(timeSeriesItem => timeSeriesItem.value) } -export const isObject = (item: unknown): item is object => { - return item != null && typeof item === 'object' && !Array.isArray(item) -} - type CloneableData = | number | string @@ -240,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 @@ -261,8 +261,36 @@ export const clone = (object: T): T => { return deepClone(object as CloneableData) as T } -export const hasOwnProp = (object: unknown, property: PropertyKey): boolean => { - return isObject(object) && Object.hasOwn(object, property) +/** + * 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 EmptyObject => { + if (object.constructor !== Object) { + return false + } + // Iterates over the keys of an object, if + // any exist, return false. + // eslint-disable-next-line no-unreachable-loop + for (const _ in object) { + return false + } + return true +} + +export const hasOwnProp = (value: unknown, property: PropertyKey): boolean => { + return isObject(value) && Object.hasOwn(value, property) } export const isCFEnvironment = (): boolean => { @@ -273,7 +301,7 @@ const isString = (value: unknown): value is string => { return typeof value === 'string' } -export const isEmptyString = (value: unknown): value is string | undefined | null => { +export const isEmptyString = (value: unknown): value is '' | undefined | null => { return value == null || (isString(value) && value.trim().length === 0) } @@ -281,25 +309,12 @@ export const isNotEmptyString = (value: unknown): value is string => { return isString(value) && value.trim().length > 0 } -export const isEmptyArray = (object: unknown): object is unknown[] => { - return Array.isArray(object) && object.length === 0 -} - -export const isNotEmptyArray = (object: unknown): object is unknown[] => { - return Array.isArray(object) && object.length > 0 +export const isEmptyArray = (value: unknown): value is never[] => { + return Array.isArray(value) && value.length === 0 } -export const isEmptyObject = (obj: object): boolean => { - if (obj.constructor !== Object) { - return false - } - // Iterates over the keys of an object, if - // any exist, return false. - // eslint-disable-next-line no-unreachable-loop - for (const _ in obj) { - return false - } - return true +export const isNotEmptyArray = (value: unknown): value is unknown[] => { + return Array.isArray(value) && value.length > 0 } export const insertAt = (str: string, subStr: string, pos: number): string => @@ -328,11 +343,11 @@ export const secureRandom = (): number => { } export const JSONStringifyWithMapSupport = ( - obj: Record | Array> | Map, + object: Record | Array> | Map, space?: number ): string => { return JSON.stringify( - obj, + object, (_, value: Record) => { if (value instanceof Map) { return {