From: Jérôme Benoit Date: Fri, 10 Feb 2023 17:19:06 +0000 (+0100) Subject: fix(simulator): fix empty array detection helper semantic X-Git-Tag: v1.1.94~5 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=53ac516c575adaacd199a68d39b5ace22876ee83;p=e-mobility-charging-stations-simulator.git fix(simulator): fix empty array detection helper semantic Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index afa56c37..62f04891 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -241,7 +241,7 @@ export class Bootstrap { this.numberOfChargingStationTemplates = 0; this.numberOfChargingStations = 0; const stationTemplateUrls = Configuration.getStationTemplateUrls(); - if (!Utils.isEmptyArray(stationTemplateUrls)) { + if (Utils.isNotEmptyArray(stationTemplateUrls)) { this.numberOfChargingStationTemplates = stationTemplateUrls.length; stationTemplateUrls.forEach((stationTemplateUrl) => { this.numberOfChargingStations += stationTemplateUrl.numberOfStations ?? 0; diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 65ea0873..a425d865 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -170,7 +170,7 @@ export default class ChargingStation { }; public hasAuthorizedTags(): boolean { - return !Utils.isEmptyArray( + return Utils.isNotEmptyArray( this.authorizedTagsCache.getAuthorizedTags( ChargingStationUtils.getAuthorizationFile(this.stationInfo) ) @@ -713,7 +713,7 @@ export default class ChargingStation { this.getAutomaticTransactionGeneratorConfigurationFromTemplate(), this ); - if (!Utils.isEmptyArray(connectorIds)) { + if (Utils.isNotEmptyArray(connectorIds)) { for (const connectorId of connectorIds) { this.automaticTransactionGenerator?.startConnector(connectorId); } @@ -724,7 +724,7 @@ export default class ChargingStation { } public stopAutomaticTransactionGenerator(connectorIds?: number[]): void { - if (!Utils.isEmptyArray(connectorIds)) { + if (Utils.isNotEmptyArray(connectorIds)) { for (const connectorId of connectorIds) { this.automaticTransactionGenerator?.stopConnector(connectorId); } @@ -865,7 +865,7 @@ export default class ChargingStation { ); stationInfo.ocppVersion = stationTemplate?.ocppVersion ?? OCPPVersion.VERSION_16; ChargingStationUtils.createSerialNumber(stationTemplate, stationInfo); - if (!Utils.isEmptyArray(stationTemplate?.power)) { + if (Utils.isNotEmptyArray(stationTemplate?.power)) { stationTemplate.power = stationTemplate.power as number[]; const powerArrayRandomIndex = Math.floor(Utils.secureRandom() * stationTemplate.power.length); stationInfo.maximumPower = @@ -1928,7 +1928,7 @@ export default class ChargingStation { private getConfiguredSupervisionUrl(): URL { const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls(); - if (!Utils.isEmptyArray(supervisionUrls)) { + if (Utils.isNotEmptyArray(supervisionUrls)) { switch (Configuration.getSupervisionUrlDistribution()) { case SupervisionUrlDistribution.ROUND_ROBIN: // FIXME diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 2d1adb5d..54e4df72 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -112,7 +112,7 @@ export class ChargingStationUtils { public static getConfiguredNumberOfConnectors(stationTemplate: ChargingStationTemplate): number { let configuredMaxConnectors: number; - if (Utils.isEmptyArray(stationTemplate.numberOfConnectors) === false) { + if (Utils.isNotEmptyArray(stationTemplate.numberOfConnectors) === true) { const numberOfConnectors = stationTemplate.numberOfConnectors as number[]; configuredMaxConnectors = numberOfConnectors[Math.floor(Utils.secureRandom() * numberOfConnectors.length)]; @@ -334,7 +334,7 @@ export class ChargingStationUtils { .chargingProfiles.sort((a, b) => b.stackLevel - a.stackLevel) ); } - if (!Utils.isEmptyArray(chargingProfiles)) { + if (Utils.isNotEmptyArray(chargingProfiles)) { const result = ChargingStationUtils.getLimitFromChargingProfiles( chargingProfiles, chargingStation.logPrefix() diff --git a/src/charging-station/SharedLRUCache.ts b/src/charging-station/SharedLRUCache.ts index 97ed4cd1..3e2e0de0 100644 --- a/src/charging-station/SharedLRUCache.ts +++ b/src/charging-station/SharedLRUCache.ts @@ -113,7 +113,7 @@ export default class SharedLRUCache { Utils.isNullOrUndefined(chargingStationConfiguration?.configurationKey) === false && Utils.isNullOrUndefined(chargingStationConfiguration?.stationInfo) === false && Utils.isNullOrUndefined(chargingStationConfiguration?.configurationHash) === false && - Utils.isEmptyArray(chargingStationConfiguration?.configurationKey) === false && + Utils.isNotEmptyArray(chargingStationConfiguration?.configurationKey) === true && Utils.isEmptyObject(chargingStationConfiguration?.stationInfo) === false && Utils.isNotEmptyString(chargingStationConfiguration?.configurationHash) === true ); diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index 039c7764..7fcb12fb 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -602,7 +602,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer const connectorStatus = chargingStation.getConnectorStatus(commandPayload.connectorId); if ( !Utils.isNullOrUndefined(commandPayload.connectorId) && - !Utils.isEmptyArray(connectorStatus?.chargingProfiles) + Utils.isNotEmptyArray(connectorStatus?.chargingProfiles) ) { connectorStatus.chargingProfiles = []; logger.debug( @@ -616,7 +616,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer let clearedCP = false; for (const connectorId of chargingStation.connectors.keys()) { if ( - !Utils.isEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles) + Utils.isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles) ) { chargingStation .getConnectorStatus(connectorId) diff --git a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts index d0d46c25..eceb2148 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ServiceUtils.ts @@ -769,7 +769,7 @@ export class OCPP16ServiceUtils extends OCPPServiceUtils { chargingStation.getConnectorStatus(connectorId).chargingProfiles = []; } let cpReplaced = false; - if (!Utils.isEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)) { + if (Utils.isNotEmptyArray(chargingStation.getConnectorStatus(connectorId)?.chargingProfiles)) { chargingStation .getConnectorStatus(connectorId) ?.chargingProfiles?.forEach((chargingProfile: OCPP16ChargingProfile, index: number) => { diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 178e4be7..3fced606 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -218,7 +218,7 @@ export class OCPPServiceUtils { chargingStation.getConnectorStatus(connectorId)?.MeterValues; for ( let index = 0; - Utils.isEmptyArray(sampledValueTemplates) === false && index < sampledValueTemplates.length; + Utils.isNotEmptyArray(sampledValueTemplates) === true && index < sampledValueTemplates.length; index++ ) { if ( diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index f8cebbf0..62249549 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -154,7 +154,7 @@ export default abstract class AbstractUIService { procedureName: BroadcastChannelProcedureName, payload: BroadcastChannelRequestPayload ): void { - if (!Utils.isEmptyArray(payload.hashIds)) { + if (Utils.isNotEmptyArray(payload.hashIds)) { payload.hashIds = payload.hashIds .map((hashId) => { if (this.uiServer.chargingStations.has(hashId) === true) { @@ -169,7 +169,7 @@ export default abstract class AbstractUIService { }) .filter((hashId) => hashId !== undefined); } - const expectedNumberOfResponses = !Utils.isEmptyArray(payload.hashIds) + const expectedNumberOfResponses = Utils.isNotEmptyArray(payload.hashIds) ? payload.hashIds.length : this.uiServer.chargingStations.size; this.uiServiceWorkerBroadcastChannel.sendRequest([uuid, procedureName, payload]); diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 1f17fe87..fa1c7b6b 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -214,13 +214,17 @@ export default class Utils { } public static isEmptyArray(object: unknown | unknown[]): boolean { - if (!Array.isArray(object)) { + if (Array.isArray(object) && object.length === 0) { return true; } - if (object.length > 0) { - return false; + return false; + } + + public static isNotEmptyArray(object: unknown | unknown[]): boolean { + if (Array.isArray(object) && object.length > 0) { + return true; } - return true; + return false; } public static isEmptyObject(obj: object): boolean { diff --git a/test/utils/UtilsTest.ts b/test/utils/UtilsTest.ts index 423d61f3..836e41b9 100644 --- a/test/utils/UtilsTest.ts +++ b/test/utils/UtilsTest.ts @@ -229,16 +229,33 @@ describe('Utils test suite', () => { it('Verify isEmptyArray()', () => { expect(Utils.isEmptyArray([])).toBe(true); expect(Utils.isEmptyArray([1, 2])).toBe(false); - expect(Utils.isEmptyArray(undefined)).toBe(true); - expect(Utils.isEmptyArray(null)).toBe(true); - expect(Utils.isEmptyArray('')).toBe(true); - expect(Utils.isEmptyArray('test')).toBe(true); - expect(Utils.isEmptyArray(0)).toBe(true); - expect(Utils.isEmptyArray({})).toBe(true); - expect(Utils.isEmptyArray(new Map())).toBe(true); - expect(Utils.isEmptyArray(new Set())).toBe(true); - expect(Utils.isEmptyArray(new WeakMap())).toBe(true); - expect(Utils.isEmptyArray(new WeakSet())).toBe(true); + expect(Utils.isEmptyArray(['1', '2'])).toBe(false); + expect(Utils.isEmptyArray(undefined)).toBe(false); + expect(Utils.isEmptyArray(null)).toBe(false); + expect(Utils.isEmptyArray('')).toBe(false); + expect(Utils.isEmptyArray('test')).toBe(false); + expect(Utils.isEmptyArray(0)).toBe(false); + expect(Utils.isEmptyArray({})).toBe(false); + expect(Utils.isEmptyArray(new Map())).toBe(false); + expect(Utils.isEmptyArray(new Set())).toBe(false); + expect(Utils.isEmptyArray(new WeakMap())).toBe(false); + expect(Utils.isEmptyArray(new WeakSet())).toBe(false); + }); + + it('Verify isNotEmptyArray()', () => { + expect(Utils.isNotEmptyArray([])).toBe(false); + expect(Utils.isNotEmptyArray([1, 2])).toBe(true); + expect(Utils.isNotEmptyArray(['1', '2'])).toBe(true); + expect(Utils.isNotEmptyArray(undefined)).toBe(false); + expect(Utils.isNotEmptyArray(null)).toBe(false); + expect(Utils.isNotEmptyArray('')).toBe(false); + expect(Utils.isNotEmptyArray('test')).toBe(false); + expect(Utils.isNotEmptyArray(0)).toBe(false); + expect(Utils.isNotEmptyArray({})).toBe(false); + expect(Utils.isNotEmptyArray(new Map())).toBe(false); + expect(Utils.isNotEmptyArray(new Set())).toBe(false); + expect(Utils.isNotEmptyArray(new WeakMap())).toBe(false); + expect(Utils.isNotEmptyArray(new WeakSet())).toBe(false); }); it('Verify isEmptyObject()', () => {