From: Jérôme Benoit Date: Fri, 23 Aug 2024 18:00:09 +0000 (+0200) Subject: refactor: refine isNotEmptyString() and isNotEmptyArray() helpers type X-Git-Tag: ocpp-server@v1.5.2~95 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=665864cacd27d66cd323b6f66b2724f6e9e244e8;p=e-mobility-charging-stations-simulator.git refactor: refine isNotEmptyString() and isNotEmptyArray() helpers type Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 83e5517b..87dfc509 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -388,7 +388,7 @@ export class ChargingStation extends EventEmitter { } private getConfiguredSupervisionUrl (): URL { - let configuredSupervisionUrl: string + let configuredSupervisionUrl: string | undefined const supervisionUrls = this.stationInfo?.supervisionUrls ?? Configuration.getSupervisionUrls() if (isNotEmptyArray(supervisionUrls)) { let configuredSupervisionUrlIndex: number @@ -413,7 +413,7 @@ export class ChargingStation extends EventEmitter { break } configuredSupervisionUrl = supervisionUrls[configuredSupervisionUrlIndex] - } else { + } else if (typeof supervisionUrls === 'string') { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion configuredSupervisionUrl = supervisionUrls! } @@ -603,17 +603,16 @@ export class ChargingStation extends EventEmitter { stationInfo.chargingStationId = getChargingStationId(this.index, stationTemplate) createSerialNumber(stationTemplate, stationInfo) stationInfo.voltageOut = this.getVoltageOut(stationInfo) - if (isNotEmptyArray(stationTemplate.power)) { + if (isNotEmptyArray(stationTemplate.power)) { const powerArrayRandomIndex = Math.floor(secureRandom() * stationTemplate.power.length) stationInfo.maximumPower = stationTemplate.powerUnit === PowerUnits.KILO_WATT ? stationTemplate.power[powerArrayRandomIndex] * 1000 : stationTemplate.power[powerArrayRandomIndex] - } else { + } else if (typeof stationTemplate.power === 'number') { stationInfo.maximumPower = stationTemplate.powerUnit === PowerUnits.KILO_WATT - ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - stationTemplate.power! * 1000 + ? stationTemplate.power * 1000 : stationTemplate.power } stationInfo.maximumAmperage = this.getMaximumAmperage(stationInfo) diff --git a/src/charging-station/Helpers.ts b/src/charging-station/Helpers.ts index 240a54a4..112fb904 100644 --- a/src/charging-station/Helpers.ts +++ b/src/charging-station/Helpers.ts @@ -869,11 +869,11 @@ export const waitChargingStationEvents = async ( const getConfiguredMaxNumberOfConnectors = (stationTemplate: ChargingStationTemplate): number => { let configuredMaxNumberOfConnectors = 0 - if (isNotEmptyArray(stationTemplate.numberOfConnectors)) { + if (isNotEmptyArray(stationTemplate.numberOfConnectors)) { const numberOfConnectors = stationTemplate.numberOfConnectors configuredMaxNumberOfConnectors = numberOfConnectors[Math.floor(secureRandom() * numberOfConnectors.length)] - } else if (stationTemplate.numberOfConnectors != null) { + } else if (typeof stationTemplate.numberOfConnectors === 'number') { configuredMaxNumberOfConnectors = stationTemplate.numberOfConnectors } else if (stationTemplate.Connectors != null && stationTemplate.Evses == null) { configuredMaxNumberOfConnectors = @@ -1031,7 +1031,7 @@ const getChargingProfilesLimit = ( start: chargingSchedule.startSchedule, }) ) { - if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { + if (isNotEmptyArray(chargingSchedule.chargingSchedulePeriod)) { const chargingSchedulePeriodCompareFn = ( a: ChargingSchedulePeriod, b: ChargingSchedulePeriod diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 4fa8bbfb..d62b81f9 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1194,7 +1194,7 @@ const getSampledValueTemplate = ( ) { if ( !OCPPConstants.OCPP_MEASURANDS_SUPPORTED.includes( - sampledValueTemplates[index]?.measurand ?? MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER + sampledValueTemplates[index].measurand ?? MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER ) ) { logger.warn( @@ -1202,8 +1202,8 @@ const getSampledValueTemplate = ( ) } else if ( phase != null && - sampledValueTemplates[index]?.phase === phase && - sampledValueTemplates[index]?.measurand === measurand && + sampledValueTemplates[index].phase === phase && + sampledValueTemplates[index].measurand === measurand && getConfigurationKey( chargingStation, StandardParametersKey.MeterValuesSampledData @@ -1212,8 +1212,8 @@ const getSampledValueTemplate = ( return sampledValueTemplates[index] } else if ( phase == null && - sampledValueTemplates[index]?.phase == null && - sampledValueTemplates[index]?.measurand === measurand && + sampledValueTemplates[index].phase == null && + sampledValueTemplates[index].measurand === measurand && getConfigurationKey( chargingStation, StandardParametersKey.MeterValuesSampledData @@ -1222,8 +1222,8 @@ const getSampledValueTemplate = ( return sampledValueTemplates[index] } else if ( measurand === MeterValueMeasurand.ENERGY_ACTIVE_IMPORT_REGISTER && - (sampledValueTemplates[index]?.measurand == null || - sampledValueTemplates[index]?.measurand === measurand) + (sampledValueTemplates[index].measurand == null || + sampledValueTemplates[index].measurand === measurand) ) { return sampledValueTemplates[index] } diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index c816cf71..2da9b1cb 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -13,7 +13,7 @@ import { } from 'date-fns' import { getRandomValues, randomBytes, randomUUID } from 'node:crypto' import { env, nextTick } from 'node:process' -import { is } from 'rambda' +import { is, isNotEmpty, type NonEmptyArray, type ReadonlyNonEmptyArray } from 'rambda' import { type JsonType, @@ -230,12 +230,16 @@ export const isCFEnvironment = (): boolean => { return env.VCAP_APPLICATION != null } -export const isNotEmptyString = (value: unknown): value is string => { +declare const nonEmptyString: unique symbol +type NonEmptyString = { [nonEmptyString]: true } & string +export const isNotEmptyString = (value: unknown): value is NonEmptyString => { return typeof value === 'string' && value.trim().length > 0 } -export const isNotEmptyArray = (value: unknown): value is unknown[] => { - return Array.isArray(value) && value.length > 0 +export const isNotEmptyArray = ( + value: unknown +): value is NonEmptyArray | ReadonlyNonEmptyArray => { + return Array.isArray(value) && isNotEmpty(value as T[]) } export const insertAt = (str: string, subStr: string, pos: number): string =>