Skip to content

Commit 26ffaf6

Browse files
Copilotmaehr
andcommitted
Refactor normalization logic: remove hardcoded case statements, use helper functions
Co-authored-by: maehr <[email protected]>
1 parent 1dc20c0 commit 26ffaf6

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

utils/sortJSON.js

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@ import fs from 'fs';
22

33
const filepath = 'src/lib/data/entries.json';
44

5+
// Language code mappings for non-standard to ISO codes
6+
const languageCodeMappings = {
7+
dendi: 'ddn'
8+
};
9+
10+
// Helper function to capitalize region names properly
11+
const capitalizeRegionName = (regionName) => {
12+
return regionName
13+
.toLowerCase()
14+
.split(' ')
15+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
16+
.join(' ');
17+
};
18+
19+
// Helper function to normalize language codes
20+
const normalizeLanguageCode = (languageCode) => {
21+
const lowercase = languageCode.toLowerCase();
22+
return languageCodeMappings[lowercase] || lowercase;
23+
};
24+
525
// Define allowed values for validation
626
const allowedValues = {
727
region: [
@@ -148,47 +168,10 @@ fs.readFile(filepath, 'utf-8', (err, data) => {
148168
.map((value) => {
149169
if (fieldName === 'region') {
150170
// Region-specific normalizations - capitalize country/region names
151-
const lowercaseValue = value.toLowerCase();
152-
switch (lowercaseValue) {
153-
case 'africa':
154-
return 'Africa';
155-
case 'asia':
156-
return 'Asia';
157-
case 'austria':
158-
return 'Austria';
159-
case 'europe':
160-
return 'Europe';
161-
case 'france':
162-
return 'France';
163-
case 'germany':
164-
return 'Germany';
165-
case 'global':
166-
return 'Global';
167-
case 'great britain':
168-
return 'Great Britain';
169-
case 'netherlands':
170-
return 'Netherlands';
171-
case 'north america':
172-
return 'North America';
173-
case 'switzerland':
174-
return 'Switzerland';
175-
default:
176-
// Capitalize first letter of each word for unknown regions
177-
return value
178-
.toLowerCase()
179-
.split(' ')
180-
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
181-
.join(' ');
182-
}
171+
return capitalizeRegionName(value);
183172
} else if (fieldName === 'language') {
184173
// Language-specific normalizations - convert to proper ISO codes
185-
const lowercaseValue = value.toLowerCase();
186-
switch (lowercaseValue) {
187-
case 'dendi':
188-
return 'ddn'; // ISO 639-3 code for Dendi
189-
default:
190-
return lowercaseValue; // Keep other language codes lowercase
191-
}
174+
return normalizeLanguageCode(value);
192175
} else if (fieldName === 'type') {
193176
// Type-specific normalizations - keep lowercase
194177
return value.toLowerCase();

0 commit comments

Comments
 (0)