-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbartender.go
More file actions
407 lines (365 loc) · 13.9 KB
/
bartender.go
File metadata and controls
407 lines (365 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package cooklang
import (
"math"
"strings"
)
// ConversionMode defines how precise unit conversions should be
type ConversionMode int
const (
// PreciseMode uses exact scientific conversions (29.5735 ml/oz)
PreciseMode ConversionMode = iota
// BartenderMode uses practical bartender conversions (30 ml/oz)
// with rounding to friendly values and smart unit selection
BartenderMode
)
// UnitSystemUnknown represents an unknown or undetectable unit system
const UnitSystemUnknown UnitSystem = "unknown"
// Bartender-friendly conversion constants
// These are the practical conversions used by bartenders, not scientific ones
const (
// Volume conversions (bartender-friendly)
MlPerOz = 30.0 // Bartender standard (scientific: 29.5735)
MlPerTbsp = 15.0 // 1 tablespoon
MlPerTsp = 5.0 // 1 teaspoon
MlPerCup = 240.0 // Standard US cup
MlPerDash = 0.92 // ~1ml, standard bar dash
MlPerSplash = 7.5 // Roughly 1/4 oz
MlPerBarspoon = 5.0 // Same as teaspoon
MlPerJigger = 45.0 // Standard jigger (1.5 oz)
MlPerPony = 30.0 // Pony shot (1 oz)
// Scientific conversions for PreciseMode
MlPerOzPrecise = 29.5735
)
// CocktailUnit represents a unit commonly used in cocktail recipes
type CocktailUnit struct {
Name string // Primary name (e.g., "dash")
Aliases []string // Alternative names
MlValue float64 // Value in milliliters
USValue float64 // Value in fluid ounces (for bartender mode)
System UnitSystem // Which system this unit belongs to
IsCocktail bool // True if this is a cocktail-specific unit (dash, splash, etc.)
}
// cocktailUnits defines all cocktail-related units with their conversions
var cocktailUnits = []CocktailUnit{
// Cocktail-specific units (no system - they're universal)
{Name: "dash", Aliases: []string{"dashes"}, MlValue: MlPerDash, USValue: MlPerDash / MlPerOz, IsCocktail: true},
{Name: "splash", Aliases: []string{"splashes"}, MlValue: MlPerSplash, USValue: MlPerSplash / MlPerOz, IsCocktail: true},
{Name: "barspoon", Aliases: []string{"barspoons", "bar spoon", "bar spoons"}, MlValue: MlPerBarspoon, USValue: MlPerBarspoon / MlPerOz, IsCocktail: true},
{Name: "jigger", Aliases: []string{"jiggers"}, MlValue: MlPerJigger, USValue: MlPerJigger / MlPerOz, IsCocktail: true},
{Name: "pony", Aliases: []string{"ponies"}, MlValue: MlPerPony, USValue: MlPerPony / MlPerOz, IsCocktail: true},
// US Volume units
{Name: "fl oz", Aliases: []string{"fluid ounce", "fluid ounces", "fl. oz", "fl. oz."}, MlValue: MlPerOz, USValue: 1, System: UnitSystemUS},
{Name: "oz", Aliases: []string{"ounce", "ounces"}, MlValue: MlPerOz, USValue: 1, System: UnitSystemUS},
{Name: "tbsp", Aliases: []string{"tablespoon", "tablespoons", "T", "Tbsp"}, MlValue: MlPerTbsp, USValue: 0.5, System: UnitSystemUS},
{Name: "tsp", Aliases: []string{"teaspoon", "teaspoons", "t"}, MlValue: MlPerTsp, USValue: MlPerTsp / MlPerOz, System: UnitSystemUS},
{Name: "cup", Aliases: []string{"cups", "c"}, MlValue: MlPerCup, USValue: MlPerCup / MlPerOz, System: UnitSystemUS},
{Name: "quart", Aliases: []string{"quarts", "qt"}, MlValue: 946.0, USValue: 32, System: UnitSystemUS},
{Name: "pint", Aliases: []string{"pints", "pt"}, MlValue: 473.0, USValue: 16, System: UnitSystemUS},
{Name: "gallon", Aliases: []string{"gallons", "gal"}, MlValue: 3785.0, USValue: 128, System: UnitSystemUS},
// Metric Volume units
{Name: "ml", Aliases: []string{"milliliter", "milliliters", "millilitre", "millilitres"}, MlValue: 1, USValue: 1 / MlPerOz, System: UnitSystemMetric},
{Name: "cl", Aliases: []string{"centiliter", "centiliters", "centilitre", "centilitres"}, MlValue: 10, USValue: 10 / MlPerOz, System: UnitSystemMetric},
{Name: "dl", Aliases: []string{"deciliter", "deciliters", "decilitre", "decilitres"}, MlValue: 100, USValue: 100 / MlPerOz, System: UnitSystemMetric},
{Name: "l", Aliases: []string{"liter", "liters", "litre", "litres", "L"}, MlValue: 1000, USValue: 1000 / MlPerOz, System: UnitSystemMetric},
}
// GetCocktailUnit looks up a unit by name (case-insensitive).
// It searches both primary names and aliases for cocktail-related units.
//
// Parameters:
// - name: The unit name to look up (e.g., "oz", "dash", "ml")
//
// Returns:
// - *CocktailUnit: The matching unit info, or nil if not found
//
// Example:
//
// unit := cooklang.GetCocktailUnit("fl oz")
// if unit != nil {
// fmt.Printf("1 %s = %.1f ml\n", unit.Name, unit.MlValue)
// }
func GetCocktailUnit(name string) *CocktailUnit {
normalizedName := strings.ToLower(strings.TrimSpace(name))
for i := range cocktailUnits {
if strings.ToLower(cocktailUnits[i].Name) == normalizedName {
return &cocktailUnits[i]
}
for _, alias := range cocktailUnits[i].Aliases {
if strings.ToLower(alias) == normalizedName {
return &cocktailUnits[i]
}
}
}
return nil
}
// DetectUnitSystemFromUnit determines the unit system from a single unit name.
// Returns UnitSystemUnknown if the unit is not recognized or is cocktail-specific.
//
// Parameters:
// - unitName: The unit to check (e.g., "ml", "oz", "cup")
//
// Returns:
// - UnitSystem: The detected system (UnitSystemMetric, UnitSystemUS, or UnitSystemUnknown)
//
// Example:
//
// system := cooklang.DetectUnitSystemFromUnit("cup")
// // system == UnitSystemUS
func DetectUnitSystemFromUnit(unitName string) UnitSystem {
unit := GetCocktailUnit(unitName)
if unit != nil && unit.System != "" {
return unit.System
}
return UnitSystemUnknown
}
// DetectIngredientListUnitSystem detects the dominant unit system in an ingredient list.
// It counts US vs metric units and returns the more common one.
// Returns UnitSystemUS as default when tied (most cocktail recipes are US-based).
//
// Parameters:
// - il: The ingredient list to analyze
//
// Returns:
// - UnitSystem: The dominant system, or UnitSystemUnknown if no units found
//
// Example:
//
// ingredients := cocktail.GetIngredients()
// system := cooklang.DetectIngredientListUnitSystem(ingredients)
// if system == cooklang.UnitSystemMetric {
// fmt.Println("Recipe uses metric measurements")
// }
func DetectIngredientListUnitSystem(il *IngredientList) UnitSystem {
if il == nil {
return UnitSystemUnknown
}
usCount := 0
metricCount := 0
for _, ing := range il.Ingredients {
system := DetectUnitSystemFromUnit(ing.Unit)
switch system {
case UnitSystemUS:
usCount++
case UnitSystemMetric:
metricCount++
}
}
// Return the dominant system
if usCount > metricCount {
return UnitSystemUS
} else if metricCount > usCount {
return UnitSystemMetric
}
// Default to US if tied (most cocktail recipes are US-based)
if usCount > 0 || metricCount > 0 {
return UnitSystemUS
}
return UnitSystemUnknown
}
// SmartUnitResult contains the result of intelligent unit selection
type SmartUnitResult struct {
Value float64 // The numeric value
Unit string // The selected unit name
Original string // Original formatted value for reference
}
// SelectBestUnit chooses the most appropriate unit for a given volume in ml.
// This is used in bartender mode to pick human-friendly units.
//
// Features:
// - Very small amounts (≤3ml) → dashes
// - Small amounts → barspoons or fractional oz
// - Standard amounts → oz with nice fractions
// - Large amounts → cups
// - Metric: rounds to nearest 5ml for amounts ≥30ml, 2.5ml for smaller amounts
//
// Parameters:
// - mlValue: The volume in milliliters
// - targetSystem: The desired unit system (UnitSystemMetric or UnitSystemUS)
//
// Returns:
// - SmartUnitResult: Contains the converted value and selected unit
//
// Example:
//
// result := cooklang.SelectBestUnit(45, cooklang.UnitSystemUS)
// fmt.Printf("%v %s\n", result.Value, result.Unit) // "1.5 oz"
func SelectBestUnit(mlValue float64, targetSystem UnitSystem) SmartUnitResult {
// For very small amounts (<= 3 ml), use dashes
// This threshold captures amounts like 1/12 oz (2.5ml) = ~3 dashes
if mlValue <= 3 && mlValue > 0 {
dashes := mlValue / MlPerDash
roundedDashes := math.Round(dashes)
if roundedDashes < 1 {
roundedDashes = 1
}
return SmartUnitResult{
Value: roundedDashes,
Unit: "dash",
}
}
if targetSystem == UnitSystemMetric {
return selectBestMetricUnit(mlValue)
}
// For US system (default)
return selectBestUSUnit(mlValue)
}
// selectBestMetricUnit picks the best metric unit for a given ml value.
// Rounds to nearest 5ml for amounts ≥30ml, or nearest 2.5ml for smaller amounts.
func selectBestMetricUnit(mlValue float64) SmartUnitResult {
// Round to nearest 5 for amounts >= 30ml
if mlValue >= 30 {
rounded := math.Round(mlValue/5) * 5
if rounded >= 1000 {
return SmartUnitResult{Value: rounded / 1000, Unit: "l"}
}
if rounded >= 100 {
return SmartUnitResult{Value: rounded / 10, Unit: "cl"}
}
return SmartUnitResult{Value: rounded, Unit: "ml"}
}
// For small amounts, round to nearest 2.5ml
rounded := math.Round(mlValue/2.5) * 2.5
if rounded < 1 {
rounded = mlValue // Keep original if too small
}
return SmartUnitResult{Value: rounded, Unit: "ml"}
}
// selectBestUSUnit picks the best US unit for a given ml value.
// Uses bartender-friendly fractions and appropriate units for the quantity.
func selectBestUSUnit(mlValue float64) SmartUnitResult {
ozValue := mlValue / MlPerOz
// Very small amounts: use dashes (already handled in SelectBestUnit)
if mlValue <= 3 {
dashes := mlValue / MlPerDash
roundedDashes := math.Round(dashes)
if roundedDashes < 1 {
roundedDashes = 1
}
return SmartUnitResult{Value: roundedDashes, Unit: "dash"}
}
// Small amounts (up to ~7ml / 1/4 oz): consider barspoons
if mlValue < 10 {
// Try barspoons for amounts that work well with them
barspoons := mlValue / MlPerBarspoon
if barspoons <= 2 && IsNiceFraction(barspoons, 0.1) {
return SmartUnitResult{Value: RoundToNiceFraction(barspoons, 0.1), Unit: "barspoon"}
}
// Fall back to oz fraction
return SmartUnitResult{Value: RoundToNiceFraction(ozValue, 0.05), Unit: "oz"}
}
// Standard drink amounts: prefer oz with nice fractions
if mlValue <= 240 { // Up to 1 cup
rounded := RoundToNiceFraction(ozValue, 0.05)
return SmartUnitResult{Value: rounded, Unit: "oz"}
}
// Larger amounts: use cups
cups := mlValue / MlPerCup
if cups >= 1 {
rounded := RoundToNiceFraction(cups, 0.05)
return SmartUnitResult{Value: rounded, Unit: "cup"}
}
return SmartUnitResult{Value: RoundToNiceFraction(ozValue, 0.05), Unit: "oz"}
}
// ConvertVolumeBartender converts a volume from one unit to another using bartender-friendly rounding.
// First converts to ml, then selects the best unit in the target system.
//
// Parameters:
// - value: The numeric quantity to convert
// - fromUnit: The source unit (e.g., "oz", "ml")
// - toSystem: The target unit system
//
// Returns:
// - SmartUnitResult: Contains the converted value and selected unit
//
// Example:
//
// result := cooklang.ConvertVolumeBartender(1.5, "oz", cooklang.UnitSystemMetric)
// fmt.Printf("%v %s\n", result.Value, result.Unit) // "45 ml"
func ConvertVolumeBartender(value float64, fromUnit string, toSystem UnitSystem) SmartUnitResult {
// First convert to ml
fromUnitInfo := GetCocktailUnit(fromUnit)
if fromUnitInfo == nil {
// Unknown unit, return as-is
return SmartUnitResult{Value: value, Unit: fromUnit}
}
mlValue := value * fromUnitInfo.MlValue
// Then select the best unit in the target system
return SelectBestUnit(mlValue, toSystem)
}
// FormatBartenderValue formats a SmartUnitResult for display with fractions and pluralization.
// Uses fraction formatting (e.g., "1 1/2" instead of "1.5") and handles unit pluralization.
//
// Parameters:
// - result: The SmartUnitResult to format
//
// Returns:
// - string: Formatted string (e.g., "1 1/2 oz", "3 dashes")
//
// Example:
//
// result := cooklang.SmartUnitResult{Value: 1.5, Unit: "oz"}
// fmt.Println(cooklang.FormatBartenderValue(result)) // "1 1/2 oz"
func FormatBartenderValue(result SmartUnitResult) string {
// Use fraction formatting for nice display
valueStr := FormatAsFractionDefault(result.Value)
// Handle pluralization for certain units
unit := result.Unit
if result.Value != 1 && !strings.HasSuffix(unit, "s") {
// Simple pluralization for common units
switch unit {
case "dash":
unit = "dashes"
case "splash":
unit = "splashes"
case "cup":
unit = "cups"
case "barspoon":
unit = "barspoons"
}
}
return valueStr + " " + unit
}
// IsCocktailSpecificUnit returns true if the unit is cocktail-specific (dash, splash, etc.).
// Cocktail-specific units are universal and should not be converted between systems.
//
// Parameters:
// - unitName: The unit to check
//
// Returns:
// - bool: true if the unit is cocktail-specific
//
// Example:
//
// cooklang.IsCocktailSpecificUnit("dash") // true
// cooklang.IsCocktailSpecificUnit("oz") // false
func IsCocktailSpecificUnit(unitName string) bool {
unit := GetCocktailUnit(unitName)
return unit != nil && unit.IsCocktail
}
// ShouldSkipConversion returns true if unit conversion should be skipped.
// Conversion is skipped when the source unit is already in the target system,
// or when the unit is cocktail-specific (universal units like dash, splash).
//
// Parameters:
// - sourceUnit: The current unit
// - targetSystem: The desired unit system
//
// Returns:
// - bool: true if conversion should be skipped
//
// Example:
//
// cooklang.ShouldSkipConversion("ml", cooklang.UnitSystemMetric) // true (already metric)
// cooklang.ShouldSkipConversion("dash", cooklang.UnitSystemUS) // true (cocktail-specific)
// cooklang.ShouldSkipConversion("ml", cooklang.UnitSystemUS) // false (needs conversion)
func ShouldSkipConversion(sourceUnit string, targetSystem UnitSystem) bool {
sourceSystem := DetectUnitSystemFromUnit(sourceUnit)
// Skip if source system matches target system
if sourceSystem == targetSystem && targetSystem != UnitSystemUnknown {
return true
}
// Skip for cocktail-specific units (they're universal)
if IsCocktailSpecificUnit(sourceUnit) {
return true
}
return false
}