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 / useKeyPress.ts
Last active May 15, 2021 14:42
Custom react hook for window events 'keyup' and 'keydown'
import { useEffect, useState } from 'react';
type Callback = (element?: string | number) => void
type TargetKey = string | number | Array<string|number>
const isNumber = (value: unknown): value is number => typeof value === 'number';
const isString = (value: unknown): value is string => typeof value === 'string';
const isNumberArray = (object: unknown): object is number[] =>
Array.isArray(object) && object.every(e => isNumber(e));
@RodrigoNovais
RodrigoNovais / useCounter.ts
Last active April 15, 2020 16:57
Custom react hook to handle counters
import { useState, useCallback } from 'react'
export type Counter = {
count: number
increment: (value?: number | undefined) => void
decrement: (value?: number | undefined) => void
reset: (value?: number | undefined) => void
}
export default (initialValue: number = 0): Counter => {
@RodrigoNovais
RodrigoNovais / notEmpty.ts
Created April 19, 2020 18:37
not empty value checker
export default <T>(value: T | null | undefined): value is T => !!value
@RodrigoNovais
RodrigoNovais / hasKey.ts
Created April 19, 2020 19:31
custom object has a key checker
export const hasKey = <T>(object: T, key: keyof any): key is keyof T => key in object
type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
Pick<T, Exclude<keyof T, Keys>>
& {
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>
}[Keys]
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]
@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) => {
@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 / 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 / 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][]
}
}