@immicore/immi-tools 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -184,28 +184,48 @@ function getCurrentPeriodRate(rates) {
|
|
|
184
184
|
// (This handles the case where data hasn't been updated yet)
|
|
185
185
|
return rates.length > 0 ? rates[rates.length - 1] : null;
|
|
186
186
|
}
|
|
187
|
+
/**
|
|
188
|
+
* Normalize string by removing accents/diacritics
|
|
189
|
+
* e.g., "Montréal" → "montreal", "Québec" → "quebec", "Trois-Rivières" → "trois-rivieres"
|
|
190
|
+
*/
|
|
191
|
+
function normalizeString(str) {
|
|
192
|
+
return str
|
|
193
|
+
.toLowerCase()
|
|
194
|
+
.trim()
|
|
195
|
+
.normalize('NFD')
|
|
196
|
+
.replace(/[\u0300-\u036f]/g, ''); // Remove combining diacritical marks
|
|
197
|
+
}
|
|
187
198
|
/**
|
|
188
199
|
* Find CMA matching the given location string
|
|
189
|
-
* Supports fuzzy matching
|
|
200
|
+
* Supports fuzzy matching and accent-insensitive search
|
|
190
201
|
*/
|
|
191
202
|
function findMatchingCMA(data, location) {
|
|
192
203
|
const searchLower = location.toLowerCase().trim();
|
|
193
|
-
|
|
204
|
+
const searchNormalized = normalizeString(location);
|
|
205
|
+
// 1. Exact match on full CMA name (case-insensitive)
|
|
194
206
|
const exactMatch = data.find(d => d.cma.toLowerCase() === searchLower);
|
|
195
207
|
if (exactMatch)
|
|
196
208
|
return exactMatch;
|
|
197
|
-
// 2. Exact match on city name
|
|
209
|
+
// 2. Exact match on city name (case-insensitive)
|
|
198
210
|
const cityMatch = data.find(d => d.city.toLowerCase() === searchLower);
|
|
199
211
|
if (cityMatch)
|
|
200
212
|
return cityMatch;
|
|
201
|
-
// 3.
|
|
202
|
-
const
|
|
203
|
-
|
|
213
|
+
// 3. Normalized match (accent-insensitive) on CMA name
|
|
214
|
+
const normalizedCmaMatch = data.find(d => normalizeString(d.cma) === searchNormalized);
|
|
215
|
+
if (normalizedCmaMatch)
|
|
216
|
+
return normalizedCmaMatch;
|
|
217
|
+
// 4. Normalized match (accent-insensitive) on city name
|
|
218
|
+
const normalizedCityMatch = data.find(d => normalizeString(d.city) === searchNormalized);
|
|
219
|
+
if (normalizedCityMatch)
|
|
220
|
+
return normalizedCityMatch;
|
|
221
|
+
// 5. City starts with search term (normalized)
|
|
222
|
+
const startsWithMatch = data.find(d => normalizeString(d.city).startsWith(searchNormalized) ||
|
|
223
|
+
normalizeString(d.cma).startsWith(searchNormalized));
|
|
204
224
|
if (startsWithMatch)
|
|
205
225
|
return startsWithMatch;
|
|
206
|
-
//
|
|
207
|
-
const containsMatch = data.find(d => d.cma
|
|
208
|
-
d.city
|
|
226
|
+
// 6. CMA contains search term (normalized)
|
|
227
|
+
const containsMatch = data.find(d => normalizeString(d.cma).includes(searchNormalized) ||
|
|
228
|
+
normalizeString(d.city).includes(searchNormalized));
|
|
209
229
|
if (containsMatch)
|
|
210
230
|
return containsMatch;
|
|
211
231
|
return null;
|