From: Jérôme Benoit Date: Sat, 13 Jan 2024 13:59:33 +0000 (+0100) Subject: refactor: rename cloneObject() -> clone() X-Git-Tag: v1.2.32~24 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=40615072ef972a19ec60372066d384f4e859a1c7;p=e-mobility-charging-stations-simulator.git refactor: rename cloneObject() -> clone() Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 3d53fc81..aa372812 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -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( + ? clone( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.chargingStation.getAutomaticTransactionGeneratorStatuses()![connectorId - 1] ) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 7046f541..6cfcd707 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -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)) + this.connectors.set(connectorId, clone(connectorStatus)) } } else if (configuration.evsesStatus != null && configuration.connectorsStatus == null) { for (const [evseId, evseStatusConfiguration] of configuration.evsesStatus.entries()) { - const evseStatus = cloneObject(evseStatusConfiguration) + const evseStatus = clone(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)) + this.connectors.set(connectorId, clone(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(configurationFromFile) + ? clone(configurationFromFile) : {} if (this.stationInfo?.stationInfoPersistentConfiguration === true) { configurationData.stationInfo = this.stationInfo diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 50b26885..8be89fe1 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -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)) + connectorsMap.set(connectorId, clone(connectorStatus)) } } else { logger.warn( @@ -468,7 +468,7 @@ export const warnTemplateKeysDeprecation = ( export const stationTemplateToStationInfo = ( stationTemplate: ChargingStationTemplate ): ChargingStationInfo => { - stationTemplate = cloneObject(stationTemplate) + stationTemplate = clone(stationTemplate) delete stationTemplate.power delete stationTemplate.powerUnit delete stationTemplate.Connectors @@ -566,7 +566,7 @@ export const getConnectorChargingProfiles = ( chargingStation: ChargingStation, connectorId: number ): ChargingProfile[] => { - return cloneObject( + return clone( (chargingStation.getConnectorStatus(connectorId)?.chargingProfiles ?? []) .sort((a, b) => b.stackLevel - a.stackLevel) .concat( diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index dc2ab503..e03feba9 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -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(commandName as RequestCommand) - payload = cloneObject(payload) + payload = clone(payload) OCPPServiceUtils.convertDateToISOString(payload) if (validate(payload)) { return true @@ -267,7 +267,7 @@ export abstract class OCPPRequestService { const validate = this.getJsonRequestResponseValidateFunction( commandName as IncomingRequestCommand ) - payload = cloneObject(payload) + payload = clone(payload) OCPPServiceUtils.convertDateToISOString(payload) if (validate(payload)) { return true diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 3827d0dd..95c36bf9 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -257,7 +257,7 @@ const deepClone = ( return clone as O } -export const cloneObject = (object: T): T => { +export const clone = (object: T): T => { return deepClone(object as CloneableData) as T } diff --git a/src/utils/index.ts b/src/utils/index.ts index 58fc2376..0069a875 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -25,7 +25,7 @@ export { } from './MessageChannelUtils.js' export { JSONStringifyWithMapSupport, - cloneObject, + clone, convertToBoolean, convertToDate, convertToFloat, diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index efd0470d..0a8a4882 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -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()', () => {