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
  • 05:26 (UTC -05:00)
  • X @JakeWeisDev
View GitHub Profile
@JacobWeisenburger
JacobWeisenburger / zustandCustomPersistStorageTemplate.ts
Created March 31, 2023 16:16
Zustand custom persist storage template
import { createStore } from 'zustand'
import { persist, StorageValue } from 'zustand/middleware'
const store = createStore(
persist(
() => ( {} ),
{
name: 'store-state',
storage: {
async getItem ( name: string ): StorageValue<unknown> {
@JacobWeisenburger
JacobWeisenburger / ZustandStore.tsx
Created March 30, 2023 17:54
How to use some of Zustand in Deno Fresh
import { useSyncExternalStore } from 'https://esm.sh/preact@10.11.2/compat'
import { createStore, StoreApi } from 'https://raw.githubusercontent.com/pmndrs/zustand/v4.3.6/src/vanilla.ts'
function useStore<State> ( store: StoreApi<State> ) {
return useSyncExternalStore( store.subscribe, store.getState )
}
const store = createStore( () => ( { count: 0, foo: 'bar' } ) )
export default function ZustandStore () {
@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 )
@JacobWeisenburger
JacobWeisenburger / package.json
Last active January 20, 2023 16:41
ts-node-starter
{
"name": "ts-node-starter",
"main": "src/index.ts",
"author": "Jacob Weisenburger",
"type": "module",
"scripts": {
"start": "node --no-warnings --loader ts-node/esm --es-module-specifier-resolution=node src",
"dev": "nodemon",
"dev.cmd": "start cmd /k npm run dev"
},
import { z } from 'zod'
import { mapValues, omit, pick } from 'lodash'
function partialSafeParse<Schema extends z.ZodObject<any>> ( schema: Schema, input: unknown ) {
const result = schema.safeParse( input )
if ( result.success ) return result
const { fieldErrors, formErrors } = result.error.flatten()
if ( formErrors.length ) return result
import { Temporal } from 'temporal'
const TemporalDateUtils = ( datelike: string | Date ) => {
const date = typeof datelike === 'string' ? new Date( datelike ) : datelike
const datelikeString = typeof datelike === 'string' ? datelike : date.toISOString()
const instant = Temporal.Instant.from( date.toISOString() )
const plainDateString = instant.toString().split( 'T' )[ 0 ]
const timeIncluded = /T\d+/.test( datelikeString )
@JacobWeisenburger
JacobWeisenburger / CSS Supports Grid
Created September 6, 2017 00:21
Check to see if CSS Grid is supported
@supports (display: grid) {}