From cffc32b7bc5a6569525e92b562af8a93760bc339 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Tue, 21 May 2024 19:18:34 +0200 Subject: [PATCH] refactor: cleanup Infinity usage 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/performance/PerformanceStatistics.ts | 4 ++-- src/utils/StatisticUtils.ts | 4 ++-- src/utils/Utils.ts | 2 +- tests/utils/StatisticUtils.test.ts | 4 ++-- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 080349ea..978bcbd3 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -411,14 +411,17 @@ export class ChargingStation extends EventEmitter { const connectorChargingProfilesPowerLimit = getChargingStationConnectorChargingProfilesPowerLimit(this, connectorId) return min( - isNaN(connectorMaximumPower) ? Infinity : connectorMaximumPower, + isNaN(connectorMaximumPower) ? Number.POSITIVE_INFINITY : connectorMaximumPower, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion isNaN(connectorAmperageLimitationPowerLimit!) - ? Infinity + ? Number.POSITIVE_INFINITY : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion connectorAmperageLimitationPowerLimit!, // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - isNaN(connectorChargingProfilesPowerLimit!) ? Infinity : connectorChargingProfilesPowerLimit! + isNaN(connectorChargingProfilesPowerLimit!) + ? Number.POSITIVE_INFINITY + : // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + connectorChargingProfilesPowerLimit! ) } diff --git a/src/performance/PerformanceStatistics.ts b/src/performance/PerformanceStatistics.ts index 558fb8ca..f05a4182 100644 --- a/src/performance/PerformanceStatistics.ts +++ b/src/performance/PerformanceStatistics.ts @@ -267,12 +267,12 @@ export class PerformanceStatistics { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.minTimeMeasurement = min( entry.duration, - this.statistics.statisticsData.get(entry.name)?.minTimeMeasurement ?? Infinity + this.statistics.statisticsData.get(entry.name)?.minTimeMeasurement ?? Number.POSITIVE_INFINITY ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.maxTimeMeasurement = max( entry.duration, - this.statistics.statisticsData.get(entry.name)?.maxTimeMeasurement ?? -Infinity + this.statistics.statisticsData.get(entry.name)?.maxTimeMeasurement ?? Number.NEGATIVE_INFINITY ) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.statistics.statisticsData.get(entry.name)!.totalTimeMeasurement = diff --git a/src/utils/StatisticUtils.ts b/src/utils/StatisticUtils.ts index 9f50b8ea..501dea77 100644 --- a/src/utils/StatisticUtils.ts +++ b/src/utils/StatisticUtils.ts @@ -1,10 +1,10 @@ import { mean } from 'rambda' export const min = (...args: number[]): number => - args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) + args.reduce((minimum, num) => (minimum < num ? minimum : num), Number.POSITIVE_INFINITY) export const max = (...args: number[]): number => - args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) + args.reduce((maximum, num) => (maximum > num ? maximum : num), Number.NEGATIVE_INFINITY) // TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree export const nthPercentile = (dataSet: number[], percentile: number): number => { diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 2c042217..fe059e06 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -154,7 +154,7 @@ export const getRandomFloat = (max = Number.MAX_VALUE, min = 0): number => { if (max < min) { throw new RangeError('Invalid interval') } - if (max - min === Infinity) { + if (max - min === Number.POSITIVE_INFINITY) { throw new RangeError('Invalid interval') } return (randomBytes(4).readUInt32LE() / 0xffffffff) * (max - min) + min diff --git a/tests/utils/StatisticUtils.test.ts b/tests/utils/StatisticUtils.test.ts index b54cdee8..c0bf1b6c 100644 --- a/tests/utils/StatisticUtils.test.ts +++ b/tests/utils/StatisticUtils.test.ts @@ -6,7 +6,7 @@ import { max, min, nthPercentile, stdDeviation } from '../../src/utils/Statistic await describe('StatisticUtils test suite', async () => { await it('Verify min()', () => { - expect(min()).toBe(Infinity) + expect(min()).toBe(Number.POSITIVE_INFINITY) expect(min(0, 1)).toBe(0) expect(min(1, 0)).toBe(0) expect(min(0, -1)).toBe(-1) @@ -14,7 +14,7 @@ await describe('StatisticUtils test suite', async () => { }) await it('Verify max()', () => { - expect(max()).toBe(-Infinity) + expect(max()).toBe(Number.NEGATIVE_INFINITY) expect(max(0, 1)).toBe(1) expect(max(1, 0)).toBe(1) expect(max(0, -1)).toBe(0) -- 2.34.1