fix: allow zero value on number field (#1436)

* fix: allow zero value on number field

* fix: test
This commit is contained in:
Jérémy M
2023-09-04 17:03:31 +02:00
committed by GitHub
parent 8e22ffd021
commit 85156ce9ae
2 changed files with 10 additions and 10 deletions

View File

@ -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`, () => {

View File

@ -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;
}
}