refactor: rename cloneObject() -> clone()
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 13 Jan 2024 13:59:33 +0000 (14:59 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 13 Jan 2024 13:59:33 +0000 (14:59 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/Helpers.ts
src/charging-station/ocpp/OCPPRequestService.ts
src/utils/Utils.ts
src/utils/index.ts
tests/utils/Utils.test.ts

index 3d53fc812488b7c8a9b7b47d72460510ec8aca8c..aa3728129b74c9f1ca6f4c00d3fd0fa09748dde6 100644 (file)
@@ -19,7 +19,7 @@ import {
 } from '../types/index.js'
 import {
   Constants,
-  cloneObject,
+  clone,
   convertToDate,
   formatDurationMilliSeconds,
   getRandomInteger,
@@ -371,7 +371,7 @@ export class AutomaticTransactionGenerator {
   private getConnectorStatus (connectorId: number): Status {
     const connectorStatus =
       this.chargingStation.getAutomaticTransactionGeneratorStatuses()?.[connectorId - 1] != null
-        ? cloneObject<Status>(
+        ? clone<Status>(
           // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
           this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1]
         )
index 7046f541095152581c4f3c2c89bd826e854087b7..6cfcd707d489f3e8e709247d377f240226c8e160 100644 (file)
@@ -132,7 +132,7 @@ import {
   buildStartedMessage,
   buildStoppedMessage,
   buildUpdatedMessage,
-  cloneObject,
+  clone,
   convertToBoolean,
   convertToDate,
   convertToInt,
@@ -1397,11 +1397,11 @@ export class ChargingStation extends EventEmitter {
   private initializeConnectorsOrEvsesFromFile (configuration: ChargingStationConfiguration): void {
     if (configuration.connectorsStatus != null && configuration.evsesStatus == null) {
       for (const [connectorId, connectorStatus] of configuration.connectorsStatus.entries()) {
-        this.connectors.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus))
+        this.connectors.set(connectorId, clone<ConnectorStatus>(connectorStatus))
       }
     } else if (configuration.evsesStatus != null && configuration.connectorsStatus == null) {
       for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) {
-        const evseStatus = cloneObject<EvseStatusConfiguration>(evseStatusConfiguration)
+        const evseStatus = clone<EvseStatusConfiguration>(evseStatusConfiguration)
         delete evseStatus.connectorsStatus
         this.evses.set(evseId, {
           ...(evseStatus as EvseStatus),
@@ -1488,7 +1488,7 @@ export class ChargingStation extends EventEmitter {
               this.logPrefix(),
               this.templateFile
             )
-            this.connectors.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus))
+            this.connectors.set(connectorId, clone<ConnectorStatus>(connectorStatus))
           }
           initializeConnectorsMapStatus(this.connectors, this.logPrefix())
           this.saveConnectorsStatus()
@@ -1632,7 +1632,7 @@ export class ChargingStation extends EventEmitter {
         const configurationFromFile = this.getConfigurationFromFile()
         let configurationData: ChargingStationConfiguration =
           configurationFromFile != null
-            ? cloneObject<ChargingStationConfiguration>(configurationFromFile)
+            ? clone<ChargingStationConfiguration>(configurationFromFile)
             : {}
         if (this.stationInfo?.stationInfoPersistentConfiguration === true) {
           configurationData.stationInfo = this.stationInfo
index 50b26885bbf999e21e5e92d243475a0a68c55871..8be89fe1a65d3ee812ddd88ad11e43bdf292a895 100644 (file)
@@ -57,7 +57,7 @@ import {
   ACElectricUtils,
   Constants,
   DCElectricUtils,
-  cloneObject,
+  clone,
   convertToDate,
   convertToInt,
   isArraySorted,
@@ -331,7 +331,7 @@ export const buildConnectorsMap = (
       const connectorStatus = connectors[connector]
       const connectorId = convertToInt(connector)
       checkStationInfoConnectorStatus(connectorId, connectorStatus, logPrefix, templateFile)
-      connectorsMap.set(connectorId, cloneObject<ConnectorStatus>(connectorStatus))
+      connectorsMap.set(connectorId, clone<ConnectorStatus>(connectorStatus))
     }
   } else {
     logger.warn(
@@ -468,7 +468,7 @@ export const warnTemplateKeysDeprecation = (
 export const stationTemplateToStationInfo = (
   stationTemplate: ChargingStationTemplate
 ): ChargingStationInfo => {
-  stationTemplate = cloneObject<ChargingStationTemplate>(stationTemplate)
+  stationTemplate = clone<ChargingStationTemplate>(stationTemplate)
   delete stationTemplate.power
   delete stationTemplate.powerUnit
   delete stationTemplate.Connectors
@@ -566,7 +566,7 @@ export const getConnectorChargingProfiles = (
   chargingStation: ChargingStation,
   connectorId: number
 ): ChargingProfile[] => {
-  return cloneObject<ChargingProfile[]>(
+  return clone<ChargingProfile[]>(
     (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? [])
       .sort((a, b) => b.stackLevel - a.stackLevel)
       .concat(
index dc2ab503e394dc786b12f9580af5393946a14933..e03feba9650d2d4cb8789a77bc9e17cb2a37f74d 100644 (file)
@@ -24,7 +24,7 @@ import {
   type ResponseType
 } from '../../types/index.js'
 import {
-  cloneObject,
+  clone,
   formatDurationMilliSeconds,
   handleSendMessageError,
   logger
@@ -214,7 +214,7 @@ export abstract class OCPPRequestService {
       return true
     }
     const validate = this.getJsonRequestValidateFunction<T>(commandName as RequestCommand)
-    payload = cloneObject<T>(payload)
+    payload = clone<T>(payload)
     OCPPServiceUtils.convertDateToISOString<T>(payload)
     if (validate(payload)) {
       return true
@@ -267,7 +267,7 @@ export abstract class OCPPRequestService {
     const validate = this.getJsonRequestResponseValidateFunction<T>(
       commandName as IncomingRequestCommand
     )
-    payload = cloneObject<T>(payload)
+    payload = clone<T>(payload)
     OCPPServiceUtils.convertDateToISOString<T>(payload)
     if (validate(payload)) {
       return true
index 3827d0dde99c519d6efb9477ff39e60cfb3d18ab..95c36bf93c53b147cf77d2f29756f6891da529b0 100644 (file)
@@ -257,7 +257,7 @@ const deepClone = <I extends CloneableData, O extends CloneableData = I>(
   return clone as O
 }
 
-export const cloneObject = <T>(object: T): T => {
+export const clone = <T>(object: T): T => {
   return deepClone(object as CloneableData) as T
 }
 
index 58fc23762b3cc5fd9f709bfcee5db0cdbc116647..0069a8755229874d678502da3748ec27cdae2f15 100644 (file)
@@ -25,7 +25,7 @@ export {
 } from './MessageChannelUtils.js'
 export {
   JSONStringifyWithMapSupport,
-  cloneObject,
+  clone,
   convertToBoolean,
   convertToDate,
   convertToFloat,
index efd0470db275e4ac25deede04fe75d2b70983a69..0a8a488282bfe99c01b6f7d5c0fc81b2aa4934f6 100644 (file)
@@ -5,7 +5,7 @@ import { expect } from 'expect'
 
 import { Constants } from '../../src/utils/Constants.js'
 import {
-  cloneObject,
+  clone,
   convertToBoolean,
   convertToDate,
   convertToFloat,
@@ -68,7 +68,7 @@ await describe('Utils test suite', async () => {
     expect(formatDurationSeconds(hoursToSeconds(4380))).toBe('182 days 12 hours')
   })
 
-  await it('Verify isValidTime()', () => {
+  await it('Verify isValidDate()', () => {
     expect(isValidDate(undefined)).toBe(false)
     expect(isValidDate(-1)).toBe(true)
     expect(isValidDate(0)).toBe(true)
@@ -248,33 +248,33 @@ await describe('Utils test suite', async () => {
     expect(isObject(new WeakSet())).toBe(true)
   })
 
-  await it('Verify cloneObject()', () => {
+  await it('Verify clone()', () => {
     const obj = { 1: 1 }
-    expect(cloneObject(obj)).toStrictEqual(obj)
-    expect(cloneObject(obj) === obj).toBe(false)
+    expect(clone(obj)).toStrictEqual(obj)
+    expect(clone(obj) === obj).toBe(false)
     const nestedObj = { 1: obj, 2: obj }
-    expect(cloneObject(nestedObj)).toStrictEqual(nestedObj)
-    expect(cloneObject(nestedObj) === nestedObj).toBe(false)
+    expect(clone(nestedObj)).toStrictEqual(nestedObj)
+    expect(clone(nestedObj) === nestedObj).toBe(false)
     const array = [1, 2]
-    expect(cloneObject(array)).toStrictEqual(array)
-    expect(cloneObject(array) === array).toBe(false)
+    expect(clone(array)).toStrictEqual(array)
+    expect(clone(array) === array).toBe(false)
     const objArray = [obj, obj]
-    expect(cloneObject(objArray)).toStrictEqual(objArray)
-    expect(cloneObject(objArray) === objArray).toBe(false)
+    expect(clone(objArray)).toStrictEqual(objArray)
+    expect(clone(objArray) === objArray).toBe(false)
     const date = new Date()
-    expect(cloneObject(date)).toStrictEqual(date)
-    expect(cloneObject(date) === date).toBe(false)
+    expect(clone(date)).toStrictEqual(date)
+    expect(clone(date) === date).toBe(false)
     const map = new Map([['1', '2']])
-    expect(cloneObject(map)).toStrictEqual({})
+    expect(clone(map)).toStrictEqual({})
     const set = new Set(['1'])
-    expect(cloneObject(set)).toStrictEqual({})
+    expect(clone(set)).toStrictEqual({})
     // The URL object seems to have not enumerable properties
     const url = new URL('https://domain.tld')
-    expect(cloneObject(url)).toStrictEqual({})
+    expect(clone(url)).toStrictEqual({})
     const weakMap = new WeakMap([[{ 1: 1 }, { 2: 2 }]])
-    expect(cloneObject(weakMap)).toStrictEqual({})
+    expect(clone(weakMap)).toStrictEqual({})
     const weakSet = new WeakSet([{ 1: 1 }, { 2: 2 }])
-    expect(cloneObject(weakSet)).toStrictEqual({})
+    expect(clone(weakSet)).toStrictEqual({})
   })
 
   await it('Verify hasOwnProp()', () => {