refactor: use ramdba helper for builtin types
[e-mobility-charging-stations-simulator.git] / src / utils / Utils.ts
index bc9a8f436a8c1e7c8b60457f817e9467a6c96933..aeed9b0fa44b4a073bff682583c95695cbb954ac 100644 (file)
@@ -1,4 +1,4 @@
-import { getRandomValues, randomBytes, randomInt, randomUUID } from 'node:crypto'
+import { getRandomValues, randomBytes, randomUUID } from 'node:crypto'
 import { env, nextTick } from 'node:process'
 
 import {
@@ -12,6 +12,7 @@ import {
   minutesToSeconds,
   secondsToMilliseconds
 } from 'date-fns'
+import { is } from 'rambda'
 
 import {
   type JsonType,
@@ -19,7 +20,6 @@ import {
   type TimestampedData,
   WebSocketCloseEventStatusString
 } from '../types/index.js'
-import { Constants } from './Constants.js'
 
 export const logPrefix = (prefixString = ''): string => {
   return `${new Date().toLocaleString()}${prefixString}`
@@ -159,15 +159,6 @@ export const getRandomFloat = (max = Number.MAX_VALUE, min = 0): number => {
   return (randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min
 }
 
-export const getRandomInteger = (max = Constants.MAX_RANDOM_INTEGER, min = 0): number => {
-  max = Math.floor(max)
-  if (min !== 0) {
-    min = Math.ceil(min)
-    return Math.floor(randomInt(min, max + 1))
-  }
-  return Math.floor(randomInt(max + 1))
-}
-
 /**
  * Rounds the given number to the given scale.
  * The rounding is done using the "round half away from zero" method.
@@ -225,11 +216,11 @@ export const clone = <T>(object: T): T => {
  * @internal
  */
 export const isAsyncFunction = (fn: unknown): fn is (...args: unknown[]) => Promise<unknown> => {
-  return typeof fn === 'function' && fn.constructor.name === 'AsyncFunction'
+  return is(Function, fn) && fn.constructor.name === 'AsyncFunction'
 }
 
 export const isObject = (value: unknown): value is object => {
-  return value != null && typeof value === 'object' && !Array.isArray(value)
+  return value != null && !Array.isArray(value) && is(Object, value)
 }
 
 export const hasOwnProp = (value: unknown, property: PropertyKey): boolean => {
@@ -287,7 +278,7 @@ export const JSONStringify = <
   return JSON.stringify(
     object,
     (_, value: Record<string, unknown>) => {
-      if (value instanceof Map) {
+      if (is(Map, value)) {
         switch (mapFormat) {
           case MapStringifyFormat.object:
             return { ...Object.fromEntries<Map<string, Record<string, unknown>>>(value.entries()) }
@@ -295,7 +286,7 @@ export const JSONStringify = <
           default:
             return [...value]
         }
-      } else if (value instanceof Set) {
+      } else if (is(Set, value)) {
         return [...value] as JsonType[]
       }
       return value