Renamed nullable utils into isDefined and isUndefinedOrNull (#4402)

* Renamed nullable utils into isDefined and isUndefinedOrNull
This commit is contained in:
Lucas Bordeau
2024-03-11 14:28:57 +01:00
committed by GitHub
parent 3f15cc5b7a
commit 581dfafe11
169 changed files with 469 additions and 493 deletions

View File

@ -0,0 +1,15 @@
import { isDefined } from '~/utils/isDefined';
describe('isDefined', () => {
it('returns true if value is not undefined nor null', () => {
expect(isDefined('')).toBe(true);
});
it('returns false if value is null', () => {
expect(isDefined(null)).toBe(false);
});
it('returns false if value is undefined', () => {
expect(isDefined(undefined)).toBe(false);
});
});

View File

@ -1,15 +0,0 @@
import { isNonNullable } from '~/utils/isNonNullable';
describe('isNonNullable', () => {
it('returns true if value is not undefined nor null', () => {
expect(isNonNullable('')).toBe(true);
});
it('returns false if value is null', () => {
expect(isNonNullable(null)).toBe(false);
});
it('returns false if value is undefined', () => {
expect(isNonNullable(undefined)).toBe(false);
});
});

View File

@ -1,6 +1,6 @@
import { LinkType } from '@/ui/navigation/link/components/SocialLink';
import { isNonNullable } from './isNonNullable';
import { isDefined } from './isDefined';
export const checkUrlType = (url: string) => {
if (
@ -11,9 +11,7 @@ export const checkUrlType = (url: string) => {
return LinkType.LinkedIn;
}
if (
isNonNullable(
/^((http|https):\/\/)?(?:www\.)?twitter\.com\/(\w+)?/i.exec(url),
)
isDefined(/^((http|https):\/\/)?(?:www\.)?twitter\.com\/(\w+)?/i.exec(url))
) {
return LinkType.Twitter;
}

View File

@ -1,6 +1,6 @@
import { LinkType } from '@/ui/navigation/link/components/SocialLink';
import { isNonNullable } from './isNonNullable';
import { isDefined } from './isDefined';
type getUrlDisplayValueByUrlTypeProps = {
type: LinkType;
@ -15,7 +15,7 @@ export const getDisplayValueByUrlType = ({
const matches = href.match(
/(?:https?:\/\/)?(?:www.)?linkedin.com\/(?:in|company|school)\/(.*)/,
);
if (isNonNullable(matches?.[1])) {
if (isDefined(matches?.[1])) {
return matches?.[1];
} else {
return 'LinkedIn';
@ -26,7 +26,7 @@ export const getDisplayValueByUrlType = ({
const matches = href.match(
/(?:https?:\/\/)?(?:www.)?twitter.com\/([-a-zA-Z0-9@:%_+.~#?&//=]*)/,
);
if (isNonNullable(matches?.[1])) {
if (isDefined(matches?.[1])) {
return `@${matches?.[1]}`;
} else {
return '@twitter';

View File

@ -1,7 +1,7 @@
import { isNonNullable } from './isNonNullable';
import { isDefined } from './isDefined';
export const isDomain = (url: string | undefined | null) =>
isNonNullable(url) &&
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,7 +1,7 @@
import { isNonNullable } from './isNonNullable';
import { isDefined } from './isDefined';
export const isURL = (url: string | undefined | null) =>
isNonNullable(url) &&
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,4 +1,4 @@
import { isNull, isUndefined } from '@sniptt/guards';
export const isNonNullable = <T>(value: T): value is NonNullable<T> =>
export const isDefined = <T>(value: T): value is NonNullable<T> =>
!isUndefined(value) && !isNull(value);

View File

@ -1,4 +1,4 @@
import { isNull, isUndefined } from '@sniptt/guards';
export const isNullable = (value: any): value is null | undefined =>
export const isUndefinedOrNull = (value: any): value is null | undefined =>
isUndefined(value) || isNull(value);

View File

@ -2,7 +2,7 @@ import { AtomEffect } from 'recoil';
import { cookieStorage } from '~/utils/cookie-storage';
import { isNonNullable } from './isNonNullable';
import { isDefined } from './isDefined';
export const localStorageEffect =
<T>(key: string): AtomEffect<T> =>
@ -24,8 +24,8 @@ export const cookieStorageEffect =
({ setSelf, onSet }) => {
const savedValue = cookieStorage.getItem(key);
if (
isNonNullable(savedValue) &&
isNonNullable(JSON.parse(savedValue)['accessToken'])
isDefined(savedValue) &&
isDefined(JSON.parse(savedValue)['accessToken'])
) {
setSelf(JSON.parse(savedValue));
}