fix: allow zero value on number field (#1436)
* fix: allow zero value on number field * fix: test
This commit is contained in:
@ -16,12 +16,12 @@ describe('canBeCastAsPositiveIntegerOrNull', () => {
|
|||||||
expect(canBeCastAsPositiveIntegerOrNull(-9)).toBeFalsy();
|
expect(canBeCastAsPositiveIntegerOrNull(-9)).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return false if zero`, () => {
|
it(`should return true if zero`, () => {
|
||||||
expect(canBeCastAsPositiveIntegerOrNull(0)).toBeFalsy();
|
expect(canBeCastAsPositiveIntegerOrNull(0)).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return false if string 0`, () => {
|
it(`should return true if string 0`, () => {
|
||||||
expect(canBeCastAsPositiveIntegerOrNull('0')).toBeFalsy();
|
expect(canBeCastAsPositiveIntegerOrNull('0')).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should return false if negative float`, () => {
|
it(`should return false if negative float`, () => {
|
||||||
@ -82,12 +82,12 @@ describe('castAsPositiveIntegerOrNull', () => {
|
|||||||
expect(castAsPositiveIntegerOrNull('9')).toBe(9);
|
expect(castAsPositiveIntegerOrNull('9')).toBe(9);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should throw if trying to cast a 0 to positive integer`, () => {
|
it(`should cast an integer to zero integer`, () => {
|
||||||
expect(() => castAsPositiveIntegerOrNull(0)).toThrow(Error);
|
expect(castAsPositiveIntegerOrNull(0)).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should throw if trying to cast a string 0 to positive integer`, () => {
|
it(`should cast an integer string to zero integer`, () => {
|
||||||
expect(() => castAsPositiveIntegerOrNull('0')).toThrow(Error);
|
expect(castAsPositiveIntegerOrNull('0')).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`should throw if trying to cast a positive float string to positive integer`, () => {
|
it(`should throw if trying to cast a positive float string to positive integer`, () => {
|
||||||
|
|||||||
@ -8,7 +8,7 @@ export function canBeCastAsPositiveIntegerOrNull(
|
|||||||
if (typeof probablePositiveNumberOrNull === 'number') {
|
if (typeof probablePositiveNumberOrNull === 'number') {
|
||||||
return (
|
return (
|
||||||
Number.isInteger(probablePositiveNumberOrNull) &&
|
Number.isInteger(probablePositiveNumberOrNull) &&
|
||||||
Math.sign(probablePositiveNumberOrNull) === 1
|
Math.sign(probablePositiveNumberOrNull) !== -1
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ export function canBeCastAsPositiveIntegerOrNull(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Number.isInteger(stringAsNumber) && Math.sign(stringAsNumber) === 1) {
|
if (Number.isInteger(stringAsNumber) && Math.sign(stringAsNumber) !== -1) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user