refactor: cleanup isNullOrdefined usage
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 29 Dec 2023 18:09:21 +0000 (19:09 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 29 Dec 2023 18:09:21 +0000 (19:09 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/Bootstrap.ts
src/charging-station/SharedLRUCache.ts
src/charging-station/broadcast-channel/ChargingStationWorkerBroadcastChannel.ts

index 329f748367d0a20790cca278f08a92e549882a22..94198ac6f502d7433f7c5b0498c476ec328b52ff 100644 (file)
@@ -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}` : ''
       }:`
     )
   }
index 6a8d79fae543fa9bf51e3c8005a1e72191065680..5b23676f24e39049cedf24f7581132f5b265a925 100644 (file)
@@ -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)`
                 : ''
             }`
index 97f417e72fb8813e67bf7a384e2d7743697e0f53..123d8701e8aa05d48b7fe9c476f2933ba5c1395f 100644 (file)
@@ -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)
     )
   }
index d990c48e8cf698ca9e0c67d7ddbcb0b5967d3a50..a6125de2d51a7a0cf0feeec61909f2b511466028 100644 (file)
@@ -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) {