From d760a0a625ddec9919e7ad691ddcd60ded1452b0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 11 Feb 2024 15:05:24 +0100 Subject: [PATCH] refactor: convert to nullish condition check where appropriate MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 9 +++--- src/charging-station/Helpers.ts | 40 ++++++++++++------------- src/utils/Configuration.ts | 20 +++++-------- 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 9da4fdbb..6b83b136 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -1038,15 +1038,14 @@ export class ChargingStation extends EventEmitter { connectorId?: number ): boolean { const reservation = this.getReservationBy('reservationId', reservationId) - const reservationExists = reservation !== undefined && !hasReservationExpired(reservation) + const reservationExists = reservation != null && !hasReservationExpired(reservation) if (arguments.length === 1) { return !reservationExists } else if (arguments.length > 1) { - const userReservation = - idTag !== undefined ? this.getReservationBy('idTag', idTag) : undefined + const userReservation = idTag != null ? this.getReservationBy('idTag', idTag) : undefined const userReservationExists = - userReservation !== undefined && !hasReservationExpired(userReservation) - const notConnectorZero = connectorId === undefined ? true : connectorId > 0 + userReservation != null && !hasReservationExpired(userReservation) + const notConnectorZero = connectorId == null ? true : connectorId > 0 const freeConnectorsAvailable = this.getNumberOfReservableConnectors() > 0 return ( !reservationExists && !userReservationExists && notConnectorZero && freeConnectorsAvailable diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 8870ce86..f69d86f8 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -146,16 +146,16 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat const chargingStationInfo = { chargePointModel: stationTemplate.chargePointModel, chargePointVendor: stationTemplate.chargePointVendor, - ...(stationTemplate.chargeBoxSerialNumberPrefix !== undefined && { + ...(stationTemplate.chargeBoxSerialNumberPrefix != null && { chargeBoxSerialNumber: stationTemplate.chargeBoxSerialNumberPrefix }), - ...(stationTemplate.chargePointSerialNumberPrefix !== undefined && { + ...(stationTemplate.chargePointSerialNumberPrefix != null && { chargePointSerialNumber: stationTemplate.chargePointSerialNumberPrefix }), - ...(stationTemplate.meterSerialNumberPrefix !== undefined && { + ...(stationTemplate.meterSerialNumberPrefix != null && { meterSerialNumber: stationTemplate.meterSerialNumberPrefix }), - ...(stationTemplate.meterType !== undefined && { + ...(stationTemplate.meterType != null && { meterType: stationTemplate.meterType }) } @@ -418,21 +418,21 @@ export const createBootNotificationRequest = ( return { chargePointModel: stationInfo.chargePointModel, chargePointVendor: stationInfo.chargePointVendor, - ...(stationInfo.chargeBoxSerialNumber !== undefined && { + ...(stationInfo.chargeBoxSerialNumber != null && { chargeBoxSerialNumber: stationInfo.chargeBoxSerialNumber }), - ...(stationInfo.chargePointSerialNumber !== undefined && { + ...(stationInfo.chargePointSerialNumber != null && { chargePointSerialNumber: stationInfo.chargePointSerialNumber }), - ...(stationInfo.firmwareVersion !== undefined && { + ...(stationInfo.firmwareVersion != null && { firmwareVersion: stationInfo.firmwareVersion }), - ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }), - ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi }), - ...(stationInfo.meterSerialNumber !== undefined && { + ...(stationInfo.iccid != null && { iccid: stationInfo.iccid }), + ...(stationInfo.imsi != null && { imsi: stationInfo.imsi }), + ...(stationInfo.meterSerialNumber != null && { meterSerialNumber: stationInfo.meterSerialNumber }), - ...(stationInfo.meterType !== undefined && { + ...(stationInfo.meterType != null && { meterType: stationInfo.meterType }) } satisfies OCPP16BootNotificationRequest @@ -443,16 +443,16 @@ export const createBootNotificationRequest = ( chargingStation: { model: stationInfo.chargePointModel, vendorName: stationInfo.chargePointVendor, - ...(stationInfo.firmwareVersion !== undefined && { + ...(stationInfo.firmwareVersion != null && { firmwareVersion: stationInfo.firmwareVersion }), - ...(stationInfo.chargeBoxSerialNumber !== undefined && { + ...(stationInfo.chargeBoxSerialNumber != null && { serialNumber: stationInfo.chargeBoxSerialNumber }), - ...((stationInfo.iccid !== undefined || stationInfo.imsi !== undefined) && { + ...((stationInfo.iccid != null || stationInfo.imsi != null) && { modem: { - ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }), - ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi }) + ...(stationInfo.iccid != null && { iccid: stationInfo.iccid }), + ...(stationInfo.imsi != null && { imsi: stationInfo.imsi }) } }) } @@ -477,7 +477,7 @@ export const warnTemplateKeysDeprecation = ( templateKey.deprecatedKey, logPrefix, templateFile, - templateKey.key !== undefined ? `Use '${templateKey.key}' instead` : undefined + templateKey.key != null ? `Use '${templateKey.key}' instead` : undefined ) convertDeprecatedTemplateKey(stationTemplate, templateKey.deprecatedKey, templateKey.key) } @@ -770,7 +770,7 @@ const warnDeprecatedTemplateKey = ( templateFile: string, logMsgToAppend = '' ): void => { - if (template[key as keyof ChargingStationTemplate] !== undefined) { + if (template[key as keyof ChargingStationTemplate] != null) { const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' }` @@ -784,8 +784,8 @@ const convertDeprecatedTemplateKey = ( deprecatedKey: string, key?: string ): void => { - if (template[deprecatedKey as keyof ChargingStationTemplate] !== undefined) { - if (key !== undefined) { + if (template[deprecatedKey as keyof ChargingStationTemplate] != null) { + if (key != null) { (template as unknown as Record)[key] = template[deprecatedKey as keyof ChargingStationTemplate] } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 19afc586..1cec80f0 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -90,8 +90,7 @@ export class Configuration { public static getSupervisionUrls (): string | string[] | undefined { if ( - Configuration.getConfigurationData()?.['supervisionURLs' as keyof ConfigurationData] !== - undefined + Configuration.getConfigurationData()?.['supervisionURLs' as keyof ConfigurationData] != null ) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion Configuration.getConfigurationData()!.supervisionUrls = Configuration.getConfigurationData()![ @@ -337,8 +336,8 @@ export class Configuration { undefined, "Use 'stationTemplateUrls' instead" ) - Configuration.getConfigurationData()?.['stationTemplateURLs' as keyof ConfigurationData] !== - undefined && + Configuration.getConfigurationData()?.['stationTemplateURLs' as keyof ConfigurationData] != + null && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion (Configuration.getConfigurationData()!.stationTemplateUrls = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -348,7 +347,7 @@ export class Configuration { Configuration.getConfigurationData()?.stationTemplateUrls.forEach( (stationTemplateUrl: StationTemplateUrl) => { // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - if (stationTemplateUrl['numberOfStation' as keyof StationTemplateUrl] !== undefined) { + if (stationTemplateUrl['numberOfStation' as keyof StationTemplateUrl] != null) { console.error( `${chalk.green(logPrefix())} ${chalk.red( `Deprecated configuration key 'numberOfStation' usage for template file '${stationTemplateUrl.file}' in 'stationTemplateUrls'. Use 'numberOfStations' instead` @@ -509,14 +508,13 @@ export class Configuration { ): void { if ( sectionName != null && - Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] !== - undefined && + Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] != null && ( Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] as Record< string, unknown > - )[key] !== undefined + )[key] != null ) { console.error( `${chalk.green(logPrefix())} ${chalk.red( @@ -525,9 +523,7 @@ export class Configuration { }` )}` ) - } else if ( - Configuration.getConfigurationData()?.[key as keyof ConfigurationData] !== undefined - ) { + } else if (Configuration.getConfigurationData()?.[key as keyof ConfigurationData] != null) { console.error( `${chalk.green(logPrefix())} ${chalk.red( `Deprecated configuration key '${key}' usage${ @@ -577,7 +573,7 @@ export class Configuration { ) delete Configuration.configurationData Configuration.configurationSectionCache.clear() - if (Configuration.configurationChangeCallback !== undefined) { + if (Configuration.configurationChangeCallback != null) { Configuration.configurationChangeCallback() .catch(error => { throw typeof error === 'string' ? new Error(error) : error -- 2.34.1