Skip to content

Instantly share code, notes, and snippets.

View ramirezsandin's full-sized avatar

Jorge R. ramirezsandin

View GitHub Profile
@ramirezsandin
ramirezsandin / dates.ts
Created August 19, 2022 09:25
Helper funcctions to work with dates
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)}`;
};
@ramirezsandin
ramirezsandin / fetch.ts
Created July 29, 2022 10:09
Custom Fetch library
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(() => ({}));
}
@ramirezsandin
ramirezsandin / yup-validation.pipe.ts
Last active July 23, 2022 15:01
Validation Pipe for Yup & NestJS
// 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) {
@ramirezsandin
ramirezsandin / promises.ts
Created July 18, 2022 14:56
Helpers functions for Promises
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';
@ramirezsandin
ramirezsandin / query-params.ts
Last active June 28, 2022 13:31
Helper functions to work with query params on the URL
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) => {
@ramirezsandin
ramirezsandin / useRouterParams.ts
Last active July 4, 2024 17:11
React hook to be used with Next.js, it uses useRouter internally and offers some helper functions to manipulate the url query params.
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();
@ramirezsandin
ramirezsandin / strings.ts
Last active August 11, 2022 06:36
Extract the number of initials from a given string. Ideal for names.
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
}