/** * The longest search text the storefront ever sends to the API (CF-084 / S4-07). * * The suggestions endpoint accepted a 4000-character `q` and ran a case-insensitive match over names, * SKUs, descriptions, categories, tags and brands with it. No shopper types 100 characters into a search * box, so the storefront cuts the text before it leaves the browser (suggestions) or reaches the product * list call (`?search=`). The API should cap it too; this is the storefront's half. * * ⛔ Imports nothing, on purpose: `scripts/check-oauth-state.mjs` runs this file as plain TypeScript. */ export const MAX_SEARCH_LENGTH = 100; /** * Cut at {@link MAX_SEARCH_LENGTH} characters (never in the middle of a surrogate pair). Does NOT trim: * the text may be what a shopper is still typing ("red "), and trimming it would eat the space. */ export function clampSearchQuery(raw: string | null | undefined): string { const text = raw ?? ''; if (text.length <= MAX_SEARCH_LENGTH) return text; const cut = text.slice(0, MAX_SEARCH_LENGTH); const last = cut.charCodeAt(cut.length - 1); // A high surrogate at the cut would leave half an emoji. return last >= 0xd800 && last <= 0xdbff ? cut.slice(0, -1) : cut; }