From 85156ce9aebb4ecd1ad37e628da865731f7c3801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20M?= Date: Mon, 4 Sep 2023 17:03:31 +0200 Subject: [PATCH] fix: allow zero value on number field (#1436) * fix: allow zero value on number field * fix: test --- .../cast-as-positive-integer-or-null.test.ts | 16 ++++++++-------- .../utils/cast-as-positive-integer-or-null.ts | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.ts b/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.ts index 1c3e9d10b..b9a88647c 100644 --- a/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.ts +++ b/front/src/utils/__tests__/cast-as-positive-integer-or-null.test.ts @@ -16,12 +16,12 @@ describe('canBeCastAsPositiveIntegerOrNull', () => { expect(canBeCastAsPositiveIntegerOrNull(-9)).toBeFalsy(); }); - it(`should return false if zero`, () => { - expect(canBeCastAsPositiveIntegerOrNull(0)).toBeFalsy(); + it(`should return true if zero`, () => { + expect(canBeCastAsPositiveIntegerOrNull(0)).toBeTruthy(); }); - it(`should return false if string 0`, () => { - expect(canBeCastAsPositiveIntegerOrNull('0')).toBeFalsy(); + it(`should return true if string 0`, () => { + expect(canBeCastAsPositiveIntegerOrNull('0')).toBeTruthy(); }); it(`should return false if negative float`, () => { @@ -82,12 +82,12 @@ describe('castAsPositiveIntegerOrNull', () => { expect(castAsPositiveIntegerOrNull('9')).toBe(9); }); - it(`should throw if trying to cast a 0 to positive integer`, () => { - expect(() => castAsPositiveIntegerOrNull(0)).toThrow(Error); + it(`should cast an integer to zero integer`, () => { + expect(castAsPositiveIntegerOrNull(0)).toBe(0); }); - it(`should throw if trying to cast a string 0 to positive integer`, () => { - expect(() => castAsPositiveIntegerOrNull('0')).toThrow(Error); + it(`should cast an integer string to zero integer`, () => { + expect(castAsPositiveIntegerOrNull('0')).toBe(0); }); it(`should throw if trying to cast a positive float string to positive integer`, () => { diff --git a/front/src/utils/cast-as-positive-integer-or-null.ts b/front/src/utils/cast-as-positive-integer-or-null.ts index 56ad707fe..1751fd4a3 100644 --- a/front/src/utils/cast-as-positive-integer-or-null.ts +++ b/front/src/utils/cast-as-positive-integer-or-null.ts @@ -8,7 +8,7 @@ export function canBeCastAsPositiveIntegerOrNull( if (typeof probablePositiveNumberOrNull === 'number') { return ( Number.isInteger(probablePositiveNumberOrNull) && - Math.sign(probablePositiveNumberOrNull) === 1 + Math.sign(probablePositiveNumberOrNull) !== -1 ); } @@ -27,7 +27,7 @@ export function canBeCastAsPositiveIntegerOrNull( return false; } - if (Number.isInteger(stringAsNumber) && Math.sign(stringAsNumber) === 1) { + if (Number.isInteger(stringAsNumber) && Math.sign(stringAsNumber) !== -1) { return true; } }