To transform a string in a browser environment such that all characters are uppercase and accents are removed, we can utilize JavaScript's built-in String.prototype.normalize() method along with a regular expression to strip out combining diacritical marks.
This approach ensures that accented characters like é, É, ç, ñ, ö, etc., are transformed into their unaccented uppercase counterparts like E, C, N, O, etc.
✅ Key Steps in the Transformation:
Normalize the string using normalize('NFD')
This decomposes characters into their base character and combining diacritical marks (e.g., é becomes e + ´).
Remove all combining diacritics
These are in the Unicode range \u0300 to \u036f. We use a regular expression to filter them out.
Convert the result to uppercase
This ensures the final string is fully uppercase.
This solution assumes modern browser support (which includes all major browsers like Chrome, Firefox, Safari, Edge).
It works well for Latin-based scripts and common accented characters.
If you need broader language support or handling of rare characters, consider using a library like unorm or latinize.
✅ Summary:
This TypeScript function is concise, efficient, and leverages standard browser APIs to handle the transformation of accented characters into uppercase unaccented equivalents. It's ideal for use in form validation, search normalization, or any case-insensitive string comparison scenario.
User
Great, in svelte 5 with runes mode I want to do that:
États-Unis (+1)
To implement the transformation logic in a Svelte 5 component using runes mode, where you want to uppercase and remove accents from country names in a <select> dropdown, follow this structured and efficient approach.
✅ Goal
Transform strings like "États-Unis (+1)" into "ETATS-UNIS (+1)" — keeping the phone code intact while ensuring the country name is properly normalized and capitalized.
This is useful for consistent UI display and sorting, especially when grouping countries by their first letter.
📌 Key Concepts
Svelte 5 Runes Mode: Uses $state and $derived for reactivity.
String Normalization: Uses normalize('NFD') to decompose characters.
Accent Removal: Uses regex to strip diacritics (\u0300-\u036f).
Dynamic Option Rendering: Map over a list of countries and apply transformation.
🧠 Strategy
Define a list of country data (name, code, iso).
Create a removeAccentsAndUppercase function.
Use $derived to create a grouped list of countries by the first letter of the transformed name.
Render <optgroup> and <option> dynamically based on the grouped data.
💡 Svelte 5 Component (Runes Mode)
svelte
<script lang="ts">
import { $state, $derived } from 'svelte';
// Reactive country data
const $countries = $state([
{ iso: 'US', name: 'États-Unis', code: '+1' },
{ iso: 'FR', name: 'France', code: '+33' },
{ iso: 'CA', name: 'Canada', code: '+1' },
{ iso: 'ES', name: 'España', code: '+34' },
{ iso: 'DE', name: 'Deutschland', code: '+49' },
]);
// Transformation function
function removeAccentsAndUppercase(input: string): string {
return input
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '')
.toUpperCase();
}
// Derived grouped data
const $groupedCountries = $derived(() => {
const grouped: Record<string, Array<typeof $countries[0]>> = {};
for (const country of $countries) {
const transformedName = removeAccentsAndUppercase(country.name);
const firstLetter = transformedName.charAt(0);
if (!grouped[firstLetter]) {
grouped[firstLetter] = [];
}
grouped[firstLetter].push({
...country,
displayText: `${transformedName} (${country.code})`
});
}
return Object.entries(grouped).sort(([a], [b]) => a.localeCompare(b));
});
// Reactive selected value
const $countryIso2 = $state('FR');
</script>
<!-- Select element with dynamic optgroups and options -->
<select bind:value={$countryIso2}>
{#each $groupedCountries as [letter, countries]}
<optgroup label={letter}>
{#each countries as country}
<option value={country.iso}>{country.displayText}</option>
{/each}
</optgroup>
{/each}
</select>