Skip to content

Instantly share code, notes, and snippets.

View nwellis's full-sized avatar

nwellis nwellis

  • Minneapolis, MN
View GitHub Profile
@nwellis
nwellis / TypeUtil.ts
Created April 18, 2025 12:03
Infer type from field
export function withType<TElement extends { __type: string }, TType extends TElement["__type"]>(
elements: TElement[],
type: TType
): Extract<TElement, { __type: TType }>[] {
return elements?.filter(element => element.__type === type) as Extract<TElement, { __type: TType }>[]
}
export function withoutType<TElement extends { __type: string }, TType extends TElement["__type"]>(
elements: TElement[],
type: TType
import { CleanupResult, InstallResult, PrecacheController } from "workbox-precaching"
export class CustomPrecacheController extends PrecacheController {
private _debugPrint = false
private _isPrecaching = false
get isPrecaching() { return this._isPrecaching }
private _installResult: InstallResult = undefined
@nwellis
nwellis / WorkboxSpineWarmHandle.ts
Created February 28, 2023 21:18
Workbox warm caching spine files and their associated image files
/**
* A spine export produces 3 file types- one JSON file and one atlas.txt that maps
* any number of PNGs. We know the JSON and atlas paths ahead of time, but not the PNGs.
* This method will return a promise that completes when the files are loaded. If the
* path is an atlas.txt file, it will wait for the response and read the file contents
* in order to start loading the associated PNGs as well.
*/
async function loadSpineFiles(event: ExtendableEvent, strategy: Strategy, path: string) {
const pending = Array.of<Promise<void>>()
@nwellis
nwellis / package.json
Created February 25, 2023 15:10
Rollup TS library
{
"name": "package-name",
"version": "0.9.0",
"description": "",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"browser": "dist/index.umd.js",
"types": "dist/index.d.ts",
"dependencies": {
"ms": "^2.1.3"
@nwellis
nwellis / ProxyMiddleware.mjs
Created February 25, 2023 15:04
Proxy server for Astro dev server
import { createProxyMiddleware } from "http-proxy-middleware"
/**
* Usage:
* ```js
* proxyMiddleware("proxy/path", {
* target: "https://target.proxy.io",
* changeOrigin: true,
* }),
* ```
@nwellis
nwellis / Spine.astro
Last active February 25, 2023 14:59
Renders a spine in an HTML canvas element with WebGL enabled
---
export type Props = {
id?: string
assets: {
jsonUri: string
atlasUri: string
skin?: string
}
}
export type Lazy<T, TArgs = void> = {
isInitialized(): boolean
get(args: TArgs): T
update(data: T)
clear(): Lazy<T>
}
export const lazy = <T, TArgs = void>(loader: (args: TArgs) => T) => {
let initialized = false
type ChildAndParent<TParent, TChild> = TParent extends string
? `${TParent}.${TChild extends string ? TChild : never}`
: TChild;
type DotNotation<T, TParent = false> = T extends object
? { [K in keyof T]: TParent | DotNotation<T[K], ChildAndParent<TParent, K>> }[keyof T]
: TParent
type Foo = DotNotation<{
a: "hello"
b: "darkness"
type ParamBuilder<TArgs extends Record<string, any>, TReturn> = {
[K in keyof TArgs]: (arg: TArgs[K]) => ParamBuilder<Omit<TArgs, K>, TReturn>;
} & (keyof TArgs extends never ? { call(): TReturn } : {})
function builderFor<TArgs extends object, TReturn>(fn: (args: TArgs) => TReturn) {
const args: TArgs = {} as unknown as TArgs;
return new Proxy({}, {
get(self, prop) {
if (prop === "call") {
@nwellis
nwellis / WindowInsetsExtensions.kt
Last active April 22, 2021 18:12
Extension functions using window insets
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.sample.app.R
private val statusAndNavBarType =
WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars()