Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: normalizeUri and consolidate utils package πŸ“¦ #402

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions apps/member-profile/app/routes/_profile.directory.join.3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import {
import { Route } from '@/shared/constants';
import { getMember } from '@/shared/queries';
import { ensureUserAuthenticated, user } from '@/shared/session.server';
import { formatUrl } from '@/shared/url.utils';

export async function loader({ request }: LoaderFunctionArgs) {
const session = await ensureUserAuthenticated(request);
Expand All @@ -44,13 +43,11 @@ export async function loader({ request }: LoaderFunctionArgs) {
}

const UpdateSocialsInformation = z.object({
calendlyUrl: nullableField(Student.shape.calendlyUrl).transform(formatUrl),
githubUrl: nullableField(Student.shape.githubUrl).transform(formatUrl),
calendlyUrl: nullableField(Student.shape.calendlyUrl),
githubUrl: nullableField(Student.shape.githubUrl),
instagramHandle: nullableField(Student.shape.instagramHandle),
linkedInUrl: Student.shape.linkedInUrl.transform(formatUrl),
personalWebsiteUrl: nullableField(Student.shape.personalWebsiteUrl).transform(
formatUrl
),
linkedInUrl: Student.shape.linkedInUrl,
personalWebsiteUrl: nullableField(Student.shape.personalWebsiteUrl),
twitterHandle: nullableField(Student.shape.twitterHandle),
});

Expand Down
9 changes: 3 additions & 6 deletions apps/member-profile/app/routes/_profile.profile.socials.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
toast,
user,
} from '@/shared/session.server';
import { formatUrl } from '@/shared/url.utils';

export async function loader({ request }: LoaderFunctionArgs) {
const session = await ensureUserAuthenticated(request);
Expand All @@ -50,12 +49,10 @@ export async function loader({ request }: LoaderFunctionArgs) {
}

const UpdateSocialsInformation = z.object({
calendlyUrl: nullableField(Student.shape.calendlyUrl).transform(formatUrl),
calendlyUrl: nullableField(Student.shape.calendlyUrl),
instagramHandle: nullableField(Student.shape.instagramHandle),
linkedInUrl: Student.shape.linkedInUrl.transform(formatUrl),
personalWebsiteUrl: nullableField(Student.shape.personalWebsiteUrl).transform(
formatUrl
),
linkedInUrl: Student.shape.linkedInUrl,
personalWebsiteUrl: nullableField(Student.shape.personalWebsiteUrl),
twitterHandle: nullableField(Student.shape.twitterHandle),
});

Expand Down
3 changes: 1 addition & 2 deletions apps/member-profile/app/routes/_public.apply._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { apply } from '@/member-profile.server';
import { Application } from '@/member-profile.ui';
import { Route } from '@/shared/constants';
import { commitSession, getSession } from '@/shared/session.server';
import { formatUrl } from '@/shared/url.utils';

export const meta: MetaFunction = () => {
return [{ title: 'ColorStack Family Application' }];
Expand All @@ -38,6 +37,7 @@ const ApplyInput = ApplicationType.pick({
goals: true,
graduationYear: true,
lastName: true,
linkedInUrl: true,
major: true,
otherDemographics: true,
otherMajor: true,
Expand All @@ -46,7 +46,6 @@ const ApplyInput = ApplicationType.pick({
schoolId: true,
}).extend({
codeOfConduct: z.preprocess((value) => value === '1', z.boolean()),
linkedInUrl: ApplicationType.shape.linkedInUrl.transform(formatUrl),
});

type ApplyInput = z.infer<typeof ApplyInput>;
Expand Down
14 changes: 0 additions & 14 deletions apps/member-profile/app/shared/url.utils.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/member-profile/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"isbot": "^3.6.5",
"mixpanel": "^0.18.0",
"node-emoji": "^2.1.3",
"normalize-url": "^8.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-feather": "^2.0.10",
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/domain/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Major,
Race,
} from './types';
import { normalizeUri } from '../../../utils/src/index';
import { type ExtractValue } from '../shared/types';
import { NullishString } from '../shared/zod';

Expand Down Expand Up @@ -38,6 +39,7 @@ const StudentSocialLinks = z.object({
.startsWith('http', 'URL must start with "http://".')
.url()
.transform((value) => value.toLowerCase())
.transform((value) => normalizeUri(value))
.refine((value) => value.includes('calendly.com'), {
message: 'URL must be a valid Calendly URL.',
})
Expand Down Expand Up @@ -68,6 +70,7 @@ const StudentSocialLinks = z.object({
.startsWith('http', 'URL must start with "http://".')
.url()
.transform((value) => value.toLowerCase())
.transform((value) => normalizeUri(value))
.refine((value) => value.includes('linkedin.com/in/'), {
message: 'URL must be a valid LinkedIn URL.',
})
Expand All @@ -79,6 +82,7 @@ const StudentSocialLinks = z.object({
.startsWith('http', 'URL must start with "http".')
.url()
.transform((value) => value.toLowerCase())
.transform((value) => normalizeUri(value))
.nullable(),

twitterHandle: z
Expand Down
27 changes: 0 additions & 27 deletions packages/utils/src/get-cookie.test.ts

This file was deleted.

23 changes: 0 additions & 23 deletions packages/utils/src/get-cookie.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/utils/src/id.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/utils/src/iife.test.ts

This file was deleted.

20 changes: 0 additions & 20 deletions packages/utils/src/iife.ts

This file was deleted.

109 changes: 109 additions & 0 deletions packages/utils/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { getCookie, iife, sleep, toEscapedString, toTitleCase } from '.';

describe(getCookie.name, () => {
test('If the cookie string is empty...', () => {
const result = getCookie('', '');

expect(result).toBeNull();
});

test('If the cookie string does not contain the name of the cookie...', () => {
const result = getCookie('name=Rami;', 'email');

expect(result).toBeNull();
});

test('If the cookie string contains the name of the cookie...', () => {
const result = getCookie('name=Rami;', 'name');

expect(result).toEqual('Rami');
});

test('If the cookie string contains the name of the cookie and other cookies...', () => {
const result = getCookie('name=Rami; [email protected];', 'name');

expect(result).toEqual('Rami');
});
});

describe(iife.name, () => {
test('Should immediately invoke the given function and return its result.', () => {
const result = iife(() => {
return 1 + 1;
});

expect(result).toEqual(2);
});
});

describe(sleep.name, () => {
test('Should sleep for the specified number of milliseconds.', async () => {
const start = Date.now();

await sleep(100);

const elapsed = Date.now() - start;

expect(elapsed).toBeGreaterThanOrEqual(90);
expect(elapsed).toBeLessThanOrEqual(110);
});
});

describe(toEscapedString.name, () => {
test('Should escape special characters.', () => {
expect(toEscapedString('foo.bar')).toBe('foo\\.bar');
expect(toEscapedString('foo[bar]')).toBe('foo\\[bar\\]');
expect(toEscapedString('foo(bar)')).toBe('foo\\(bar\\)');
expect(toEscapedString('foo{bar}')).toBe('foo\\{bar\\}');
expect(toEscapedString('foo?bar')).toBe('foo\\?bar');
expect(toEscapedString('foo+bar')).toBe('foo\\+bar');
expect(toEscapedString('foo*bar')).toBe('foo\\*bar');
expect(toEscapedString('foo^bar')).toBe('foo\\^bar');
expect(toEscapedString('foo$bar')).toBe('foo\\$bar');
expect(toEscapedString('foo|bar')).toBe('foo\\|bar');
});
});

describe(toTitleCase.name, () => {
const HELLO = 'Hello';
const HELLO_WORLD = 'Hello World';
const REPLY_TO_THREAD = 'Reply to Thread';
const THE_QUICK_BROWN_FOX = 'The Quick Brown Fox';

test('If the input is an empty string...', () => {
expect(toTitleCase('')).toBe('');
});

test('If the input is already in title case...', () => {
expect(toTitleCase('Hello')).toBe(HELLO);
expect(toTitleCase('Hello World')).toBe(HELLO_WORLD);
expect(toTitleCase('Reply to Thread')).toBe(REPLY_TO_THREAD);
expect(toTitleCase('The Quick Brown Fox')).toBe(THE_QUICK_BROWN_FOX);
});

test('If the input is in "camelCase"...', () => {
expect(toTitleCase('helloWorld')).toBe(HELLO_WORLD);
expect(toTitleCase('replyToThread')).toBe(REPLY_TO_THREAD);
expect(toTitleCase('theQuickBrownFox')).toBe(THE_QUICK_BROWN_FOX);
});

test('If the input is in "snake_case"...', () => {
expect(toTitleCase('hello_world')).toBe(HELLO_WORLD);
expect(toTitleCase('reply_to_thread')).toBe(REPLY_TO_THREAD);
expect(toTitleCase('the_quick_brown_fox')).toBe(THE_QUICK_BROWN_FOX);
});

test('If the input is in "SCREAMING_SNAKE_CASE"...', () => {
expect(toTitleCase('HELLO')).toBe(HELLO);
expect(toTitleCase('HELLO_WORLD')).toBe(HELLO_WORLD);
expect(toTitleCase('REPLY_TO_THREAD')).toBe(REPLY_TO_THREAD);
expect(toTitleCase('THE_QUICK_BROWN_FOX')).toBe(THE_QUICK_BROWN_FOX);
});

test('If the input is all "lowercase"...', () => {
expect(toTitleCase('hello')).toBe(HELLO);
expect(toTitleCase('hello world')).toBe(HELLO_WORLD);
expect(toTitleCase('reply to thread')).toBe(REPLY_TO_THREAD);
expect(toTitleCase('the quick brown fox')).toBe(THE_QUICK_BROWN_FOX);
});
});
Loading