Skip to content

Instantly share code, notes, and snippets.

View gustavoguichard's full-sized avatar
🏠
Working from home

Guga Guichard gustavoguichard

🏠
Working from home
View GitHub Profile
@gustavoguichard
gustavoguichard / merge-headers.ts
Last active September 26, 2024 21:11
A function to merge JS Headers
/**
* It merges multiple HeadersInit objects into a single Headers object
* @param entries Any number of HeadersInit objects
* @returns a new Headers object with the merged headers
*/
function mergeHeaders(...sources: HeadersInit[]) {
let result = new Headers()
for (let headersInit of sources) {
let headers = new Headers(headersInit)
@gustavoguichard
gustavoguichard / type-utils.ts
Created March 23, 2023 03:23
Type utilities
type Specify<T> = T extends any ? (k: T) => void : never
// Real intersection: UnionToIntersection<`c${string}` | 'car'> will become `c${string}`
type UnionToIntersection<T> = Specify<T> extends (a: infer A) => void ? A : never;
// Transforms 'a' | 'b' into ((f: "a") => void) & ((f: "b") => void)
type UnionToOverloads<U> = UnionToIntersection<Specify<U>>;
// Still figuring out why this happens
// ((f: "a") => void) & ((f: "b") => void) will become "b"
@gustavoguichard
gustavoguichard / union-to-tuple.ts
Last active January 6, 2023 20:09
Union to Tuple
type Specify<T> = T extends any ? (k: T) => void : never
// Real intersection: UnionToIntersection<`c${string}` | 'car'> will become `c${string}`
type UnionToIntersection<T> = Specify<T> extends (a: infer A) => void ? A : never;
// Transforms 'a' | 'b' into ((f: "a") => void) & ((f: "b") => void)
type UnionToOverloads<U> = UnionToIntersection<Specify<U>>;
// Still figuring out why this happens
// ((f: "a") => void) & ((f: "b") => void) will become "b"
@gustavoguichard
gustavoguichard / tailwind.config.js
Created March 22, 2022 12:12
Tailwind children selector
module.exports = {
theme: {
extend: {
// ...
},
},
plugins: [
function ({ addVariant }) {
addVariant('children', '& > *')
},
@gustavoguichard
gustavoguichard / abort.js
Created August 30, 2021 20:28
Abort a promise
const controller = new AbortController()
const toBeAborted = fetch('/', { signal: controller.signal })
controller.abort()
@gustavoguichard
gustavoguichard / body.getReader.js
Created August 30, 2021 20:25
Show body data in pieces
const res = await fetch('/')
const reader = res.body.getReader()
const data = await reader.read()
new TextDecoder().decode(data.value)
while(!data.done) {
console.log(new TextDecoder().decode(data.value))
data = await reader.read()
}
@gustavoguichard
gustavoguichard / useStore.js
Created April 16, 2021 19:40
Global state
import { useState, useEffect } from 'react'
function setState(newState, updateContext) {
this.state = { ...this.state, ...newState }
this.listeners &&
this.listeners.forEach(([context, listener]) => {
updateContext === context && listener(this.state)
})
}
ruby -F, -ane \
'puts $F[15].gsub(/"/, "") unless $F[15] !~ /\w+@\w+\.\w{2,5}/' \
~/Desktop/cadastros.csv > emails.txt
import compact from 'lodash/compact'
import get from 'lodash/get'
import head from 'lodash/head'
import orderBy from 'lodash/orderBy'
const getFirstValue = (el, path) => {
const paths = path.split('|')
const values = paths.map(path => get(el, path))
return head(compact(values))
}
const useOpenExternalLinksInNewTab = (el = typeof document === 'undefined' ? null : document) => {
useEffect(() => {
if (el && typeof window !== 'undefined') {
const links = el.querySelectorAll(
`a[href*="//"]:not([href*="${window.location.host}"])`,
)
links.forEach((link) => {
link.setAttribute('target', '_blank')
link.setAttribute('rel', 'noopener')
})