Skip to content

Instantly share code, notes, and snippets.

View Luisgustavom1's full-sized avatar

Luis Gustavo Macedo Luisgustavom1

View GitHub Profile
@Luisgustavom1
Luisgustavom1 / type-helpers.ts
Last active July 31, 2022 16:36
This is a types helper based on the specific types of properties. Created in this article to practice and study https://dev.to/busypeoples/notes-on-typescript-type-level-programming-part-3-3l03
type User = {
id: number,
name: string,
age: number,
active: boolean,
}
type FilterByType<T, U> = {
[K in keyof T]: T[K] extends U ? K : never
}[keyof T]
type PromiseAllType<P> =
P extends []
? []
: P extends [Promise<infer T> | infer T, ...infer F]
? [T, ...PromiseAllType<F>]
: never
declare function PromiseAll<P extends any[]>(args: readonly [...P]): Promise<PromiseAllType<P>>
const promise1 = Promise.resolve(3);
@Luisgustavom1
Luisgustavom1 / kebabcase.ts
Created July 31, 2022 23:04
Type-challenges - 612 Medium kebabcase
// 00612 - Medium kebabcase
type KebabCase<T extends string> =
T extends `${infer F}${infer R}`
? R extends Uncapitalize<R>
? `${Uncapitalize<F>}${KebabCase<R>}`
: `${Uncapitalize<F>}-${KebabCase<R>}`
: T
export type KebabCaseResult = KebabCase<'FooBarBaz'> //foo-bar-baz
@Luisgustavom1
Luisgustavom1 / Reverse.ts
Last active August 25, 2022 12:55
This is a reverse string using type level of typescript
type Reverse<A extends string> =
`${A}` extends `${infer H}${infer T}`
? `${Reverse<T>}${H}`
: A
type Diff<
Obj1,
Obj2,
SameKeys extends keyof Obj1 = keyof Obj1 & keyof Obj2
> = Omit<{
[K in (keyof Obj1 | keyof Obj2)]:
K extends keyof Obj1
? Obj1[K]
: K extends keyof Obj2
? Obj2[K]
@Luisgustavom1
Luisgustavom1 / ComposeProviders.tsx
Last active December 27, 2022 19:10
Compose React Providers to avoid too much chaining
import React from 'react'
interface IComposeProvidersProps {
with: Array<React.ElementType>
children: React.ReactNode
}
export const ComposeProviders = ({
with: Providers,
children,
@Luisgustavom1
Luisgustavom1 / useLocalStorage.ts
Created September 8, 2022 01:26
Hook to use states based in local storage
import React from 'react'
export const useLocalStorage = <T extends string | object>(
item: string,
initialValue = '',
): [string, React.Dispatch<(value: T) => T>] => {
const [localStorageItem, setLocalStorageItem] = React.useState(() => {
const itemStoraged = localStorage.getItem(item)
if (itemStoraged) {
@Luisgustavom1
Luisgustavom1 / new Proxy().js
Last active November 12, 2022 20:18
An example of new Proxy use
const user = {
name: 'luis'
}
const messages = {
hello: (userName) => `Ola ${userName}, seja bem vindo ao nosso sistema`,
goodBye: (userName) => `Até mais ${userName}, esperamos que volte sempre aqui`,
}
const messagesHandler = {
get(target, prop, receiver) {
return target[prop](user.name)
@Luisgustavom1
Luisgustavom1 / css-reset.css
Last active May 7, 2023 20:41
A simple reset css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
font-size: 62.5%;
}
@Luisgustavom1
Luisgustavom1 / settings.json
Last active December 3, 2023 18:23
My VSCode settings
{
"explorer.confirmDragAndDrop": true,
"diffEditor.wordWrap": "on",
"editor.fontSize": 18,
"editor.insertSpaces": true,
"editor.detectIndentation": true,
"launch": {
"configurations": []
},
"editor.codeActionsOnSave": {