From 401fa922baa555f5997732052918ae2ab5ff184c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 29 Dec 2023 19:09:21 +0100 Subject: [PATCH] refactor: cleanup isNullOrdefined usage MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 5 ++--- src/charging-station/Bootstrap.ts | 3 +-- src/charging-station/SharedLRUCache.ts | 19 +++++++------------ .../ChargingStationWorkerBroadcastChannel.ts | 18 ++++++------------ 4 files changed, 16 insertions(+), 29 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 329f7483..94198ac6 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -22,7 +22,6 @@ import { cloneObject, formatDurationMilliSeconds, getRandomInteger, - isNullOrUndefined, logPrefix, logger, secureRandom, @@ -511,7 +510,7 @@ export class AutomaticTransactionGenerator { const transactionId = this.chargingStation.getConnectorStatus(connectorId)?.transactionId logger.debug( `${this.logPrefix(connectorId)} stopping a not started transaction${ - !isNullOrUndefined(transactionId) ? ` with id ${transactionId}` : '' + transactionId != null ? ` with id ${transactionId}` : '' }` ) } @@ -528,7 +527,7 @@ export class AutomaticTransactionGenerator { private readonly logPrefix = (connectorId?: number): string => { return logPrefix( ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${ - !isNullOrUndefined(connectorId) ? ` on connector #${connectorId}` : '' + connectorId != null ? ` on connector #${connectorId}` : '' }:` ) } diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index 6a8d79fa..5b23676f 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -36,7 +36,6 @@ import { handleUncaughtException, handleUnhandledRejection, isNotEmptyArray, - isNullOrUndefined, logPrefix, logger } from '../utils/index.js' @@ -156,7 +155,7 @@ export class Bootstrap extends EventEmitter { ? `/${workerConfiguration.poolMaxSize?.toString()}` : '' } worker(s) concurrently running in '${workerConfiguration.processType}' mode${ - !isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker) + this.workerImplementation?.maxElementsPerWorker != null ? ` (${this.workerImplementation?.maxElementsPerWorker} charging station(s) per worker)` : '' }` diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index 97f417e7..123d8701 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -2,12 +2,7 @@ import { LRUMapWithDelete as LRUCache } from 'mnemonist' import { Bootstrap } from './Bootstrap.js' import type { ChargingStationConfiguration, ChargingStationTemplate } from '../types/index.js' -import { - isEmptyObject, - isNotEmptyArray, - isNotEmptyString, - isNullOrUndefined -} from '../utils/index.js' +import { isEmptyObject, isNotEmptyArray, isNotEmptyString } from '../utils/index.js' enum CacheType { chargingStationTemplate = 'chargingStationTemplate', @@ -116,15 +111,15 @@ export class SharedLRUCache { chargingStationConfiguration: ChargingStationConfiguration ): boolean { return ( - !isNullOrUndefined(chargingStationConfiguration?.configurationKey) && - !isNullOrUndefined(chargingStationConfiguration?.stationInfo) && - !isNullOrUndefined(chargingStationConfiguration?.automaticTransactionGenerator) && - !isNullOrUndefined(chargingStationConfiguration?.configurationHash) && + chargingStationConfiguration?.configurationKey != null && + chargingStationConfiguration?.stationInfo != null && + chargingStationConfiguration?.automaticTransactionGenerator != null && + chargingStationConfiguration?.configurationHash != null && isNotEmptyArray(chargingStationConfiguration?.configurationKey) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - !isEmptyObject(chargingStationConfiguration.stationInfo!) && + !isEmptyObject(chargingStationConfiguration.stationInfo) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - !isEmptyObject(chargingStationConfiguration.automaticTransactionGenerator!) && + !isEmptyObject(chargingStationConfiguration.automaticTransactionGenerator) && isNotEmptyString(chargingStationConfiguration?.configurationHash) ) } diff --git a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts index d990c48e..a6125de2 100644 --- a/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts +++ b/src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts @@ -37,13 +37,7 @@ import { type StopTransactionRequest, type StopTransactionResponse } from '../../types/index.js' -import { - Constants, - convertToInt, - isEmptyObject, - isNullOrUndefined, - logger -} from '../../utils/index.js' +import { Constants, convertToInt, isEmptyObject, logger } from '../../utils/index.js' import type { ChargingStation } from '../ChargingStation.js' import { getConfigurationKey } from '../ConfigurationKeyUtils.js' import { buildMeterValue } from '../ocpp/index.js' @@ -278,12 +272,12 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne } const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest if ( - !isNullOrUndefined(requestPayload.hashIds) && - requestPayload.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false + requestPayload.hashIds != null && + !requestPayload.hashIds.includes(this.chargingStation.stationInfo.hashId) ) { return } - if (!isNullOrUndefined(requestPayload.hashId)) { + if (requestPayload.hashId != null) { logger.error( `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead` ) @@ -294,7 +288,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne let commandResponse: CommandResponse | void | undefined try { commandResponse = await this.commandHandler(command, requestPayload) - if (isNullOrUndefined(commandResponse) || isEmptyObject(commandResponse as CommandResponse)) { + if (commandResponse == null || isEmptyObject(commandResponse)) { responsePayload = { hashId: this.chargingStation.stationInfo.hashId, status: ResponseStatus.SUCCESS @@ -303,7 +297,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne responsePayload = this.commandResponseToResponsePayload( command, requestPayload, - commandResponse as CommandResponse + commandResponse ) } } catch (error) { -- 2.34.1