Change to using arrow functions (#1603)

* Change to using arrow functions

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Matheus <matheus_benini@hotmail.com>

* Add lint rule

---------

Co-authored-by: v1b3m <vibenjamin6@gmail.com>
Co-authored-by: Matheus <matheus_benini@hotmail.com>
Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
gitstart-twenty
2023-09-16 02:41:10 +01:00
committed by GitHub
parent 549335054a
commit 00a3c8ca2b
575 changed files with 2848 additions and 3063 deletions

View File

@ -1,3 +1,2 @@
export function assertNotNull<T>(item: T): item is NonNullable<T> {
return item !== null && item !== undefined;
}
export const assertNotNull = <T>(item: T): item is NonNullable<T> =>
item !== null && item !== undefined;

View File

@ -1,8 +1,8 @@
const DEBUG_MODE = false;
export function canBeCastAsIntegerOrNull(
export const canBeCastAsIntegerOrNull = (
probableNumberOrNull: string | undefined | number | null,
): probableNumberOrNull is number | null {
): probableNumberOrNull is number | null => {
if (probableNumberOrNull === undefined) {
if (DEBUG_MODE) console.log('probableNumberOrNull === undefined');
@ -43,11 +43,11 @@ export function canBeCastAsIntegerOrNull(
}
return false;
}
};
export function castAsIntegerOrNull(
export const castAsIntegerOrNull = (
probableNumberOrNull: string | undefined | number | null,
): number | null {
): number | null => {
if (canBeCastAsIntegerOrNull(probableNumberOrNull) === false) {
throw new Error('Cannot cast to number or null');
}
@ -69,4 +69,4 @@ export function castAsIntegerOrNull(
}
return null;
}
};

View File

@ -1,6 +1,6 @@
export function canBeCastAsPositiveIntegerOrNull(
export const canBeCastAsPositiveIntegerOrNull = (
probablePositiveNumberOrNull: string | undefined | number | null,
): probablePositiveNumberOrNull is number | null {
): probablePositiveNumberOrNull is number | null => {
if (probablePositiveNumberOrNull === undefined) {
return false;
}
@ -33,11 +33,11 @@ export function canBeCastAsPositiveIntegerOrNull(
}
return false;
}
};
export function castAsPositiveIntegerOrNull(
export const castAsPositiveIntegerOrNull = (
probablePositiveNumberOrNull: string | undefined | number | null,
): number | null {
): number | null => {
if (
canBeCastAsPositiveIntegerOrNull(probablePositiveNumberOrNull) === false
) {
@ -61,4 +61,4 @@ export function castAsPositiveIntegerOrNull(
}
return null;
}
};

View File

@ -5,7 +5,7 @@ import { logError } from './logError';
export const DEFAULT_DATE_LOCALE = 'en-EN';
export function parseDate(dateToParse: Date | string | number) {
export const parseDate = (dateToParse: Date | string | number) => {
let formattedDate: DateTime | null = null;
if (!dateToParse) {
@ -27,13 +27,12 @@ export function parseDate(dateToParse: Date | string | number) {
}
return formattedDate.setLocale(DEFAULT_DATE_LOCALE);
}
};
function isSameDay(a: DateTime, b: DateTime): boolean {
return a.hasSame(b, 'day') && a.hasSame(b, 'month') && a.hasSame(b, 'year');
}
const isSameDay = (a: DateTime, b: DateTime): boolean =>
a.hasSame(b, 'day') && a.hasSame(b, 'month') && a.hasSame(b, 'year');
function formatDate(dateToFormat: Date | string | number, format: string) {
const formatDate = (dateToFormat: Date | string | number, format: string) => {
try {
const parsedDate = parseDate(dateToFormat);
return parsedDate.toFormat(format);
@ -41,23 +40,25 @@ function formatDate(dateToFormat: Date | string | number, format: string) {
logError(error);
return '';
}
}
};
export function beautifyExactDateTime(dateToBeautify: Date | string | number) {
export const beautifyExactDateTime = (
dateToBeautify: Date | string | number,
) => {
const isToday = isSameDay(parseDate(dateToBeautify), DateTime.local());
const dateFormat = isToday ? 'T' : 'DD · T';
return formatDate(dateToBeautify, dateFormat);
}
};
export function beautifyExactDate(dateToBeautify: Date | string | number) {
export const beautifyExactDate = (dateToBeautify: Date | string | number) => {
const isToday = isSameDay(parseDate(dateToBeautify), DateTime.local());
const dateFormat = isToday ? "'Today'" : 'DD';
return formatDate(dateToBeautify, dateFormat);
}
};
export function beautifyPastDateRelativeToNow(
export const beautifyPastDateRelativeToNow = (
pastDate: Date | string | number,
) {
) => {
try {
const parsedDate = parseDate(pastDate);
@ -68,9 +69,9 @@ export function beautifyPastDateRelativeToNow(
logError(error);
return '';
}
}
};
export function beautifyPastDateAbsolute(pastDate: Date | string | number) {
export const beautifyPastDateAbsolute = (pastDate: Date | string | number) => {
try {
const parsedPastDate = parseDate(pastDate);
@ -89,9 +90,9 @@ export function beautifyPastDateAbsolute(pastDate: Date | string | number) {
logError(error);
return '';
}
}
};
export function hasDatePassed(date: Date | string | number) {
export const hasDatePassed = (date: Date | string | number) => {
try {
const parsedDate = parseDate(date);
@ -105,4 +106,4 @@ export function hasDatePassed(date: Date | string | number) {
logError(error);
return false;
}
}
};

View File

@ -1,3 +1 @@
export function formatNumber(value: number): string {
return value.toLocaleString();
}
export const formatNumber = (value: number): string => value.toLocaleString();

View File

@ -1,6 +1,6 @@
import { parseDate } from './date-utils';
export function formatToHumanReadableDate(date: Date | string) {
export const formatToHumanReadableDate = (date: Date | string) => {
const parsedJSDate = parseDate(date).toJSDate();
return new Intl.DateTimeFormat(undefined, {
@ -8,17 +8,17 @@ export function formatToHumanReadableDate(date: Date | string) {
day: 'numeric',
year: 'numeric',
}).format(parsedJSDate);
}
};
export function sanitizeURL(link: string | null | undefined) {
export const sanitizeURL = (link: string | null | undefined) => {
return link
? link.replace(/(https?:\/\/)|(www\.)/g, '').replace(/\/$/, '')
: '';
}
};
export function getLogoUrlFromDomainName(
export const getLogoUrlFromDomainName = (
domainName?: string,
): string | undefined {
): string | undefined => {
const sanitizedDomain = sanitizeURL(domainName);
return `https://favicon.twenty.com/${sanitizedDomain}`;
}
};

View File

@ -1,10 +1,7 @@
import { isDefined } from './isDefined';
export function isDomain(url: string | undefined | null) {
return (
isDefined(url) &&
/^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/.test(
url,
)
export const isDomain = (url: string | undefined | null) =>
isDefined(url) &&
/^((?!-))(xn--)?[a-z0-9][a-z0-9-_]{0,61}[a-z0-9]{0,1}\.(xn--)?([a-z0-9-]{1,61}|[a-z0-9-]{1,30}\.[a-z]{2,})$/.test(
url,
);
}

View File

@ -1,10 +1,7 @@
import { isDefined } from './isDefined';
export function isURL(url: string | undefined | null) {
return (
isDefined(url) &&
url.match(
/^(https?:\/\/)?(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,63}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/i,
)
export const isURL = (url: string | undefined | null) =>
isDefined(url) &&
url.match(
/^(https?:\/\/)?(www.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-z]{2,63}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/i,
);
}

View File

@ -1,5 +1,3 @@
import deepEqual from 'deep-equal';
export function isDeeplyEqual<T>(a: T, b: T) {
return deepEqual(a, b);
}
export const isDeeplyEqual = <T>(a: T, b: T) => deepEqual(a, b);

View File

@ -1,5 +1,3 @@
export function isDefined<T>(
export const isDefined = <T>(
value: T | undefined | null,
): value is NonNullable<T> {
return value !== undefined && value !== null;
}
): value is NonNullable<T> => value !== undefined && value !== null;

View File

@ -1,6 +1,6 @@
export function isNonEmptyArray<T>(
export const isNonEmptyArray = <T>(
probableArray: T[] | undefined | null,
): probableArray is NonNullable<T[]> {
): probableArray is NonNullable<T[]> => {
if (
Array.isArray(probableArray) &&
probableArray.length &&
@ -10,4 +10,4 @@ export function isNonEmptyArray<T>(
}
return false;
}
};

View File

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

View File

@ -1,3 +1,3 @@
export function logError(message: any) {
export const logError = (message: any) => {
console.error(message);
}
};

View File

@ -1,6 +1,6 @@
import afterFrame from 'afterframe';
export function measureTotalFrameLoad(id: string) {
export const measureTotalFrameLoad = (id: string) => {
const timerId = `Total loading time for : ${id}`;
console.time(timerId);
@ -8,4 +8,4 @@ export function measureTotalFrameLoad(id: string) {
afterFrame(() => {
console.timeEnd(timerId);
});
}
};

View File

@ -1,8 +1,8 @@
export function stringToHslColor(
export const stringToHslColor = (
str: string,
saturation: number,
lightness: number,
) {
) => {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
@ -10,4 +10,4 @@ export function stringToHslColor(
const h = hash % 360;
return 'hsl(' + h + ', ' + saturation + '%, ' + lightness + '%)';
}
};

View File

@ -2,7 +2,7 @@ import { AppBasePath } from '@/types/AppBasePath';
import { AppPath } from '@/types/AppPath';
import { SettingsPath } from '@/types/SettingsPath';
export function getPageTitleFromPath(pathname: string): string {
export const getPageTitleFromPath = (pathname: string): string => {
switch (pathname) {
case AppPath.Verify:
return 'Verify';
@ -35,4 +35,4 @@ export function getPageTitleFromPath(pathname: string): string {
default:
return 'Twenty';
}
}
};