Skip to content

Instantly share code, notes, and snippets.

@ValentaTomas
ValentaTomas / useStateWithID.ts
Created October 10, 2022 18:33
Update state only if the specified field matches
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)
@ValentaTomas
ValentaTomas / filterSettledPromise.ts
Created September 24, 2022 20:10
Filter settled promise and correctly handle the resulting types
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)
@ValentaTomas
ValentaTomas / npm-release.yml
Created August 30, 2022 10:54
Release NPM package on tag push
name: Publish NPM Package
on:
push:
tags: [v*]
jobs:
build:
runs-on: ubuntu-20.04
steps:
@ValentaTomas
ValentaTomas / ExternalScript.tsx
Created August 30, 2022 10:39
Add external script to the DOM and execute it
import React, {
useEffect,
useRef,
} from 'react'
export interface Props {
src: string
}
function ExternalScript({ src }: Props) {
@ValentaTomas
ValentaTomas / createDeferredPromise.ts
Last active August 22, 2022 19:58
Create a deferred promise
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!,
@ValentaTomas
ValentaTomas / hmsToSeconds.ts
Created July 28, 2022 11:01
Convert hours:minutes:seconds to seconds
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
}
@ValentaTomas
ValentaTomas / useEventListener.tsx
Created July 11, 2022 10:00
Primitive hook for subscribing to events
// 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
@ValentaTomas
ValentaTomas / VerticalResizer.tsx
Last active July 11, 2022 10:01
Component that allows vertical drag and drop resizing for its children
import cn from 'classnames'
import {
useRef,
ReactNode,
MouseEvent as ReactMouseEvent,
useState,
} from 'react'
// See `useElement` gist
import useElement from './useElement'
@ValentaTomas
ValentaTomas / id.go
Created May 20, 2022 20:54
Very primitive ID generator
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))]
@ValentaTomas
ValentaTomas / id.ts
Created May 20, 2022 20:54
Very primitive ID generator
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