X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FUtils.ts;h=9d2b92fa4239856b0c064d062168dc5cb121a93d;hb=1c1147036bb68c12685b2e182f910689646b3b11;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..9d2b92fa 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -77,9 +77,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 +206,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 @@ -261,8 +257,25 @@ 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) +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 => { + 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 +286,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 +294,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 +328,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 {