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 / simple_curl.php
Created May 10, 2012 00:57
Simple cURL wrapper function for GET, POST, PUT & DELETE methods
/**
* Wrapper for easy cURLing
*
* @author Viliam Kopecký
*
* @param string HTTP method (GET|POST|PUT|DELETE)
* @param string URI
* @param mixed content for POST and PUT methods
* @param array headers
* @param array curl options
@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 / 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 / plaintext-mailgun-dns-config.js
Last active October 4, 2022 14:22
Mailgun: get DNS config in plaintext
console.log(
[...document.querySelectorAll('tr')]
.map((tr) =>
[...tr.querySelectorAll('td:not(:last-child):not(:first-child)')]
.map((el) => `${el.textContent}`)
.join(' ')
.trim()
)
.filter(Boolean)
.join('\n\n')
@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
}
}