JavaScript · 3620 bytes Raw Blame History
1 import { roastDatabase } from '../data/roastDatabase';
2
3 export const getRoastsForLocation = (location) => {
4 if (!location) return [];
5
6 let roasts = [];
7 const { city, state, country } = location;
8
9 // Normalize location names - convert spaces to underscores and lowercase
10 const normalizeString = (str) => str?.toLowerCase().replace(/\s+/g, '_').replace(/[^\w_]/g, '');
11
12 const cityNormalized = normalizeString(city);
13 const stateNormalized = normalizeString(state);
14 const countryNormalized = normalizeString(country);
15
16 // If only city is provided, search through all states/countries for this city
17 if (cityNormalized && !stateNormalized && !countryNormalized) {
18 for (const [regionKey, regionData] of Object.entries(roastDatabase)) {
19 // Skip the 'default' entry
20 if (regionKey === 'default') continue;
21
22 // Check if this region has the city
23 if (regionData[cityNormalized]) {
24 roasts = roasts.concat(regionData[cityNormalized]);
25 // Also add some generic roasts from the parent region
26 if (regionData.generic) {
27 roasts = roasts.concat(regionData.generic.slice(0, 2)); // Add a couple generic ones
28 }
29 break; // Found the city, stop searching
30 }
31 }
32 }
33
34 // If we have state/country info, use it for more specific matching
35 if (stateNormalized && roastDatabase[stateNormalized]) {
36 // Check for city-specific roasts within the state
37 if (cityNormalized && roastDatabase[stateNormalized][cityNormalized]) {
38 roasts = roasts.concat(roastDatabase[stateNormalized][cityNormalized]);
39 }
40 // Add state generic roasts
41 if (roastDatabase[stateNormalized].generic) {
42 roasts = roasts.concat(roastDatabase[stateNormalized].generic);
43 }
44 }
45
46 // Check for country roasts
47 if (countryNormalized) {
48 const countryMappings = {
49 'united_states': 'usa',
50 'united_states_of_america': 'usa',
51 'us': 'usa',
52 'america': 'usa',
53 'united_kingdom': 'uk',
54 'great_britain': 'uk',
55 'england': 'uk',
56 'scotland': 'uk',
57 'wales': 'uk',
58 'northern_ireland': 'uk'
59 };
60
61 const countryKey = countryMappings[countryNormalized] || countryNormalized;
62 if (roastDatabase[countryKey]?.generic) {
63 roasts = roasts.concat(roastDatabase[countryKey].generic);
64 }
65 }
66
67 // Special handling for common state abbreviations
68 const stateAbbreviations = {
69 'ny': 'newyork',
70 'nyc': 'newyork',
71 'ca': 'california',
72 'tx': 'texas',
73 'fl': 'florida',
74 'il': 'illinois',
75 'pa': 'pennsylvania',
76 'oh': 'ohio',
77 'mi': 'michigan',
78 'ga': 'georgia',
79 'nc': 'north_carolina',
80 'va': 'virginia',
81 'ma': 'massachusetts',
82 'az': 'arizona',
83 'wa': 'washington',
84 'co': 'colorado',
85 'or': 'oregon',
86 'nv': 'nevada',
87 'la': 'louisiana',
88 'wi': 'wisconsin'
89 };
90
91 // If no roasts found yet, check if the input matches a state abbreviation
92 if (roasts.length === 0 && cityNormalized) {
93 const stateKey = stateAbbreviations[cityNormalized];
94 if (stateKey && roastDatabase[stateKey]?.generic) {
95 roasts = roasts.concat(roastDatabase[stateKey].generic);
96 }
97 }
98
99 // If still no roasts, check if the city input might actually be a state name
100 if (roasts.length === 0 && cityNormalized && roastDatabase[cityNormalized]?.generic) {
101 roasts = roasts.concat(roastDatabase[cityNormalized].generic);
102 }
103
104 // If no specific roasts found, use defaults
105 if (roasts.length === 0) {
106 roasts = roastDatabase.default || [];
107 }
108
109 // Remove duplicates
110 return [...new Set(roasts)];
111 };