Chore(front): Add more typeguards (#2136)

* Chore(front): Add more typeguards

Co-authored-by: Benjamin Mayanja V <vibenjamin6@gmail.com>
Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev>

* Remove source map generation to avoid warnings

---------

Co-authored-by: Benjamin Mayanja V <vibenjamin6@gmail.com>
Co-authored-by: KlingerMatheus <klinger.matheus@gitstart.dev>
Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
gitstart-twenty
2023-10-24 10:26:47 +03:00
committed by GitHub
parent 80d558559f
commit 5acafe2fc6
29 changed files with 82 additions and 87 deletions

View File

@ -1,3 +1,5 @@
import { isNull, isNumber, isString } from '@sniptt/guards';
import { logError } from './logError';
const DEBUG_MODE = false;
@ -11,13 +13,13 @@ export const canBeCastAsIntegerOrNull = (
return false;
}
if (typeof probableNumberOrNull === 'number') {
if (isNumber(probableNumberOrNull)) {
if (DEBUG_MODE) logError('typeof probableNumberOrNull === "number"');
return Number.isInteger(probableNumberOrNull);
}
if (probableNumberOrNull === null) {
if (isNull(probableNumberOrNull)) {
if (DEBUG_MODE) logError('probableNumberOrNull === null');
return true;
@ -29,7 +31,7 @@ export const canBeCastAsIntegerOrNull = (
return true;
}
if (typeof probableNumberOrNull === 'string') {
if (isString(probableNumberOrNull)) {
const stringAsNumber = +probableNumberOrNull;
if (isNaN(stringAsNumber)) {
@ -54,7 +56,7 @@ export const castAsIntegerOrNull = (
throw new Error('Cannot cast to number or null');
}
if (probableNumberOrNull === null) {
if (isNull(probableNumberOrNull)) {
return null;
}
@ -62,11 +64,11 @@ export const castAsIntegerOrNull = (
return null;
}
if (typeof probableNumberOrNull === 'number') {
if (isNumber(probableNumberOrNull)) {
return probableNumberOrNull;
}
if (typeof probableNumberOrNull === 'string') {
if (isString(probableNumberOrNull)) {
return +probableNumberOrNull;
}

View File

@ -1,3 +1,5 @@
import { isInteger, isNull, isNumber, isString } from '@sniptt/guards';
export const canBeCastAsPositiveIntegerOrNull = (
probablePositiveNumberOrNull: string | undefined | number | null,
): probablePositiveNumberOrNull is number | null => {
@ -5,14 +7,14 @@ export const canBeCastAsPositiveIntegerOrNull = (
return false;
}
if (typeof probablePositiveNumberOrNull === 'number') {
if (isNumber(probablePositiveNumberOrNull)) {
return (
Number.isInteger(probablePositiveNumberOrNull) &&
Math.sign(probablePositiveNumberOrNull) !== -1
);
}
if (probablePositiveNumberOrNull === null) {
if (isNull(probablePositiveNumberOrNull)) {
return true;
}
@ -20,14 +22,14 @@ export const canBeCastAsPositiveIntegerOrNull = (
return true;
}
if (typeof probablePositiveNumberOrNull === 'string') {
if (isString(probablePositiveNumberOrNull)) {
const stringAsNumber = +probablePositiveNumberOrNull;
if (isNaN(stringAsNumber)) {
return false;
}
if (Number.isInteger(stringAsNumber) && Math.sign(stringAsNumber) !== -1) {
if (isInteger(stringAsNumber) && Math.sign(stringAsNumber) !== -1) {
return true;
}
}
@ -52,11 +54,11 @@ export const castAsPositiveIntegerOrNull = (
return null;
}
if (typeof probablePositiveNumberOrNull === 'number') {
if (isNumber(probablePositiveNumberOrNull)) {
return probablePositiveNumberOrNull;
}
if (typeof probablePositiveNumberOrNull === 'string') {
if (isString(probablePositiveNumberOrNull)) {
return +probablePositiveNumberOrNull;
}

View File

@ -1,3 +1,4 @@
import { isDate, isNumber, isString } from '@sniptt/guards';
import { differenceInCalendarDays, formatDistanceToNow } from 'date-fns';
import { DateTime } from 'luxon';
@ -10,11 +11,11 @@ export const parseDate = (dateToParse: Date | string | number) => {
if (!dateToParse) {
throw new Error(`Invalid date passed to formatPastDate: "${dateToParse}"`);
} else if (typeof dateToParse === 'string') {
} else if (isString(dateToParse)) {
formattedDate = DateTime.fromISO(dateToParse);
} else if (dateToParse instanceof Date) {
} else if (isDate(dateToParse)) {
formattedDate = DateTime.fromJSDate(dateToParse);
} else if (typeof dateToParse === 'number') {
} else if (isNumber(dateToParse)) {
formattedDate = DateTime.fromMillis(dateToParse);
}

View File

@ -1,15 +0,0 @@
import { isDefined } from './isDefined';
export const isNonEmptyString = (
probableNonEmptyString: string | undefined | null,
): probableNonEmptyString is string => {
if (
isDefined(probableNonEmptyString) &&
typeof probableNonEmptyString === 'string' &&
probableNonEmptyString !== ''
) {
return true;
}
return false;
};