/** * Pure pie helpers: input cleanup, limits, and the cute look (emoji + filling color) * picked from the pie's name. No runtime imports so tests can load it directly. */ export const LIMITS = { who: 60, pie: 80, note: 140, title: 60, blurb: 280, place: 80, code: 60, maxPies: 200, batch: 6, hosts: 20 } as const export type PieInput = { who: string; pie: string; note: string } export type PieLook = { emoji: string; filling: string; label: string } const LOOKS: Array<{ test: RegExp; look: PieLook }> = [ { test: /pumpkin/, look: { emoji: '🍂', filling: '#e07b2a', label: 'pumpkin' } }, { test: /sweet ?potato|yam/, look: { emoji: '🍠', filling: '#d9773c', label: 'sweet potato' } }, { test: /coconut/, look: { emoji: '🥥', filling: '#f4eee2', label: 'coconut' } }, { test: /pecan|walnut|nut/, look: { emoji: '🌰', filling: '#8a4f27', label: 'pecan' } }, { test: /apple/, look: { emoji: '🍎', filling: '#e9b55a', label: 'apple' } }, { test: /cherr/, look: { emoji: '🍒', filling: '#c8283f', label: 'cherry' } }, { test: /strawberr/, look: { emoji: '🍓', filling: '#e0475b', label: 'strawberry' } }, { test: /blueberr|huckleberr|marionberr|mixed berr|triple berr|berry/, look: { emoji: '🫐', filling: '#4b4f9e', label: 'berry' } }, { test: /peach|apricot/, look: { emoji: '🍑', filling: '#f4a261', label: 'peach' } }, { test: /key ?lime|lime/, look: { emoji: '🍈', filling: '#b9d86a', label: 'key lime' } }, { test: /lemon/, look: { emoji: '🍋', filling: '#f5d547', label: 'lemon' } }, { test: /banana/, look: { emoji: '🍌', filling: '#f3dc8b', label: 'banana cream' } }, { test: /chocolate|cocoa|french silk|mud|oreo|s.?mores/, look: { emoji: '🍫', filling: '#5a3221', label: 'chocolate' } }, { test: /rhubarb/, look: { emoji: '🌿', filling: '#d1495b', label: 'rhubarb' } }, { test: /pear/, look: { emoji: '🍐', filling: '#cfd67a', label: 'pear' } }, { test: /grape/, look: { emoji: '🍇', filling: '#6a3d8f', label: 'grape' } }, { test: /cheese ?cake|cream ?cheese/, look: { emoji: '🍰', filling: '#f6e7c8', label: 'cheesecake' } }, { test: /caramel|butterscotch|toffee/, look: { emoji: '🍯', filling: '#c98a3a', label: 'caramel' } }, { test: /cream|custard|chess|sugar|buttermilk|meringue/, look: { emoji: '🍦', filling: '#f7e3b0', label: 'cream' } }, { test: /pot ?pie|chicken|shepherd|quiche|meat|turkey/, look: { emoji: '🍗', filling: '#d9a45b', label: 'savory' } }, { test: /pizza/, look: { emoji: '🍕', filling: '#e4572e', label: 'pizza' } }, ] const DEFAULT_LOOK: PieLook = { emoji: '🥧', filling: '#e5a65a', label: 'pie' } /** Pick an emoji and filling color for a pie by its name. */ export function pieLook(name: string): PieLook { const value = String(name ?? '').toLowerCase() for (const entry of LOOKS) if (entry.test.test(value)) return entry.look return DEFAULT_LOOK } /** Trim, collapse whitespace, drop control characters, and cap length. */ export function cleanText(value: unknown, max: number): string { return String(value ?? '') .normalize('NFC') .replace(/[\u0000-\u001f\u007f-\u009f\u200b-\u200f\u202a-\u202e\u2066-\u2069]/g, ' ') .replace(/\s+/g, ' ') .trim() .slice(0, max) } /** Validate a pie form. Returns the cleaned pie or a friendly error. */ export function parsePieInput(body: unknown): { ok: true; value: PieInput } | { ok: false; error: string } { const raw = (body && typeof body === 'object' ? body : {}) as Record const tooLong = (key: 'who' | 'pie' | 'note') => String(raw[key] ?? '').trim().length > LIMITS[key] * 2 if (tooLong('who') || tooLong('pie') || tooLong('note')) return { ok: false, error: 'That is a little too long. Try something shorter.' } const who = cleanText(raw.who, LIMITS.who) const pie = cleanText(raw.pie, LIMITS.pie) const note = cleanText(raw.note, LIMITS.note) if (!who) return { ok: false, error: 'Please add your name or family name.' } if (!pie) return { ok: false, error: 'What pie are you bringing?' } return { ok: true, value: { who, pie, note } } } /** * Validate a batch: one name plus up to LIMITS.batch pies (`{ who, pies: [{ pie, note }] }`). * A single `{ who, pie, note }` is also accepted. Rows with no pie and no note are skipped. */ export function parsePieBatch(body: unknown): { ok: true; value: PieInput[] } | { ok: false; error: string } { const raw = (body && typeof body === 'object' ? body : {}) as Record const rows = Array.isArray(raw.pies) ? raw.pies : [{ pie: raw.pie, note: raw.note }] if (rows.length > LIMITS.batch * 2) return { ok: false, error: `That is a lot of pie! Add up to ${LIMITS.batch} at a time.` } const filled = rows.filter((row) => { const r = (row && typeof row === 'object' ? row : {}) as Record return String(r.pie ?? '').trim() || String(r.note ?? '').trim() }) if (!filled.length) return { ok: false, error: 'What pie are you bringing?' } if (filled.length > LIMITS.batch) return { ok: false, error: `That is a lot of pie! Add up to ${LIMITS.batch} at a time.` } const out: PieInput[] = [] for (const row of filled) { const r = row as Record const parsed = parsePieInput({ who: raw.who, pie: r.pie, note: r.note }) if (!parsed.ok) return parsed out.push(parsed.value) } return { ok: true, value: out } } /** True when a bot filled the hidden honeypot field. */ export function isHoneypotFilled(body: unknown): boolean { const raw = (body && typeof body === 'object' ? body : {}) as Record return String(raw.website ?? '').trim().length > 0 } /** * Optional: send visitors of the bare home page (/) straight to one event. * One package storage setting (no code change). Turn it on or off with the * `./settings` export: setHomeRedirect({ code: 'your-code' }) or ({ code: null }). */ export const HOME_REDIRECT_KEY = 'home_redirect' /** Optional color themes for an event page and its share image. */ export const THEMES = { harvest: { label: 'Harvest orange', ogShadow: '#d4622a', ogDot: '#e07b2a', ogChipText: '#7a2f14', ogKicker: '#f6c177' }, berry: { label: 'Berry pink', ogShadow: '#c2416b', ogDot: '#e0578a', ogChipText: '#6b1d3a', ogKicker: '#f7b6c8' }, evergreen: { label: 'Evergreen', ogShadow: '#3f8f5a', ogDot: '#56b077', ogChipText: '#1f4a2e', ogKicker: '#b9e3c3' }, } as const export type ThemeName = keyof typeof THEMES export function themeName(value: unknown): ThemeName { return typeof value === 'string' && value in THEMES ? (value as ThemeName) : 'harvest' } /** Event details. `night` scopes the pie list; a reset starts a new night id. */ export type EventInfo = { slug: string code: string title: string date: string time: string place: string blurb: string theme: ThemeName night: string creator: string hosts: string[] version: number } export const DEFAULT_BLURB = "Thanks for helping with pies! Let us know what you're bringing." /** Paths under the app root that can never be an event code. */ const RESERVED = new Set(['new', 'api', 'admin', 'e', 'ws', 'og', 'sw', 'assets', '_assets', 'manifest', 'favicon', 'robots', 'offline', 'mine', 'login']) /** * Event codes are forgiving: "Smith Family 2026", "smith_family_2026" * and "SMITH-FAMILY-2026" all become "smith-family-2026". */ export function slugify(value: unknown): string { return String(value ?? '') .normalize('NFKD') .replace(/[\u0300-\u036f]/g, '') .toLowerCase() .replace(/['\u2019]/g, '') .replace(/[^a-z0-9]+/g, '-') .replace(/^-+|-+$/g, '') .slice(0, LIMITS.code) .replace(/-+$/g, '') } export function validSlug(slug: string): boolean { return /^[a-z0-9](?:[a-z0-9-]{0,58}[a-z0-9])?$/.test(slug) && !RESERVED.has(slug) } export function cleanUsername(value: unknown): string { const name = String(value ?? '').trim().replace(/^@/, '').toLowerCase() return /^[a-z0-9][a-z0-9_.-]{0,62}$/.test(name) ? name : '' } export function cleanHosts(list: unknown, creator = ''): string[] { const raw = Array.isArray(list) ? list : String(list ?? '').split(/[\s,]+/) return [...new Set(raw.map(cleanUsername).filter((n) => n && n !== creator))].slice(0, LIMITS.hosts) } type EventFields = Pick /** Validate event details from the create form or the host page. Date is YYYY-MM-DD, time is HH:MM (24h). */ export function parseEventInput(body: unknown, current?: EventFields): { ok: true; value: EventFields } | { ok: false; error: string } { const raw = (body && typeof body === 'object' ? body : {}) as Record const title = cleanText(raw.title ?? current?.title, LIMITS.title) const blurb = cleanText(raw.blurb ?? current?.blurb ?? '', LIMITS.blurb) || (current ? '' : DEFAULT_BLURB) const place = cleanText(raw.place ?? current?.place ?? '', LIMITS.place) const date = String(raw.date ?? current?.date ?? '').trim() const time = String(raw.time ?? current?.time ?? '').trim() const theme = themeName(raw.theme ?? current?.theme) if (!title) return { ok: false, error: 'Give the night a title.' } if (!/^\d{4}-\d{2}-\d{2}$/.test(date) || Number.isNaN(Date.parse(`${date}T12:00:00Z`))) return { ok: false, error: 'Pick a date.' } if (!/^([01]\d|2[0-3]):[0-5]\d$/.test(time)) return { ok: false, error: 'Pick a time.' } return { ok: true, value: { title, blurb, place, date, time, theme } } } /** "Friday, November 27" and "6 PM" for a date + time. */ export function formatEventWhen(event: Pick): { day: string; time: string; short: string; medium: string } { const [y, m, d] = event.date.split('-').map(Number) const [hh, mm] = event.time.split(':').map(Number) const noon = new Date(Date.UTC(y!, m! - 1, d!, 12)) const day = new Intl.DateTimeFormat('en-US', { weekday: 'long', month: 'long', day: 'numeric', timeZone: 'UTC' }).format(noon) const short = new Intl.DateTimeFormat('en-US', { weekday: 'short', month: 'short', day: 'numeric', timeZone: 'UTC' }).format(noon) const medium = new Intl.DateTimeFormat('en-US', { weekday: 'long', month: 'short', day: 'numeric', timeZone: 'UTC' }).format(noon) const h12 = ((hh! + 11) % 12) + 1 const time = `${h12}${mm ? `:${String(mm).padStart(2, '0')}` : ''} ${hh! < 12 ? 'AM' : 'PM'}` return { day, time, short, medium } }