refactor(simulator): use helper for undefined or null checks
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 7 Apr 2023 10:22:13 +0000 (12:22 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 7 Apr 2023 10:22:13 +0000 (12:22 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/ChargingStationWorkerBroadcastChannel.ts
src/charging-station/UIServiceWorkerBroadcastChannel.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ui-server/ui-services/AbstractUIService.ts
src/utils/Utils.ts

index 9728108b0fbb1f4f4676176b0338a5f6f628a2a7..d058866abae0da08c3d43937af6e3931e0872c6d 100644 (file)
@@ -417,7 +417,7 @@ export class AutomaticTransactionGenerator extends AsyncResource {
   private logPrefix = (connectorId?: number): string => {
     return Utils.logPrefix(
       ` ${this.chargingStation.stationInfo.chargingStationId} | ATG${
-        connectorId !== undefined ? ` on connector #${connectorId.toString()}` : ''
+        !Utils.isNullOrUndefined(connectorId) ? ` on connector #${connectorId.toString()}` : ''
       }:`
     );
   };
index 61a4d962137ed2946a6755285420b32332a31245..4f65a4e6a0c28122cf37e877daf7b4d953973670 100644 (file)
@@ -1320,8 +1320,7 @@ export class ChargingStation {
       }
       if (
         connectorId > 0 &&
-        (this.getConnectorStatus(connectorId)?.transactionStarted === undefined ||
-          this.getConnectorStatus(connectorId)?.transactionStarted === null)
+        Utils.isNullOrUndefined(this.getConnectorStatus(connectorId)?.transactionStarted)
       ) {
         this.initializeConnectorStatus(connectorId);
       }
index 49ce0811d534be1e339c466a2b839132749235e3..c03c4acb81db8aae2a01efee281e5558fbfb3a81 100644 (file)
@@ -258,12 +258,12 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
     }
     const [uuid, command, requestPayload] = validatedMessageEvent.data as BroadcastChannelRequest;
     if (
-      requestPayload?.hashIds !== undefined &&
+      !Utils.isNullOrUndefined(requestPayload?.hashIds) &&
       requestPayload?.hashIds?.includes(this.chargingStation.stationInfo.hashId) === false
     ) {
       return;
     }
-    if (requestPayload?.hashId !== undefined) {
+    if (!Utils.isNullOrUndefined(requestPayload?.hashId)) {
       logger.error(
         `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' array instead`
       );
@@ -274,8 +274,7 @@ export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChanne
     try {
       commandResponse = await this.commandHandler(command, requestPayload);
       if (
-        commandResponse === undefined ||
-        commandResponse === null ||
+        Utils.isNullOrUndefined(commandResponse) ||
         Utils.isEmptyObject(commandResponse as CommandResponse)
       ) {
         responsePayload = {
index 5b7ab4847503eacfa3fc02fc0684c01269b3ae5c..5f77b0e3450768bc79a87cacbfa744e77c3f268b 100644 (file)
@@ -6,7 +6,7 @@ import {
   type ResponsePayload,
   ResponseStatus,
 } from '../types';
-import { logger } from '../utils';
+import { Utils, logger } from '../utils';
 
 const moduleName = 'UIServiceWorkerBroadcastChannel';
 
@@ -69,7 +69,7 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
       status: responsesStatus,
       hashIdsSucceeded: this.responses
         .get(uuid)
-        ?.responses.filter(({ hashId }) => hashId !== undefined)
+        ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId))
         .map(({ status, hashId }) => {
           if (status === ResponseStatus.SUCCESS) {
             return hashId;
@@ -78,7 +78,7 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
       ...(responsesStatus === ResponseStatus.FAILURE && {
         hashIdsFailed: this.responses
           .get(uuid)
-          ?.responses.filter(({ hashId }) => hashId !== undefined)
+          ?.responses.filter(({ hashId }) => !Utils.isNullOrUndefined(hashId))
           .map(({ status, hashId }) => {
             if (status === ResponseStatus.FAILURE) {
               return hashId;
@@ -88,7 +88,7 @@ export class UIServiceWorkerBroadcastChannel extends WorkerBroadcastChannel {
       ...(responsesStatus === ResponseStatus.FAILURE && {
         responsesFailed: this.responses
           .get(uuid)
-          ?.responses.filter((response) => response !== undefined)
+          ?.responses.filter((response) => !Utils.isNullOrUndefined(response))
           .map((response) => {
             if (response.status === ResponseStatus.FAILURE) {
               return response;
index 68e9f7c66cae6febf94318a5a5523ef5ee4d6e3d..30ed58b00c1a41137aa20472962bccfadef2eee6 100644 (file)
@@ -404,7 +404,7 @@ export class OCPP16ResponseService extends OCPPResponseService {
         break;
       }
     }
-    const authorizeConnectorIdDefined = authorizeConnectorId !== undefined;
+    const authorizeConnectorIdDefined = !Utils.isNullOrUndefined(authorizeConnectorId);
     if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) {
       authorizeConnectorIdDefined &&
         (chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = true);
index 0a636edbe8fc07bbd66ebf4be11d24e0e60842e8..900b79d5d1789fc7e5757166b920ab85330c67d2 100644 (file)
@@ -98,7 +98,7 @@ export abstract class AbstractUIService {
       };
     } finally {
       // Send response for payload not forwarded to broadcast channel
-      if (responsePayload !== undefined) {
+      if (!Utils.isNullOrUndefined(responsePayload)) {
         this.sendResponse(messageId, responsePayload);
       }
     }
@@ -151,7 +151,7 @@ export abstract class AbstractUIService {
   ): void {
     if (Utils.isNotEmptyArray(payload.hashIds)) {
       payload.hashIds = payload.hashIds
-        .filter((hashId) => hashId !== undefined)
+        .filter((hashId) => !Utils.isNullOrUndefined(hashId))
         .map((hashId) => {
           if (this.uiServer.chargingStations.has(hashId) === true) {
             return hashId;
index 7195ce09ecfff2bfab6ed5c5016b876190830c8b..058d175d0d7ebc3c3776205c6c5c9429570a2358 100644 (file)
@@ -191,7 +191,7 @@ export class Utils {
   }
 
   public static isCFEnvironment(): boolean {
-    return process.env.VCAP_APPLICATION !== undefined;
+    return !Utils.isNullOrUndefined(process.env.VCAP_APPLICATION);
   }
 
   public static isIterable<T>(obj: T): boolean {