Documentation
¶
Overview ¶
Package collator implements the ECMA-402 Intl.Collator constructor.
locales, _ := locale.ParseList("en-US")
compare, _ := collator.New(locales, collator.Options{})
ok := compare.Compare("a", "b") < 0
_ = ok
See README.md for usage examples and SPECS/45-collator.md for the contract.
Example ¶
Example demonstrates Intl.Collator.prototype.compare from ECMA-402.
package main
import (
"fmt"
"github.com/agentable/go-intl/collator"
"github.com/agentable/go-intl/locale"
)
func main() {
compare, err := collator.New(mustLocaleList("en-US"), collator.Options{})
if err != nil {
panic(err)
}
fmt.Println(compare.Compare("a", "b") < 0)
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: true
Example (Options) ¶
Example_options demonstrates Intl.Collator constructor options from ECMA-402.
package main
import (
"fmt"
gointl "github.com/agentable/go-intl"
"github.com/agentable/go-intl/collator"
"github.com/agentable/go-intl/locale"
)
func main() {
compare, err := collator.New(mustLocaleList("en-US"), collator.Options{
Numeric: gointl.Bool(true),
})
if err != nil {
panic(err)
}
fmt.Println(compare.Compare("2", "10") < 0)
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: true
Index ¶
Examples ¶
Constants ¶
const ( LookupLocaleMatcher LocaleMatcher = "lookup" BestFitLocaleMatcher LocaleMatcher = "best fit" SortUsage Usage = "sort" SearchUsage Usage = "search" BaseSensitivity Sensitivity = "base" AccentSensitivity Sensitivity = "accent" CaseSensitivity Sensitivity = "case" VariantSensitivity Sensitivity = "variant" UpperCaseFirst CaseFirst = "upper" LowerCaseFirst CaseFirst = "lower" FalseCaseFirst CaseFirst = "false" )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Collator ¶
type Collator struct {
// contains filtered or unexported fields
}
Collator compares strings according to the resolved locale and options. A Collator is safe for concurrent use; the embedded x/text/collate.Collator is not, so each Compare call holds a private clone.
func (*Collator) Compare ¶
Compare returns a negative number when x sorts before y, zero when equal, and positive when x sorts after y. The JS bridge for `Intl.Collator.prototype.compare`.
Example ¶
ExampleCollator_Compare demonstrates Intl.Collator.prototype.compare from ECMA-402.
package main
import (
"fmt"
"sort"
gointl "github.com/agentable/go-intl"
"github.com/agentable/go-intl/collator"
"github.com/agentable/go-intl/locale"
)
func main() {
compare, err := collator.New(mustLocaleList("en-US"), collator.Options{
Numeric: gointl.Bool(true),
})
if err != nil {
panic(err)
}
values := []string{"10", "2", "1"}
sort.Slice(values, func(i, j int) bool {
return compare.Compare(values[i], values[j]) < 0
})
fmt.Println(values)
}
func mustLocaleList(tags ...string) locale.List {
locales, err := locale.ParseList(tags...)
if err != nil {
panic(err)
}
return locales
}
Output: [1 2 10]
func (*Collator) ResolvedOptions ¶
func (f *Collator) ResolvedOptions() ResolvedOptions
type LocaleMatcher ¶
type LocaleMatcher string
type Options ¶
type Options struct {
LocaleMatcher LocaleMatcher
Usage Usage
Sensitivity Sensitivity
CaseFirst CaseFirst
Numeric *bool
IgnorePunctuation *bool
Collation string
}
Options mirrors the JS Intl.Collator options bag.
A zero value means "no caller preference" for every field; defaults defined by ECMA-402 (Usage=SortUsage, Sensitivity=VariantSensitivity for sort) are applied during construction.
type ResolvedOptions ¶
type ResolvedOptions struct {
Locale locale.Locale `json:"locale"`
Usage Usage `json:"usage"`
Sensitivity Sensitivity `json:"sensitivity"`
CaseFirst CaseFirst `json:"caseFirst"`
Collation string `json:"collation,omitempty"`
Numeric bool `json:"numeric"`
IgnorePunctuation bool `json:"ignorePunctuation"`
}
type Sensitivity ¶
type Sensitivity string