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 { SetStateAction, useCallback, useState } from 'react' | |
type SetStateWithID<P> = (state: SetStateAction<P>, id?: unknown) => void | |
function useStateWithID<P extends { [key: string]: any }>( | |
initialState: P, | |
idField: string, | |
): [P & { [key: string]: any }, SetStateWithID<P>] { | |
const [state, setState] = useState<P>(initialState) |
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 function assertFulfilled<T>(item: PromiseSettledResult<T>): item is PromiseFulfilledResult<T> { | |
return item.status === 'fulfilled' | |
} | |
export function assertRejected<T>(item: PromiseSettledResult<T>): item is PromiseRejectedResult { | |
return item.status === 'rejected' | |
} | |
// promises.filter(assertFulfilled).map(f => f.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
name: Publish NPM Package | |
on: | |
push: | |
tags: [v*] | |
jobs: | |
build: | |
runs-on: ubuntu-20.04 | |
steps: |
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 React, { | |
useEffect, | |
useRef, | |
} from 'react' | |
export interface Props { | |
src: string | |
} | |
function ExternalScript({ src }: Props) { |
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 function createDeferredPromise<T = void>() { | |
let resolve: ((value: T) => void) | |
let reject: ((reason?: any) => void) | |
const promise = new Promise<T>((res, rej) => { | |
resolve = res | |
reject = rej | |
}) | |
return { | |
resolve: resolve!, |
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 function convertHMSToSeconds(hms: string): number { | |
const [s, m, h] = hms.split(':').reverse() | |
let time = 0 | |
if (s) { | |
const seconds = parseInt(s) | |
if (!Number.isNaN(seconds)) { | |
time += seconds | |
} |
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
// This should be rewritten using Observers | |
function useEventListener<K extends keyof DocumentEventMap>(event: K, handler: (e: DocumentEventMap[K]) => any, deps: DependencyList = []) { | |
useEffect(function listenToEvent() { | |
document.addEventListener(event, handler) | |
return () => document.removeEventListener(event, handler) | |
}, [event, handler, ...deps]) | |
} | |
export default useEventListener |
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 cn from 'classnames' | |
import { | |
useRef, | |
ReactNode, | |
MouseEvent as ReactMouseEvent, | |
useState, | |
} from 'react' | |
// See `useElement` gist | |
import useElement from './useElement' |
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 ( | |
"math/rand" | |
) | |
var alphabet = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") | |
func genRandom(length int) string { | |
b := make([]rune, length) | |
for i := range b { | |
b[i] = alphabet[rand.Intn(len(alphabet))] |
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 function customAlphabet(alphabet: string, size: number) { | |
return () => { | |
let id = '' | |
// A compact alternative for `for (var i = 0; i < step; i++)`. | |
let i = size | |
while (i--) { | |
// `| 0` is more compact and faster than `Math.floor()`. | |
id += alphabet[(Math.random() * alphabet.length) | 0] | |
} | |
return id |