Skip to content

Instantly share code, notes, and snippets.

View ViliamKopecky's full-sized avatar
🤡
clowning

Viliam Kopecký ViliamKopecky

🤡
clowning
View GitHub Profile
@ViliamKopecky
ViliamKopecky / DirectionalBlur.tsx
Last active September 27, 2024 11:45
DirectionalBlur.tsx
import type { FunctionComponent, PropsWithChildren } from 'react'
export const DirectionalBlur: FunctionComponent<
PropsWithChildren<{
spread?: number
blur?: number
rotationDeg?: number
}>
> = ({ children, spread = 100, blur = 0.5, rotationDeg = 0 }) => {
return (
@ViliamKopecky
ViliamKopecky / objectEntries.ts
Created August 29, 2024 07:42
typesafe objectEntries, objectFromEntries
/**
* Utility type to make complex types more readable in tooltips and error messages.
*/
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
type ObjectEntry<T extends object> = Prettify<{ [K in keyof T]: [K, T[K]] }[keyof T]>;
type FromEntries<T extends Array<[PropertyKey, unknown]>> = {
@ViliamKopecky
ViliamKopecky / opRead.ts
Created October 23, 2023 20:02
1Password read password from Bun
export async function opRead(path: `op://${string}/${string}/${string}`) {
return await new Response(Bun.spawn(["op", "read", path]).stdout).text();
}
@ViliamKopecky
ViliamKopecky / Join.tsx
Last active January 10, 2024 10:37
Array.join for React children
import React from 'react'
export function Join(props: { items: Array<React.ReactNode>; glue?: React.ReactNode; lastGlue?: React.ReactNode }) {
const glue = props.glue ?? ', '
const last = props.lastGlue ?? glue
return (
<>
{props.items.map((item, i, list) => (
<React.Fragment key={i}>
@ViliamKopecky
ViliamKopecky / loadPaginated.ts
Created April 10, 2022 09:47
loading paginated async data
export async function loadPaginated<R>(loader: (page: number) => Promise<R[]>) {
const results: R[] = []
while (true) {
const found = await loader(i)
results.push(...found)
if (!found.length) {
break
}
}
@ViliamKopecky
ViliamKopecky / useSwitchingInterval.ts
Created March 23, 2022 15:40
useSwitchingInterval
function useSwitchingInterval<Item>(items: readonly Item[], seconds = 2) {
const [active, setActive] = useState(0)
useEffect(() => {
let timeout: null | NodeJS.Timeout = null
const next = () => {
setActive((a) => a + 1)
timeout = setTimeout(next, seconds * 1000)
}
@ViliamKopecky
ViliamKopecky / React.CSSProperties.d.ts
Created March 17, 2022 16:46
TypeScript definitions for React.CSSProperties with custom CSS properties support
declare namespace React {
interface CSSProperties {
[key: `--${string}`]: string | number | null | undefined
}
}
@ViliamKopecky
ViliamKopecky / minimemo.ts
Created March 10, 2022 12:48
minimemo.ts
export async function memo<Value>(
key: string,
loader: () => Value | Promise<Value>
) {
const g = global as unknown as {
memory: Record<string, { value: Value | Promise<Value>; timestamp: number }>
}
const memory = g.memory ?? {}
g.memory = memory
@ViliamKopecky
ViliamKopecky / arrayify.ts
Created March 2, 2022 08:40
array of objects to object of arrays
type ArrayifyFields<Original> = Original extends Record<string, unknown>
? {
[key in keyof Original]: Original[key][]
}
: never
export function arrayifyFields<Item extends Record<string, unknown>>(items: Item[]) {
return items.reduce((grouped, item) => {
const entries = Object.entries(item).map(
([key, value]) => [key, grouped ? [grouped[key], value] : [value]] as const,
@ViliamKopecky
ViliamKopecky / React-CSSProperties.d.ts
Created January 4, 2022 12:32
React extend declaration of CSSProperties with custom properties
declare module 'react' {
interface CSSProperties {
[key: `--${string}`]?: string | number | undefined
}
}