Skip to content

Instantly share code, notes, and snippets.

View WomB0ComB0's full-sized avatar
πŸ˜΅β€πŸ’«
I need an Advil

Mike Odnis WomB0ComB0

πŸ˜΅β€πŸ’«
I need an Advil
View GitHub Profile
@WomB0ComB0
WomB0ComB0 / build-time-typescript-type-json-parser.ts
Created November 19, 2024 07:16
Build-time Typescript-Type-safe JSON parser
// Base utility types
type Whitespace = '\u{9}' | '\u{A}' | '\u{20}' // tab, newline, space
type TrimLeft<V extends string> = V extends `${Whitespace}${infer R}`
? TrimLeft<R>
: V
type TrimRight<V extends string> = V extends `${infer R}${Whitespace}`
? TrimRight<R>
: V
/**
* Node.js file system module for file operations
*/
import fs from "node:fs";
/**
* Node.js path module for handling file paths
*/
import path from "node:path";
/**
@WomB0ComB0
WomB0ComB0 / data-loader.tsx
Created November 22, 2024 16:51
Client-side fetch component to reduce excessive re-writing of the same sequence of code
'use client';
import React, { Suspense } from 'react';
import { ClientError, Loader } from '@/components'
import { fetcher } from "@/lib"
import { useSuspenseQuery } from '@tanstack/react-query';
import { catchError, parseCodePath } from '@/utils';
/**
* A React component that handles data fetching with built-in loading, error handling, and caching.
@WomB0ComB0
WomB0ComB0 / fetcher.ts
Created November 22, 2024 20:33
Configuration options for enhanced fetching that extends the base Axios request config
import axios, { type AxiosRequestConfig, AxiosError } from 'axios';
/**
* Wraps a promise to return a tuple containing either the resolved value or an error.
* Provides a cleaner way to handle promise rejections without try/catch blocks.
*
* @template T - The type of value that the promise resolves to
* @param {Promise<T>} promise - The promise to handle
* @returns {Promise<[undefined, T] | [Error]>} A promise that resolves to a tuple containing either:
* - [undefined, T] if the promise resolves successfully, where T is the resolved value
@WomB0ComB0
WomB0ComB0 / simplify-job-tracker-parser.py
Created November 23, 2024 03:18
Python CSV parser for simplify jobs
from typing import List, Dict
from datetime import datetime
import csv
def parse_jobs(file_path: str) -> List[Dict[str, str]]:
jobs = []
with open(file_path, "r", encoding="utf-8-sig") as f: # Changed to utf-8-sig
reader = csv.DictReader(f)
for row in reader:
# Parse the date
@WomB0ComB0
WomB0ComB0 / regex-suggestor.py
Created November 24, 2024 23:00
Python script that takes in 1x1 dimensional data to suggest the best regex against the inputted data
from collections import Counter
from typing import Dict, List, Tuple
def analyze_patterns(terms_dict: Dict[str, str]) -> Tuple[Dict[str, int], List[str]]:
"""
Analyzes terms to find common patterns and potential regex optimizations.
Returns frequency of patterns and suggested regex patterns.
"""
# Convert all terms to list and lowercase for consistency
@WomB0ComB0
WomB0ComB0 / advanced-graphs.ts
Created November 26, 2024 21:55
Advanced graphs (Dijkstra, Ford, A*) in TypeScript
/**
* Represents infinity for graph algorithms, using JavaScript's maximum safe integer
*/
const INF = Number.MAX_SAFE_INTEGER;
/**
* Creates a 2D array (matrix) initialized with zeros
* @param {number} x - Number of rows
* @param {number} y - Number of columns
* @returns {number[][]} A 2D array of dimensions x by y filled with zeros
@WomB0ComB0
WomB0ComB0 / hono-mutation.ts
Created November 27, 2024 16:30
Optimizing API Mutations with React Query and Hono API
import { toast } from "sonner";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { InferRequestType, InferResponseType } from "hono";
import { hono_api } from "@/providers/core/server/react";
import { useRouter } from "next/navigation";
type MutationOptions<TRoute extends keyof typeof hono_api> = {
route: TRoute;
method: keyof (typeof hono_api)[TRoute];
successMessage?: string;
@WomB0ComB0
WomB0ComB0 / extract-types-properties.ts
Created November 30, 2024 23:17
Extract the types and properties from input literal/type/tuple of types
type Decrement<N extends number> = [
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19
][N];
type ExtractNthProperty<T, N extends number> =
T extends readonly [infer First, ...infer Rest]
? N extends 0
? First
: ExtractNthProperty<Rest, Decrement<N>>
@WomB0ComB0
WomB0ComB0 / clean.ts
Created December 6, 2024 18:53
Utilities for https://github.com/WomB0ComB0/leetcode-solutions, utilizing the LeetCode GraphQL endpoint
import { promises as fs } from 'fs';
import path from 'path';
/**
* Utility class for cleaning and standardizing filenames across programming language directories.
* Handles conversion to kebab-case, roman numeral suffixes, and ensures unique filenames.
*/
class FilenameCleaner {
/** List of programming language directories to process */
private static readonly PROGRAMMING_DIRECTORIES = [