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 / 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')
})
@gustavoguichard
gustavoguichard / Masonry.js
Created November 10, 2018 00:16
Masonry react component
import React, { cloneElement, memo } from 'react'
import times from 'lodash/times'
const Masonry = ({ columns, children, ...props }) => (
<div {...props} className="masonry-tile">
{times(columns, index => (
<div className="vert-tile" key={`tile-${index}`}>
{React.Children.toArray(children)
.filter(
(child, filterIndex) =>