Intl.Collator

A library for comparing strings in a language sensitve way. This module binds to Intl.Collator. Note that the Intl.js polyfill does not include Intl.Collator. In environments without Intl.Collator this library will fall back to String.prototype.localeCompare, which will not respect the locale and options passed in.

Create

type Collator

A Collator object, for comparing strings in a language sensitive way.

fromLocale
:
Intl.Locale.Locale -> Intl.Collator.Collator

Create a Collator using rules from the specified language

compare (fromLocale "pt-BR") "Tudo" "Bem"
fromOptions
:
Intl.Collator.Options -> Intl.Collator.Collator

Create a Collator using rules from the language and other options.

let
  naturalCompare = fromOptions
    { locale = Locale.en
    , usage = Sort
    , sensitivity = Base
    , ignorePunctuation = True
    , numeric = True
    , caseFirst = Default
    }
    |> compare
in
  naturalCompare "123" "25" -- GT

Comparing Strings

compare
:
Intl.Collator.Collator -> String -> String -> Basics.Order

Compare two Strings according to the sort order of the Collator.

compare (fromLocale Locale.en) "123" "25" -- LT

Support and options

Not all environments will support all languages and options. These functions help determine what is supported, and what options a particular Collator will use.

type Usage
= Sort
| Search

Whether the comparison is for sorting or for searching for matching strings.

type Sensitivity
= Base
| Accent
| Case
| Variant

Which differences in the strings should lead to non-zero result values. Possible values are:

  • Base: Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A.
  • Accent: Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A.
  • Case: Only strings that differ in base letters or case compare as unequal. Examples: a ≠ b, a = á, a ≠ A.
  • Variant: Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples: a ≠ b, a ≠ á, a ≠ A.
type CaseFirst
= Upper
| Lower
| Default

Whether upper case or lower case should sort first, or use the default order for the language.

defaults
:
Intl.Collator.Options

Returns the default options. This is helpful if you only care to change a few options.

options : Options
options =
  { defaults | sensitivity = Base }
resolvedOptions
:
Intl.Collator.Collator -> Intl.Collator.Options

Returns the locale and collation options computed when the Collator was created.

if (resolvedOptions collator).numeric then
  "Sorts numbers naturally"
else
  "Sorts numbers lexically"
supportedLocalesOf
:
List Intl.Locale.Locale -> List Intl.Locale.Locale

Returns a list from the provided languages that are supported without having to fall back to the runtime's default language.

case fromLanguageTag "qya" of
  Just elvish ->
    if isEmpty (supportedLocalesOf [ elvish ]) then
      "I can't sort Elvish text"
    else
      "Tolkien is #1"
  Nothing ->
    "You shall not pass"
Basic information
README
Browse source on Github
Elm version: 0.17.1 <= v < 0.18.0
Contains Native Module(s)
Modules
Intl.Collator
Intl.Currency Intl.DateTimeFormat Intl.Locale Intl.NumberFormat Intl.TimeZone
Versions
1.0.0
Dependencies