/** * The name customers see for a courier service at checkout. Mirrors * includes/Shipping/CustomerServiceName.php (keep the two in step). * * Courier service names describe how the merchant hands the parcel over ("Collection", "Drop Off", * "ParcelShop", "Lockers to Home"), which reads to a customer as if they must collect it. */ /** Brand found in the courier (or service) name, and the brand shown. */ const BRANDS: [RegExp, string][] = [ [/royal\s*mail/i, 'Royal Mail'], [/parcelforce/i, 'Parcelforce'], [/evri|hermes/i, 'Evri'], [/\bdhl\b/i, 'DHL'], [/\bups\b/i, 'UPS'], [/fedex/i, 'FedEx Express'], [/inpost/i, 'InPost'], [/\bdpd\b/i, 'DPD'], [/yodel/i, 'Yodel'], [/\bdx/i, 'DX'], ]; /** Brands whose services go by the brand and speed tier only: FedEx has Priority (express) and Economy. */ const BRAND_ONLY = ['FedEx Express']; /** Words about how the merchant sends the parcel, or its size band: not for customers. */ const SENDER_WORDS: RegExp[] = [ /\blockers?\s+to\s+(home|door)\b/gi, /\bshop\s+to\s+(home|door)\b/gi, /\bto\s+(home|door)\b/gi, /\bto\s+(parcel\s*shops?|lockers?)\b/gi, /\bdrop[\s-]*off\b/gi, /\bdrop[\s-]*shops?\b/gi, /\bparcel\s*shops?\b/gi, /\baccess\s+point\b/gi, /\bcollection\b/gi, /\blockers?\b/gi, /\bworldwide\b/gi, /\becommerce\b/gi, /\buk\b/gi, /\bsmall\b/gi, /\bmedium\b/gi, /\bservice\b/gi, /[®™]/g, ]; function brandOf(text: string): string | null { const found = BRANDS.find(([pattern]) => pattern.test(text)); return found ? found[1] : null; } /** * Customer-facing name for a service. * * @param service Service: courier, and name with the courier already taken out. * @param tags Quote tags; "shopdelivery" means the customer collects from a pickup point, a * locker when the quote is also tagged "locker". * @return Name customers see. */ export function customerServiceName( service: { courier?: string; name?: string; slug?: string }, tags: string[] = [] ): string { const courier = (service.courier ?? '').trim(); const name = (service.name ?? '').trim(); const pickup = pickupNote(tags); const brand = brandOf(courier) ?? brandOf(name) ?? courier; if (BRAND_ONLY.includes(brand)) { const tier = /\beconomy\b/i.test(name) ? 'FedEx Economy' : brand; return tier + pickup; } let what = name; BRANDS.forEach(([pattern]) => { what = what.replace(new RegExp(pattern.source, 'gi'), ' '); }); SENDER_WORDS.forEach((pattern) => { what = what.replace(pattern, ' '); }); what = what .replace(/\s+/g, ' ') .replace(/\s+([,.])/g, '$1') .replace(/^[\s\-–—,:;/]+|[\s\-–—,:;/]+$/g, ''); // Don't repeat what the brand already says: "FedEx Express" + "Express Saver" is "FedEx Express Saver". brand.split(' ').forEach((word) => { if (word && `${what} `.toLowerCase().startsWith(`${word.toLowerCase()} `)) { what = what.slice(word.length).trimStart(); } }); if (brand === '' && what === '') return service.slug ?? ''; // "Standard" only stands in for a missing name; a pickup point service says how it arrives instead. if (what === '' && pickup === '') what = 'Standard'; const label = (`${brand} ${what}`.trim() + pickup).trim(); return label || (service.slug ?? ''); } /** * What a pickup point service adds to the name: " Locker Pick-up" or " Shop Pick-up"; '' otherwise. * * @param tags Quote tags. * @return Note to add. */ function pickupNote(tags: string[]): string { if (!tags.includes('shopdelivery')) return ''; return tags.includes('locker') ? ' Locker Pick-up' : ' Shop Pick-up'; } /** * Quote tags for a saved service's pickup kind, for customerServiceName. * * @param pickup '' (home delivery), 'locker' or 'shop'. * @return Tags. */ export function pickupTags(pickup?: string): string[] { if (pickup === 'locker') return ['shopdelivery', 'locker']; if (pickup === 'shop') return ['shopdelivery']; return []; } /** * A quote's pickup kind, saved with the service: 'locker' or 'shop' for pickup point services * (tag "shopdelivery"), '' for home delivery. * * @param tags Quote tags. * @return Pickup kind. */ export function pickupKind(tags: string[] = []): '' | 'locker' | 'shop' { if (!tags.includes('shopdelivery')) return ''; return tags.includes('locker') ? 'locker' : 'shop'; }