📜  intl collator - Javascript (1)

📅  最后修改于: 2023-12-03 15:15:51.444000             🧑  作者: Mango

Intl Collator - JavaScript

The Intl Collator object is a built-in object in JavaScript that provides language-sensitive string comparison. It uses the specified language or locale to compare strings according to the rules of the language or locale.

Syntax
new Intl.Collator([locales[, options]])
  • locales: A string or an array of strings that specifies the language or locale to use for string comparison. If multiple locales are provided, the first one is used as the primary locale and the other ones are used as fallback locales if the primary locale is not supported. (Optional)
  • options: An object that specifies additional options for string comparison. The available options are listed below. (Optional)
Options

The following options are available for the options parameter:

  • localeMatcher: Specifies how to match the locales provided in the locales parameter. The available values are "lookup" (default) and "best fit".
  • usage: Specifies the context in which the comparison is performed. The available values are "sort" (default) and "search".
  • sensitivity: Specifies how sensitive the comparison is to differences in casing, accents, and diacritics. The available values are "base", "accent", "case", and "variant".
  • ignorePunctuation: Specifies whether to ignore punctuation in the comparison. The available values are true and false. Default is false.
  • numeric: Specifies whether to compare digits numerically, rather than lexicographically. The available values are true and false. Default is false.
Examples
Basic Usage
const collator = new Intl.Collator('en-US');

console.log(collator.compare('apple', 'banana')); // Output: -1
console.log(collator.compare('banana', 'apple')); // Output: 1
console.log(collator.compare('apple', 'apple')); // Output: 0

This example creates a new Intl.Collator object with the 'en-US' locale, which is used to compare the strings 'apple' and 'banana'. The collator.compare() method returns -1, indicating that 'apple' comes before 'banana' in alphabetical order.

Advanced Usage
const collator = new Intl.Collator('sv-SE', {
  usage: 'search',
  sensitivity: 'base'
});

console.log(collator.compare('låg', 'lag')); // Output: 0
console.log(collator.compare('LAG', 'lag')); // Output: -1
console.log(collator.compare('Å', 'Ä')); // Output: 1

This example creates a new Intl.Collator object with the 'sv-SE' locale, which is used to compare the strings 'låg' and 'lag'. The usage option is set to 'search', which means that the comparison is performed as if the strings were being used in a search context. The sensitivity option is set to 'base', which means that differences in casing, accents, and diacritics are ignored. The collator.compare() method returns 0, indicating that 'låg' and 'lag' are equivalent for search purposes. The second comparison 'LAG' and 'lag' returns -1 meaning that uppercase letters come before lowercase, taking into account sorting conventions of the locale. The third example comparing 'Å' and 'Ä' returns 1, indicating that 'Å' comes after 'Ä' in alphabetical order in the Swedish sorting conventions.

Conclusion

The Intl Collator object is an important tool for developers to provide language-sensitive string comparison in their web applications. By taking into account the rules and conventions of a specific locale, developers can provide more accurate and relevant sorting and searching functionality.