Skip to content

Instantly share code, notes, and snippets.

View philwolstenholme's full-sized avatar

Phil Wolstenholme philwolstenholme

View GitHub Profile
import { UAParser } from "ua-parser-js";
/**
* Uses the `Sec-CH-UA-Mobile` header if it's available to work out how many
* items to show per page.
*/
export const getItemsPerPage = ({
astroRequest,
mobileItemsPerPage = 5,
desktopItemsPerPage = 12,
@philwolstenholme
philwolstenholme / replaceClassNames.js
Created October 21, 2025 11:51
Disclosure: this is from GTP-5 but I've tested it in a browser
/**
* Replace text inside class attributes across the DOM (allows partial matches).
*
* @param {string} find - Text to find inside class attribute (partial allowed).
* @param {string} replaceWith - Replacement text.
* @param {Object} [options]
* @param {HTMLElement|Document|Element} [options.scope=document] - Limit to a subtree.
* @returns {number} Count of elements that were modified.
*/
function replaceClassNames(find, replaceWith, { scope = document } = {}) {
@philwolstenholme
philwolstenholme / renderServerComponent.ts
Created November 20, 2025 11:20 — forked from sroebert/renderServerComponent.ts
A (hacky) way to render server components for React testing library.
import type { PropsWithChildren, ReactElement, ReactNode } from 'react';
import React, { Children, cloneElement, isValidElement } from 'react';
import { render } from '@testing-library/react';
function setFakeReactDispatcher<T>(action: () => T): T {
/**
* We use some internals from React to avoid a lot of warnings in our tests when faking
* to render server components. If the structure of React changes, this function should still work,
* but the tests will again print warnings.
@philwolstenholme
philwolstenholme / renderServerComponent.ts
Created November 20, 2025 11:20 — forked from sroebert/renderServerComponent.ts
A (hacky) way to render server components for React testing library.
import type { PropsWithChildren, ReactElement, ReactNode } from 'react';
import React, { Children, cloneElement, isValidElement } from 'react';
import { render } from '@testing-library/react';
function setFakeReactDispatcher<T>(action: () => T): T {
/**
* We use some internals from React to avoid a lot of warnings in our tests when faking
* to render server components. If the structure of React changes, this function should still work,
* but the tests will again print warnings.