Skip to content

Commit

Permalink
avoid locale-dependent string comparisons
Browse files Browse the repository at this point in the history
It doesn't seem appropriate to use locale dependent ordering to sort the
variants and extensions for normalization.  The result of normalization
should be consistent.

The Unicode CLDR specficiation is not specific about how these tags are
sorted, it just says that they should be in "alphabetical order".  They
only contain a-z (lower case) and 0-9, so for most locales alphabetical
order is the same as the code point order we are now using.

This commit removes a dependency on the ECMAScript internationalization
API.  This API is not available everywhere, particularly on embedded
devices like TVs where storage space for internationalization data is
limited.  This is important as bcp-47-normalize is now used by dash.js
which in turn is used by many HbbTV applications.
  • Loading branch information
adoakley committed Jul 14, 2023
1 parent cc8009b commit 0d62b3f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import {likely} from './likely.js'

const own = {}.hasOwnProperty

const collator = new Intl.Collator()

/**
* @param {Schema} base
* @param {Partial<Schema>} changes
Expand Down Expand Up @@ -159,7 +157,7 @@ export function bcp47Normalize(tag, options) {
removeLikelySubtags(schema)

// 5. Sort variants, and sort extensions on singleton.
schema.variants.sort(collator.compare)
schema.variants.sort()
schema.extensions.sort(compareSingleton)

// 6. Warn if fields (currently only regions) should be updated but have
Expand Down Expand Up @@ -323,5 +321,11 @@ function add(object, key, value) {
* @returns {number}
*/
function compareSingleton(left, right) {
return collator.compare(left.singleton, right.singleton)
if (left.singleton > right.singleton) {
return 1;
} else if (left.singleton < right.singleton) {
return -1;
} else {
return 0;
}
}

0 comments on commit 0d62b3f

Please sign in to comment.