Skip to content

Instantly share code, notes, and snippets.

View JacobWeisenburger's full-sized avatar
🚀
Available for hire!

Jacob Weisenburger JacobWeisenburger

🚀
Available for hire!
  • Weis Guys
  • Twin Cities, MN
  • 09:46 (UTC -05:00)
  • X @JakeWeisDev
View GitHub Profile
@t3dotgg
t3dotgg / try-catch.ts
Last active May 17, 2025 03:36
Theo's preferred way of handling try/catch in TypeScript
// Types for the result object with discriminated union
type Success<T> = {
data: T;
error: null;
};
type Failure<E> = {
data: null;
error: E;
};
@JacobWeisenburger
JacobWeisenburger / CustomErrorMessages.ts
Created February 10, 2025 19:07
A way to make custom error messages for types
type ValidateType<T extends string> =
T extends 'bad' ? 'Error message'
: T extends 'another bad' ? 'Another error message'
: T extends 'Error message' | 'Another error message' ? `Can't use '${ T }'`
: T
function fn<T extends string> ( arg: ValidateType<T> ) { }
fn( 'this should work' ) // works
fn( 'bad' ) // Argument of type '"bad"' is not assignable to parameter of type '"Error message"'.
@JacobWeisenburger
JacobWeisenburger / checkIfYouTubeChannelIsLive.ts
Last active January 28, 2025 17:59
checkIfYouTubeChannelIsLive
/**
CORS blocks this request from the browser, so you will need to run this code on the server.
*/
async function checkIfYouTubeChannelIsLive ( channelId: string ) {
const liveCheckURL = `https://www.youtube.com/embed/live_stream?channel=${ channelId }`
const responseText = await fetch( liveCheckURL ).then( res => res.text() )
const isLive = responseText.indexOf( 'This video is unavailable' ) < 0
return isLive
}
@JacobWeisenburger
JacobWeisenburger / makeErrorMap.ts
Last active January 13, 2023 16:27
an easier way to make error maps for Zod
import { z } from 'zod'
type ErrorCode = z.ZodIssueCode | 'required'
type Message = string
type MessageBuilder = ( data: unknown, options?: any[] ) => Message
type ErrorRecord = Partial<Record<ErrorCode, Message | MessageBuilder>>
function makeErrorMap ( errorRecord: ErrorRecord ): z.ZodErrorMap {
return ( issue, ctx ) => {
const options = issue.code === 'invalid_enum_value'
? issue.options : undefined
@JacobWeisenburger
JacobWeisenburger / makeSearchParamsObjectSchema.ts
Last active April 19, 2025 19:41
a way to parse URLSearchParams with Zod
import { z } from 'zod'
function safeParseJSON ( string: string ): any {
try { return JSON.parse( string ) }
catch { return string }
}
function searchParamsToValues ( searchParams: URLSearchParams ): Record<string, any> {
return Array.from( searchParams.keys() ).reduce( ( record, key ) => {
const values = searchParams.getAll( key ).map( safeParseJSON )
@souporserious
souporserious / headings.js
Last active June 24, 2023 17:16
Extract headings from a markdown string.
const headings = content
.split('\n')
.filter((line) => line.match(/#{1,3}\s/))
.map((line) => {
const [, level, title] = line.match(/(#{1,3})\s(.*)/)
return {
level: level.length,
title,
}
})
@pierrejoubert73
pierrejoubert73 / markdown-details-collapsible.md
Last active May 14, 2025 20:12
How to add a collapsible section in markdown.

How to add a collapsible section in markdown

1. Example

Click me

Heading

  1. Foo
  2. Bar
    • Baz
  • Qux