Skip to content

Instantly share code, notes, and snippets.

View RodrigoNovais's full-sized avatar

Rodrigo Tolentino de Novais RodrigoNovais

View GitHub Profile
@RodrigoNovais
RodrigoNovais / .eslintrc-node
Last active May 18, 2023 03:46
Configuração de ESLint para NodeJS
{
"env": {
"node": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/errors",
"plugin:import/warnings",
@RodrigoNovais
RodrigoNovais / Optional.ts
Created January 9, 2023 06:19
Make properties optional in TypeScript
export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
/**
* Group array of objects by given keys
*
* @param keys keys to be grouped by
* @param array objects to be grouped
*
* @returns an object with objects in `array` grouped by `keys`
*/
export const groupBy = <T>(keys: (keyof T)[]) => (array: T[]): Record<string, T[]> => {
return array.reduce((objectsByKeyValue, obj) => {
@RodrigoNovais
RodrigoNovais / portal.tsx
Created May 15, 2021 18:41
NextJS Portal Template
import React, { Fragment, useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
const Portal: React.FC<{ selector: string }> = ({ children, selector }) => {
const [mounted, setMounted] = useState<boolean>(false);
useEffect(() => {
setMounted(true);
return () => setMounted(false);
}, [selector]);
@RodrigoNovais
RodrigoNovais / timer.ts
Last active May 19, 2021 03:14
Utility timer class
import EventEmitter from 'events';
export interface TimerEvents {
'tick': (time: number) => void;
'complete': () => void;
}
export interface Timer {
addListener<U extends keyof TimerEvents>(event: U, listener: TimerEvents[U]): this;
prependListener<U extends keyof TimerEvents>(event: U, listener: TimerEvents[U]): this;
@RodrigoNovais
RodrigoNovais / array-extension.ts
Last active October 26, 2022 02:27
Typescript extension methods
declare global {
interface Array<T> {
/**
* Creates an tuple of grouped elements limited by the length of the first array.
* @template U
* @param {U} list
*/
zip<U>(list: U[]): [T, U][]
}
}
@RodrigoNovais
RodrigoNovais / intersection.ts
Last active May 24, 2021 18:20
Returns the values from array that are present in each of the other arrays
/**
* Returns the values from array that are present in each of the other arrays
*
* @param {...unknown[]} arrays
* @returns {unknown[]}
*/
export const intersection = (...arrays: unknown[]): unknown[] => {
return arrays.reduce((a, b) => a.filter((c: any) => b.includes(c)))
};
@RodrigoNovais
RodrigoNovais / difference.ts
Last active May 24, 2021 18:19
Returns the values from array that are not present in the other arrays
/**
* Returns the values from array that are not present in the other arrays
*
* @param {unknown[]} first
* @param {unknown[]} second
* @returns {unknown[]}
*/
export const difference = (first: unknown[], second: unknown[]): unknown[] => {
return first.filter(item => !second.includes(item));
};
@RodrigoNovais
RodrigoNovais / chunck.ts
Created April 4, 2021 01:07
Create an array of elements split into groups the length of size
/**
* Creates an array of elements split into groups the length of size
*
* @template T
* @param {T[]} input
* @param {number} size
* @returns {T[][]}
*/
export const chunk = <T>(input: T[], size: number): T[][] => {
return input.reduce((array, item, index) => {
type RequireOnlyOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>>
& {
[K in Keys]-?:
Required<Pick<T, K>>
& Partial<Record<Exclude<Keys, K>, undefined>>
}[Keys]