Skip to content

Instantly share code, notes, and snippets.

@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);
}
}
@colelawrence
colelawrence / Prettify.d.ts
Created February 24, 2024 01:25
Improve preview of union types in autocomplete
/** Improve preview of union types in autocomplete. */
export type Prettify<T> = { [K in keyof T]: T[K] } & {};
@colelawrence
colelawrence / obfuscate.js
Created February 21, 2024 20:32
Obfuscate secure tokens retaining the shape and scale (Raycast)
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Obfuscate
// @raycast.mode compact
// Optional parameters:
// @raycast.icon 🤖
// @raycast.argument1 { "type": "text", "placeholder": "Original" }
const MINUTE = 1000 * 60;
function asyncCachedFn<Args extends any[], Result>(options: {
fn: (...args: Args) => Promise<Result>;
key: (...args: Args) => string;
}) {
const cache: Map<
string,
{ computedAt: number; evict: any; result: Promise<Result> }
@colelawrence
colelawrence / dev-error.ts
Last active January 11, 2024 21:32
DevError removes irrelevant stack trace lines
// regexes to remove lines from thrown error stacktraces
const AT_NODE_INTERNAL_RE = /^\s*at.+node:internal.+/gm;
const AT_TYPES_INTERNAL_RE = /^\s*at.+\/types\/src\/.+/gm;
const AT_INVARIANT_RE = /^\s*(at|[^@]+@) (?:Object\.)?invariant.+/gm;
const AT_INVARIANT_MORE_RE = /^\s*at.+(?:invariant|asError|createErrorObj).+/gm;
const AT_TEST_HELPERS_RE = /^\s*(at|[^@]+@).+test\-helpers.+/gm;
// const AT_WEB_MODULES = /^\s*(at|[^@]+@).+(web_modules|\-[a-f0-9]{8}\.js).*/gm
const AT_ASSORTED_HELPERS_RE =
/^\s*(at|[^@]+@).+(debounce|invariant|iife)\.[tj]s.*/gm;
@colelawrence
colelawrence / qwik-tw-styled-components.tsx
Last active January 2, 2024 18:19
Qwik styled-components with Tailwind merge. To remix for your codebase at your option! Also supports `data-qwik-inspector` pasthrough.
import { Slot, component$ } from "@builder.io/qwik";
import type { JSX } from "@builder.io/qwik/jsx-runtime";
import { twMerge } from "tailwind-merge";
/** Comes from {@link tw} template. */
type TwString = string & {
_tw: true;
};
/** This applies types to somethign like `tw.div` or `tw.h1`, which will leverage the types from `JSX.IntrinsicElements` */
@colelawrence
colelawrence / useFuseSearch.ts
Created December 28, 2023 14:26
Qwik hook for searching items with Fuse.js
import type { NoSerialize, Signal } from "@builder.io/qwik";
import { noSerialize, useSignal, useVisibleTask$ } from "@builder.io/qwik";
import type { IFuseOptions } from "fuse.js";
import Fuse from "fuse.js";
export function useFuseSearch<T>(
config: {
items: Signal<T[]> | T[];
search: Signal<string> | string;
},