Search code examples
javascriptnode.jslocalizationinternationalization

Efficient way to use locale sensitive strings as keys to a data structure


What is the most efficient way to key a data-structure using locale sensitive strings.

I essentially want a sorted TreeMap with the comparison algorithm for the sort being a.localeCompare(b, locale, { sensitivity: 'base' }).

The ideal method would be to have some sort of normalize/conversion such that a.convert() === b.convert() for all a and b where a.localeCompare(b, ...) returns 0. Then I could use the result of that conversion as the key for a hashMap like javascript objects or Map.

Alternatively is there a known sorted TreeMap where I could specify the comparison of the keys?


Solution

  • I used npm parameterize library to create a locale insensitive key. The function provided takes a utf-8 string and turns it into a relatively readable lowercase string. This then can be used for a hash code in a hashmap. So 'Résumé' and 'resume' would both map to the same item in the hashmap.