From b1340ec1468911ac6d199013e4d382f2975f453c Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Thu, 29 Dec 2022 23:48:03 +0100 Subject: [PATCH] Ensure number conversion helpers do not return NaN MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 16 ++++++++++++---- test/utils/UtilsTest.ts | 12 ++++++------ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index 08e87320..f28de981 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -52,7 +52,9 @@ export default class Utils { return Utils.formatDurationMilliSeconds(duration * 1000); } - public static convertToDate(value: unknown): Date | null | undefined { + public static convertToDate( + value: Date | string | number | null | undefined + ): Date | null | undefined { if (Utils.isNullOrUndefined(value)) { return value as null | undefined; } @@ -60,7 +62,7 @@ export default class Utils { return value; } if (Utils.isString(value) || typeof value === 'number') { - return new Date(value as string | number); + return new Date(value); } return null; } @@ -74,11 +76,14 @@ export default class Utils { return value as number; } if (typeof value === 'number') { - changedValue = Math.trunc(value); + return Math.trunc(value); } if (Utils.isString(value)) { changedValue = parseInt(value as string); } + if (isNaN(changedValue)) { + throw new Error(`Cannot convert to integer: ${value.toString()}`); + } return changedValue; } @@ -90,6 +95,9 @@ export default class Utils { if (Utils.isString(value)) { changedValue = parseFloat(value as string); } + if (isNaN(changedValue)) { + throw new Error(`Cannot convert to float: ${value.toString()}`); + } return changedValue; } @@ -98,7 +106,7 @@ export default class Utils { if (value) { // Check the type if (typeof value === 'boolean') { - result = value; + return value; } else if ( Utils.isString(value) && ((value as string).toLowerCase() === 'true' || value === '1') diff --git a/test/utils/UtilsTest.ts b/test/utils/UtilsTest.ts index b621f88a..45ba4eb0 100644 --- a/test/utils/UtilsTest.ts +++ b/test/utils/UtilsTest.ts @@ -28,12 +28,6 @@ describe('Utils test suite', () => { const invalidDate = Utils.convertToDate(''); expect(invalidDate instanceof Date && !isNaN(invalidDate.getTime())).toBe(false); expect(Utils.convertToDate(0)).toStrictEqual(new Date('1970-01-01T00:00:00.000Z')); - expect(Utils.convertToDate([])).toBe(null); - expect(Utils.convertToDate({})).toBe(null); - expect(Utils.convertToDate(new Map())).toBe(null); - expect(Utils.convertToDate(new Set())).toBe(null); - expect(Utils.convertToDate(new WeakMap())).toBe(null); - expect(Utils.convertToDate(new WeakSet())).toBe(null); const dateStr = '2020-01-01T00:00:00.000Z'; let date = Utils.convertToDate(dateStr); expect(date).toBeInstanceOf(Date); @@ -59,6 +53,9 @@ describe('Utils test suite', () => { expect(Utils.convertToInt(1.1)).toBe(1); expect(Utils.convertToInt(1.9)).toBe(1); expect(Utils.convertToInt(1.999)).toBe(1); + expect(() => { + Utils.convertToInt('NaN'); + }).toThrow('Cannot convert to integer: NaN'); }); it('Verify convertToFloat()', () => { @@ -77,6 +74,9 @@ describe('Utils test suite', () => { expect(Utils.convertToFloat(1.1)).toBe(1.1); expect(Utils.convertToFloat(1.9)).toBe(1.9); expect(Utils.convertToFloat(1.999)).toBe(1.999); + expect(() => { + Utils.convertToFloat('NaN'); + }).toThrow('Cannot convert to float: NaN'); }); it('Verify convertToBoolean()', () => { -- 2.34.1