115 lines
4.9 KiB
TypeScript
115 lines
4.9 KiB
TypeScript
/**
|
|
* ANNOTATED COPY — every line explained
|
|
* Source: src/lib/customization.ts
|
|
* NOT used by the app — read this to learn how the real file works
|
|
*/
|
|
// Import project module (@/ alias = src/ folder in tsconfig)
|
|
import { Category, MeatCustomization, ProductCustomization } from '@/types';
|
|
// (blank line — separates logical blocks for readability)
|
|
// TypeScript type alias — union or shorthand for complex types
|
|
type Translator = (path: string, params?: Record<string, string | number>) => string;
|
|
// (blank line — separates logical blocks for readability)
|
|
// Named export — utility function other files can import
|
|
export function getDefaultCustomization(category: Category): ProductCustomization {
|
|
// Conditional branch — different behavior based on runtime value
|
|
if (category === 'fish') {
|
|
// Return value from function
|
|
return { type: 'fish' };
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|
|
// Return value from function
|
|
return {
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
type: category,
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
cuts: 8,
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
cuttingStyle: 'karahi',
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
};
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|
|
// (blank line — separates logical blocks for readability)
|
|
// Named export — utility function other files can import
|
|
export function getCustomizationKey(customization: ProductCustomization): string {
|
|
// Conditional branch — different behavior based on runtime value
|
|
if (customization.type === 'fish') {
|
|
// Return value from function
|
|
return 'standard';
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|
|
// Return value from function
|
|
return `cuts-${customization.cuts}::${customization.cuttingStyle}`;
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|
|
// (blank line — separates logical blocks for readability)
|
|
// Named export — utility function other files can import
|
|
export function getCustomizationLabel(
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
customization: ProductCustomization,
|
|
// Line 25: t: Translator
|
|
t: Translator
|
|
// Line 26: ): string {
|
|
): string {
|
|
// Conditional branch — different behavior based on runtime value
|
|
if (customization.type === 'fish') {
|
|
// Return value from function
|
|
return t('product.standardCut');
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|
|
// Return value from function
|
|
return t('product.cutsAndStyle', {
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
cuts: customization.cuts,
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
style: t(`cutting.${customization.cuttingStyle}`),
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
});
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|
|
// (blank line — separates logical blocks for readability)
|
|
// Named export — utility function other files can import
|
|
export function getCartItemKey(
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
productId: string,
|
|
// Line 38: customization: ProductCustomization
|
|
customization: ProductCustomization
|
|
// Line 39: ): string {
|
|
): string {
|
|
// Return value from function
|
|
return `${productId}::${getCustomizationKey(customization)}`;
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|
|
// (blank line — separates logical blocks for readability)
|
|
// Named export — utility function other files can import
|
|
export function updateMeatCustomization(
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
current: ProductCustomization,
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
category: Category,
|
|
// Line 46: update: Partial<Pick<MeatCustomization, 'cuts' | 'cutting...
|
|
update: Partial<Pick<MeatCustomization, 'cuts' | 'cuttingStyle'>>
|
|
// Line 47: ): MeatCustomization {
|
|
): MeatCustomization {
|
|
// Line 48: const base =
|
|
const base =
|
|
// Line 49: current.type !== 'fish' && current.type === category
|
|
current.type !== 'fish' && current.type === category
|
|
// Line 50: ? current
|
|
? current
|
|
// Line 51: : (getDefaultCustomization(category) as MeatCustomization);
|
|
: (getDefaultCustomization(category) as MeatCustomization);
|
|
// (blank line — separates logical blocks for readability)
|
|
// Return value from function
|
|
return {
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
type: category as MeatCustomization['type'],
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
cuts: update.cuts ?? base.cuts,
|
|
// Property or array item — trailing comma allowed in TypeScript
|
|
cuttingStyle: update.cuttingStyle ?? base.cuttingStyle,
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
};
|
|
// Closing brace — end of block (function, if, object, JSX)
|
|
}
|