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
  • 18:44 (UTC -05:00)
  • X @JakeWeisDev
View GitHub Profile
@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 8, 2024 07:07
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 20, 2024 22:17
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