From 300418e9ba42d252a1a7079dab7904f40561a7ef Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 29 Dec 2023 20:44:23 +0100 Subject: [PATCH] refactor: remove isUndefined() helper 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 | 16 ++---- src/charging-station/Helpers.ts | 50 +++++++++---------- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 19 +++---- src/charging-station/ocpp/OCPPServiceUtils.ts | 3 +- src/utils/Configuration.ts | 37 ++++++-------- src/utils/Utils.ts | 9 +--- src/utils/index.ts | 1 - tests/utils/Utils.test.ts | 14 ------ 8 files changed, 56 insertions(+), 93 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index a1bb33a9..281ce247 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -143,7 +143,6 @@ import { handleFileException, isNotEmptyArray, isNotEmptyString, - isUndefined, logPrefix, logger, min, @@ -963,20 +962,15 @@ export class ChargingStation extends EventEmitter { connectorId?: number ): boolean { const reservation = this.getReservationBy('reservationId', reservationId) - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const reservationExists = !isUndefined(reservation) && !hasReservationExpired(reservation!) + const reservationExists = reservation !== undefined && !hasReservationExpired(reservation) if (arguments.length === 1) { return !reservationExists } else if (arguments.length > 1) { - const userReservation = !isUndefined(idTag) - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - this.getReservationBy('idTag', idTag!) - : undefined + const userReservation = + idTag !== undefined ? this.getReservationBy('idTag', idTag) : undefined const userReservationExists = - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - !isUndefined(userReservation) && !hasReservationExpired(userReservation!) - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const notConnectorZero = isUndefined(connectorId) ? true : connectorId! > 0 + userReservation !== undefined && !hasReservationExpired(userReservation) + const notConnectorZero = connectorId === undefined ? 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 38988d05..49ec116e 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -65,7 +65,6 @@ import { isEmptyString, isNotEmptyArray, isNotEmptyString, - isUndefined, isValidTime, logger, secureRandom @@ -146,16 +145,16 @@ export const getHashId = (index: number, stationTemplate: ChargingStationTemplat const chargingStationInfo = { chargePointModel: stationTemplate.chargePointModel, chargePointVendor: stationTemplate.chargePointVendor, - ...(!isUndefined(stationTemplate.chargeBoxSerialNumberPrefix) && { + ...(stationTemplate.chargeBoxSerialNumberPrefix !== undefined && { chargeBoxSerialNumber: stationTemplate.chargeBoxSerialNumberPrefix }), - ...(!isUndefined(stationTemplate.chargePointSerialNumberPrefix) && { + ...(stationTemplate.chargePointSerialNumberPrefix !== undefined && { chargePointSerialNumber: stationTemplate.chargePointSerialNumberPrefix }), - ...(!isUndefined(stationTemplate.meterSerialNumberPrefix) && { + ...(stationTemplate.meterSerialNumberPrefix !== undefined && { meterSerialNumber: stationTemplate.meterSerialNumberPrefix }), - ...(!isUndefined(stationTemplate.meterType) && { + ...(stationTemplate.meterType !== undefined && { meterType: stationTemplate.meterType }) } @@ -356,7 +355,7 @@ export const initializeConnectorsMapStatus = ( if (connectorId === 0) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion connectors.get(connectorId)!.availability = AvailabilityType.Operative - if (isUndefined(connectors.get(connectorId)?.chargingProfiles)) { + if (connectors.get(connectorId)?.chargingProfiles == null) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion connectors.get(connectorId)!.chargingProfiles = [] } @@ -398,21 +397,21 @@ export const createBootNotificationRequest = ( return { chargePointModel: stationInfo.chargePointModel, chargePointVendor: stationInfo.chargePointVendor, - ...(!isUndefined(stationInfo.chargeBoxSerialNumber) && { + ...(stationInfo.chargeBoxSerialNumber !== undefined && { chargeBoxSerialNumber: stationInfo.chargeBoxSerialNumber }), - ...(!isUndefined(stationInfo.chargePointSerialNumber) && { + ...(stationInfo.chargePointSerialNumber !== undefined && { chargePointSerialNumber: stationInfo.chargePointSerialNumber }), - ...(!isUndefined(stationInfo.firmwareVersion) && { + ...(stationInfo.firmwareVersion !== undefined && { firmwareVersion: stationInfo.firmwareVersion }), - ...(!isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }), - ...(!isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }), - ...(!isUndefined(stationInfo.meterSerialNumber) && { + ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }), + ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi }), + ...(stationInfo.meterSerialNumber !== undefined && { meterSerialNumber: stationInfo.meterSerialNumber }), - ...(!isUndefined(stationInfo.meterType) && { + ...(stationInfo.meterType !== undefined && { meterType: stationInfo.meterType }) } satisfies OCPP16BootNotificationRequest @@ -423,16 +422,16 @@ export const createBootNotificationRequest = ( chargingStation: { model: stationInfo.chargePointModel, vendorName: stationInfo.chargePointVendor, - ...(!isUndefined(stationInfo.firmwareVersion) && { + ...(stationInfo.firmwareVersion !== undefined && { firmwareVersion: stationInfo.firmwareVersion }), - ...(!isUndefined(stationInfo.chargeBoxSerialNumber) && { + ...(stationInfo.chargeBoxSerialNumber !== undefined && { serialNumber: stationInfo.chargeBoxSerialNumber }), - ...((!isUndefined(stationInfo.iccid) || !isUndefined(stationInfo.imsi)) && { + ...((stationInfo.iccid !== undefined || stationInfo.imsi !== undefined) && { modem: { - ...(!isUndefined(stationInfo.iccid) && { iccid: stationInfo.iccid }), - ...(!isUndefined(stationInfo.imsi) && { imsi: stationInfo.imsi }) + ...(stationInfo.iccid !== undefined && { iccid: stationInfo.iccid }), + ...(stationInfo.imsi !== undefined && { imsi: stationInfo.imsi }) } }) } @@ -457,7 +456,7 @@ export const warnTemplateKeysDeprecation = ( templateKey.deprecatedKey, logPrefix, templateFile, - !isUndefined(templateKey.key) ? `Use '${templateKey.key}' instead` : undefined + templateKey.key !== undefined ? `Use '${templateKey.key}' instead` : undefined ) convertDeprecatedTemplateKey(stationTemplate, templateKey.deprecatedKey, templateKey.key) } @@ -679,7 +678,7 @@ const getConfiguredMaxNumberOfConnectors = (stationTemplate: ChargingStationTemp const numberOfConnectors = stationTemplate.numberOfConnectors as number[] configuredMaxNumberOfConnectors = numberOfConnectors[Math.floor(secureRandom() * numberOfConnectors.length)] - } else if (!isUndefined(stationTemplate.numberOfConnectors)) { + } else if (stationTemplate.numberOfConnectors != null) { configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors as number } else if (stationTemplate.Connectors != null && stationTemplate.Evses == null) { configuredMaxNumberOfConnectors = @@ -735,7 +734,7 @@ const initializeConnectorStatus = (connectorStatus: ConnectorStatus): void => { connectorStatus.transactionStarted = false connectorStatus.energyActiveImportRegisterValue = 0 connectorStatus.transactionEnergyActiveImportRegisterValue = 0 - if (isUndefined(connectorStatus.chargingProfiles)) { + if (connectorStatus.chargingProfiles == null) { connectorStatus.chargingProfiles = [] } } @@ -747,7 +746,7 @@ const warnDeprecatedTemplateKey = ( templateFile: string, logMsgToAppend = '' ): void => { - if (!isUndefined(template?.[key as keyof ChargingStationTemplate])) { + if (template?.[key as keyof ChargingStationTemplate] !== undefined) { const logMsg = `Deprecated template key '${key}' usage in file '${templateFile}'${ isNotEmptyString(logMsgToAppend) ? `. ${logMsgToAppend}` : '' }` @@ -761,10 +760,9 @@ const convertDeprecatedTemplateKey = ( deprecatedKey: string, key?: string ): void => { - if (!isUndefined(template?.[deprecatedKey as keyof ChargingStationTemplate])) { - if (!isUndefined(key)) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - (template as unknown as Record)[key!] = + if (template?.[deprecatedKey as keyof ChargingStationTemplate] !== undefined) { + if (key !== undefined) { + (template as unknown as Record)[key] = template[deprecatedKey as keyof ChargingStationTemplate] } // eslint-disable-next-line @typescript-eslint/no-dynamic-delete diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index f4198388..2f2ef4c1 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -105,7 +105,6 @@ import { isEmptyArray, isNotEmptyArray, isNotEmptyString, - isUndefined, logger, sleep } from '../../../utils/index.js' @@ -501,13 +500,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { const { key } = commandPayload const configurationKey: OCPPConfigurationKey[] = [] const unknownKey: string[] = [] - if (isUndefined(key)) { + if (key == null) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion for (const configuration of chargingStation.ocppConfiguration!.configurationKey!) { - if (isUndefined(configuration.visible)) { + if (configuration.visible == null) { configuration.visible = true } - if (configuration.visible === false) { + if (!configuration.visible) { continue } configurationKey.push({ @@ -517,14 +516,13 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { }) } } else if (isNotEmptyArray(key)) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - for (const k of key!) { + for (const k of key) { const keyFound = getConfigurationKey(chargingStation, k, true) if (keyFound != null) { - if (isUndefined(keyFound.visible)) { + if (keyFound.visible == null) { keyFound.visible = true } - if (keyFound.visible === false) { + if (!keyFound.visible) { continue } configurationKey.push({ @@ -1657,15 +1655,14 @@ export class OCPP16IncomingRequestService extends OCPPIncomingRequestService { try { const { reservationId } = commandPayload const reservation = chargingStation.getReservationBy('reservationId', reservationId) - if (isUndefined(reservation)) { + if (reservation == null) { logger.debug( `${chargingStation.logPrefix()} Reservation with id ${reservationId} does not exist on charging station` ) return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_REJECTED } await chargingStation.removeReservation( - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - reservation!, + reservation, ReservationTerminationReason.RESERVATION_CANCELED ) return OCPP16Constants.OCPP_CANCEL_RESERVATION_RESPONSE_ACCEPTED diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 045feade..767203f8 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -59,7 +59,6 @@ import { handleFileException, isNotEmptyArray, isNotEmptyString, - isUndefined, logPrefix, logger, max, @@ -1045,7 +1044,7 @@ const checkMeasurandPowerDivider = ( chargingStation: ChargingStation, measurandType: MeterValueMeasurand ): void => { - if (isUndefined(chargingStation.powerDivider)) { + if (chargingStation.powerDivider == null) { const errMsg = `MeterValues measurand ${ measurandType ?? MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER }: powerDivider is undefined` diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index 021bce02..8b0b1c8f 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -15,7 +15,7 @@ import { logPrefix } from './ConfigurationUtils.js' import { Constants } from './Constants.js' -import { hasOwnProp, isCFEnvironment, isUndefined, once } from './Utils.js' +import { hasOwnProp, isCFEnvironment, once } from './Utils.js' import { ApplicationProtocol, type ConfigurationData, @@ -90,9 +90,8 @@ export class Configuration { public static getSupervisionUrls (): string | string[] | undefined { if ( - !isUndefined( - Configuration.getConfigurationData()?.['supervisionURLs' as keyof ConfigurationData] - ) + Configuration.getConfigurationData()?.['supervisionURLs' as keyof ConfigurationData] !== + undefined ) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion Configuration.getConfigurationData()!.supervisionUrls = Configuration.getConfigurationData()![ @@ -318,9 +317,8 @@ export class Configuration { undefined, "Use 'stationTemplateUrls' instead" ) - !isUndefined( - Configuration.getConfigurationData()?.['stationTemplateURLs' as keyof ConfigurationData] - ) && + Configuration.getConfigurationData()?.['stationTemplateURLs' as keyof ConfigurationData] !== + undefined && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion (Configuration.getConfigurationData()!.stationTemplateUrls = // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -329,7 +327,7 @@ export class Configuration { ] as StationTemplateUrl[]) Configuration.getConfigurationData()?.stationTemplateUrls.forEach( (stationTemplateUrl: StationTemplateUrl) => { - if (!isUndefined(stationTemplateUrl?.['numberOfStation' as keyof StationTemplateUrl])) { + if (stationTemplateUrl?.['numberOfStation' as keyof StationTemplateUrl] !== undefined) { console.error( `${chalk.green(logPrefix())} ${chalk.red( `Deprecated configuration key 'numberOfStation' usage for template file '${stationTemplateUrl.file}' in 'stationTemplateUrls'. Use 'numberOfStations' instead` @@ -490,17 +488,14 @@ export class Configuration { ): void { if ( sectionName != null && - !isUndefined( - Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] - ) && - !isUndefined( - ( - Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] as Record< - string, - unknown - > - )?.[key] - ) + Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] !== + undefined && + ( + Configuration.getConfigurationData()?.[sectionName as keyof ConfigurationData] as Record< + string, + unknown + > + )?.[key] !== undefined ) { console.error( `${chalk.green(logPrefix())} ${chalk.red( @@ -510,7 +505,7 @@ export class Configuration { )}` ) } else if ( - !isUndefined(Configuration.getConfigurationData()?.[key as keyof ConfigurationData]) + Configuration.getConfigurationData()?.[key as keyof ConfigurationData] !== undefined ) { console.error( `${chalk.green(logPrefix())} ${chalk.red( @@ -561,7 +556,7 @@ export class Configuration { ) delete Configuration.configurationData Configuration.configurationSectionCache.clear() - if (!isUndefined(Configuration.configurationChangeCallback)) { + if (Configuration.configurationChangeCallback !== undefined) { Configuration.configurationChangeCallback() .catch((error) => { throw typeof error === 'string' ? new Error(error) : error diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index f025d629..42af7c52 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -287,10 +287,6 @@ export const isNotEmptyString = (value: unknown): boolean => { return isString(value) && (value as string).trim().length > 0 } -export const isUndefined = (value: unknown): boolean => { - return value === undefined -} - export const isEmptyArray = (object: unknown): boolean => { return Array.isArray(object) && object.length === 0 } @@ -377,9 +373,8 @@ export const getWebSocketCloseEventStatusString = (code: number): string => { } } if ( - !isUndefined( - WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString] - ) + WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString] !== + undefined ) { return WebSocketCloseEventStatusString[code as keyof typeof WebSocketCloseEventStatusString] } diff --git a/src/utils/index.ts b/src/utils/index.ts index 08cdf6ce..397ea764 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -45,7 +45,6 @@ export { isEmptyString, isNotEmptyArray, isNotEmptyString, - isUndefined, isValidTime, logPrefix, max, diff --git a/tests/utils/Utils.test.ts b/tests/utils/Utils.test.ts index 1c6400ed..f85b8322 100644 --- a/tests/utils/Utils.test.ts +++ b/tests/utils/Utils.test.ts @@ -25,7 +25,6 @@ import { isNotEmptyArray, isNotEmptyString, isObject, - isUndefined, isValidTime, max, min, @@ -354,19 +353,6 @@ await describe('Utils test suite', async () => { expect(isNotEmptyString(new WeakSet())).toBe(false) }) - await it('Verify isUndefined()', () => { - expect(isUndefined(undefined)).toBe(true) - expect(isUndefined(null)).toBe(false) - expect(isUndefined('')).toBe(false) - expect(isUndefined(0)).toBe(false) - expect(isUndefined({})).toBe(false) - expect(isUndefined([])).toBe(false) - expect(isUndefined(new Map())).toBe(false) - expect(isUndefined(new Set())).toBe(false) - expect(isUndefined(new WeakMap())).toBe(false) - expect(isUndefined(new WeakSet())).toBe(false) - }) - await it('Verify isEmptyArray()', () => { expect(isEmptyArray([])).toBe(true) expect(isEmptyArray([1, 2])).toBe(false) -- 2.34.1