This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const formatToHTMLInput = (date: Date) => { | |
const year = date.getFullYear(); | |
const month = date.getMonth(); | |
const day = date.getDate(); | |
return `${year}-${`0${month}`.slice(-2)}-${`0${day}`.slice(-2)}`; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function http<T>(url: string, config: RequestInit): Promise<T> { | |
const request = new Request(url, config); | |
const response = await fetch(request); | |
if (!response.ok) { | |
const err = new Error(response.statusText); | |
err.name = 'HTTPError'; | |
throw err; | |
} | |
return await response.json().catch(() => ({})); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// NestJS documentation on schema pipes: | |
// https://docs.nestjs.com/pipes#schema-based-validation | |
import { ArgumentMetadata, BadRequestException, Injectable, PipeTransform } from '@nestjs/common'; | |
import { SchemaOf, ValidationError } from 'yup'; | |
@Injectable() | |
export class YupValidationPripe<T> implements PipeTransform { | |
constructor(private schema: SchemaOf<T>) {} | |
transform(value: T, _: ArgumentMetadata) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const isRejected = ( | |
input: PromiseSettledResult<unknown>, | |
): input is PromiseRejectedResult => input.status === 'rejected'; | |
export const isFulfilled = <T>( | |
input: PromiseSettledResult<T>, | |
): input is PromiseFulfilledResult<T> => input.status === 'fulfilled'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ParsedUrlQuery } from 'querystring' | |
export function parsedUrlQueryToURLSearchParams( | |
query: ParsedUrlQuery | |
): URLSearchParams { | |
const searchParams = new URLSearchParams() | |
for (const [key, value] of Object.entries(query)) { | |
if (!value) continue | |
if (Array.isArray(value)) { | |
value.forEach((element) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRouter } from "next/router"; | |
import { ParsedUrlQuery } from "querystring"; | |
interface UseRouterParamsOptions { | |
method?: "push" | "replace"; | |
shallow?: boolean; | |
} | |
const useRouterParams = (options?: UseRouterParamsOptions) => { | |
const { query, pathname, push, replace } = useRouter(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function extractInitials (name: string, count = 2) { | |
const re = new RegExp(/\b\w/g) | |
const initials = name.match(re) | |
return initials !== null | |
? initials.reduce((prev, curr, index) => | |
index < count ? prev + curr : prev | |
) | |
: null | |
} |