collator

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: MIT Imports: 10 Imported by: 0

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

View Source
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

func SupportedLocalesOf

func SupportedLocalesOf(locales locale.List, opts Options) (locale.List, error)

Types

type CaseFirst

type CaseFirst string

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 New

func New(locales locale.List, opts Options) (*Collator, error)

New constructs a Collator for the requested locale and options.

func (*Collator) Compare

func (f *Collator) Compare(x, y string) int

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

type Usage

type Usage string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL