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 isObject = (val: unknown): boolean => | |
val instanceof Object && !Array.isArray(val); | |
export const propertiesToArray = (obj: object): string[] => { | |
const addDelimiter = (a: string, b: string) => (a ? `${a}.${b}` : b); | |
const paths = (obj: Record<string, any> = {}, head = ''): string[] => { | |
return Object.entries(obj).reduce<string[]>((product, [key, value]) => { | |
const fullPath = addDelimiter(head, key); | |
return isObject(value) |
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 getDeepValue = < | |
T extends Record<string, any>, | |
K extends string = string, | |
>( | |
obj: T, | |
path: DeepKey<T, K> | DeepKey<T, K>[], | |
): DeepReturn<T, K> => { | |
const paths = Array.isArray(path) ? path : path.split('.'); | |
return paths.reduce<DeepReturn<T, K>>((acc, curr) => { | |
return acc[curr]; |
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
type DeepReturn<T, K extends string = string> = K extends keyof T | |
? T[K] | |
: K extends `${infer TKey}.${infer Rest}` | |
? TKey extends keyof T | |
? DeepReturn<Exclude<T[TKey], undefined>, Rest> | |
: undefined | |
: undefined; |
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
type ValidValues = string | number | Record<string, any> | Array<any> | undefined | null; | |
type DeepKey<T extends Record<string, any>, K extends string = string> = K extends keyof T | |
? T[K] extends ValidValues | |
? K | |
: never | |
: K extends `${infer TKey}.${infer Rest}` | |
? T[TKey] extends ValidValues | |
? DeepKey<Exclude<T[TKey], undefined>, Rest> extends never | |
? never |
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
const decodedResponse = await fetch('https://www.anysite.rss.xml', { | |
headers: { | |
'Content-Type': 'text/xml;charset=ISO-8859-1', | |
}, | |
}) | |
.then(r => r.arrayBuffer()) // resolve the response stream for buffer | |
.then(d => { | |
const dataView = new DataView(d); // creates a DataView object representing the buffer data | |
const isoDecoder = new TextDecoder('ISO-8859-1'); // creates an instance of the our decoder | |
const decodedString = isoDecoder.decode(dataView); // so we pass the stream of bytes for the decoder do its job |
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 { useEffect, useRef, useState } from 'react'; | |
const useMyHook = (cb, delay = 1000) => { | |
const savedCb = useRef(); | |
useEffect(() => { | |
savedCb.current = cb; | |
}, [cb]); | |
useEffect(() => { |
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
class CPF { | |
static #filter(cpfRaw) { | |
if(typeof cpfRaw !== 'string') return; | |
return cpfRaw.replace(/\D+/g, ''); | |
} | |
static #isSequence(value) { | |
const sequence = value[0].repeat(value.length); | |
return sequence === value; | |
} |