Skip to content

Instantly share code, notes, and snippets.

View ededejr's full-sized avatar

Edede Oiwoh ededejr

View GitHub Profile
@ededejr
ededejr / lignes.roomates.ts
Created June 18, 2024 01:01
a sample implementation for a lignes model
import { EtchModel, Grain, Palette } from './utils';
const Token = {
pixelDensity: 5,
strokeWeight: 5,
pointVariance: 0.05,
lineSegments: 20,
numberOfLines: 5,
shouldUseFuzzyGrain: false,
shouldUseScaledColors: false,
@ededejr
ededejr / recording-state.tsx
Created May 31, 2024 21:13
record input from mic to zustand
'use client';
import { create } from 'zustand';
type RecordingStore = {
recorder?: MediaRecorder;
isRecording: boolean;
isDiscarding: boolean;
recordingTimeMs: number;
recordingProgressToMax: number;
recordingTimer?: NodeJS.Timeout;
@ededejr
ededejr / use-current-color-value.tsx
Created May 24, 2024 03:12
use the current css color of a passed ref or the document
import type React from "react";
import { useEffect, useState } from "react";
export function useCurrentColorValue(ref?: React.RefObject<HTMLElement>) {
const [color, setColor] = useState<string | null>(null);
useEffect(() => {
const updateColor = () => {
if (ref?.current) {
setColor(getComputedStyle(ref.current).color);
@ededejr
ededejr / sh.ts
Created May 19, 2024 23:45
small nodejs sh wrapper
import { ExecSyncOptions, execSync } from 'child_process';
export function sh(command: TemplateStringsArray, ...substitutions: any[]) {
const fullCommand = command.reduce((acc, part, i) => acc + part + (substitutions[i] || ''), '');
return (options?: ExecSyncOptions) => execSync(fullCommand, { encoding: 'utf-8', ...(options || {}) });
}
@ededejr
ededejr / waitgroup.py
Last active April 10, 2024 18:52
a simple wait group written before discovering asyncio.TaskGroup
import asyncio
import logging
from typing import Any, Callable, Coroutine, List, Optional
logger = logging.getLogger(__name__)
class Waitgroup:
def __init__(self, timeout: Optional[float] = None):
self.timeout = timeout
@ededejr
ededejr / cacheable.ts
Created March 25, 2024 03:32
an extensible class which provides caching methods
import TTLCache, { Options } from '@isaacs/ttlcache';
export type CacheOptions<K = string, V = unknown> = Options<K, V>;
export class Cacheable<K = string, V = unknown> {
protected cache: TTLCache<K, V>;
constructor(options: Options<K, V> = { ttl: 1000 * 60 * 10 }) {
this.cache = new TTLCache(options);
}
@ededejr
ededejr / responsive-dialog.tsx
Last active December 22, 2023 21:15
quick hack for a responsive dialog-drawer built on components from ui.shadcn.com
import * as React from 'react';
import { useMediaQuery } from '@/lib/hooks';
import * as DialogParts from '@/ui/dialog';
import * as DrawerParts from '@/ui/drawer';
type ResponsiveDialogPartsKey =
| ''
| 'Portal'
| 'Overlay'
@ededejr
ededejr / use-relative-timestamp.tsx
Created December 1, 2023 17:19
a react hook for formatting timestamps relative to now
function useRelativeTimestamp(ts: number) {
const now = Date.now();
let displayTime = "";
const diff = now - ts;
const timestamp = new Date(ts);
if (diff <= 1000 * 60 * 60) {
const formatter = new Intl.RelativeTimeFormat("en", { numeric: "auto" });
@ededejr
ededejr / debug-with-context.ts
Created June 13, 2023 21:18
simple debug function which logs its caller
const debug = (...messages: unknown[]) => {
  const { stack } = new Error("");
  const caller = stack?.split("\n")?.[2].trim().split(" ")[1];
  console.log(`[ededejr|${caller}]: `, ...messages);
};
@ededejr
ededejr / on-process-exit.ts
Last active February 6, 2024 05:21
a utility function for executing callbacks on process exit in nodejs
function onProcessExit(callback: () => void): void {
[
"SIGINT",
"SIGTERM",
"SIGHUP",
"SIGBREAK",
"unhandledRejection",
"uncaughtException",
].forEach((signal: string) => {
process.on(signal, () => {