Skip to content

Instantly share code, notes, and snippets.

@colelawrence
colelawrence / objectPredicateMacro.ts
Created January 16, 2025 15:26
Simple object predicate macro - fast check for an object you need to check many keys exist on
const objectPredicateMacro = <T>(keys: (keyof T | (string & {}))[]): ((obj: unknown) => obj is T) => {
return new Function(
"isObject",
"value",
`return isObject(value) ${keys.map((key) => `&& ${JSON.stringify(key)} in value`).join(" ")};`,
).bind(null, isObject);
};
const isStore = objectPredicateMacro<JotaiStore>(["get", "set", "sub", "unstable_derive"]);
@colelawrence
colelawrence / feature-doc-guidelines.md
Created November 17, 2024 18:46
Feature doc guidelines example

Feature Documentation Guidelines

Feature docs are files in docs/my-idea-feature.md files.

(Product Values)

When considering the details for any given product feature keep in mind our values:

  1. Privacy and Security: Prioritize user data protection and system security in all feature designs
  2. Transparency: Ensure clear communication of data usage and consent processes
@colelawrence
colelawrence / listen.ts
Created July 30, 2024 18:15
Listen to event targets, returning the unsubscribe function. TypeScript types included for DOM
interface listen {
<K extends keyof HTMLElementEventMap>(
target: HTMLElement,
type: K,
listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => void,
options?: boolean | AddEventListenerOptions,
): () => void;
(
target: EventTarget,
type: string,
@colelawrence
colelawrence / unindent-template.ts
Last active April 3, 2025 00:07
unindent-template-array.ts
const INDENTATION_RE = /^\n( +)/;
const unindentTemplate = (
template: TemplateStringsArray,
): { readonly raw: readonly string[] } => {
const indentation = INDENTATION_RE.exec(template.raw[0])?.[1];
return indentation
? { raw: template.raw.map((a) => a.replaceAll(`\n${indentation}`, "\n")) }
: template;
};
@colelawrence
colelawrence / objMap.ts
Last active June 5, 2024 17:39
Record mapping as a map function (e.g. `[P in keyof T]: Transform<T[P]>`)
export function objMap<T extends Record<string, any>, U>(
template: T,
eachKey: <P extends Extract<keyof T, string>>(name: P, value: T[P]) => U,
): {
[P in Extract<keyof T, string>]: U;
} {
// @ts-ignore
return Object.fromEntries(
Object.entries(template)
.filter(([name]) => typeof name === "string")
pub mod prelude {
// some custom proc macros
pub use i_app_types_proc::shared;
// a "concepts" module that is purely for documenting through doc links
pub use crate::concepts;
// other common shared modules re-exported...
pub use crate::runtime;
pub use crate::testing;
@colelawrence
colelawrence / collectAndFlush.ts
Created April 4, 2024 19:50
Add items with a throttled effect of applying the items.
/**
* Add items with a throttled effect of applying the items.
* We might use this to collect items and then apply them in batches like for
* search results and timeline items.
*/
export function collectAndFlush<T>(
then: (items: T[]) => void,
// 60 fps
throttleMs: number = 17,
): {
@colelawrence
colelawrence / fetchProxyProofOfConcept.ts
Created March 25, 2024 14:47
Serializing Requests into a Response
/**
* As a proof of concept,
* JSON'infy the args, and parse them into a new Request Init,
* Make the fetch request with the new Request Init
* JSONify the response and use the jsonified response data
* to recreate the response object and return that
*
*/
const fetchProxyProofOfConcept: typeof fetch = async (...args) => {
import type { Component, DevJSX, JSXNode, JSXOutput } from "@builder.io/qwik";
import { useSignal, useVisibleTask$ } from "@builder.io/qwik";
const QWIK_DATA_ATTR = "data-qwik-inspector";
interface HackedProps {
"data-qwik-inspector"?: DevJSX;
"p:where"?: DevJSX;
}
function createLinkFromDevJSX(dev: DevJSX) {
@colelawrence
colelawrence / warnOnce.ts
Created February 27, 2024 02:46
Warn console messages only once.
const warned = new Set<string>();
export function warnOnce(message: string) {
if (!warned.has(message)) {
warned.add(message);
console.warn(message);
}
}