(that: string, locales?: Array<string>, options?: Intl.CollatorOptions): (
self: string
) => Ordering.OrderingComputes locale-aware ordering for two strings, with optional locales and
collator options, and returns the result as an Ordering (-1, 0, or
1).
Example (Comparing strings by locale)
import { pipe, String } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(pipe("a", String.localeCompare("b")), -1)
assert.deepStrictEqual(pipe("b", String.localeCompare("a")), 1)
assert.deepStrictEqual(pipe("a", String.localeCompare("a")), 0)export const const localeCompare: (
that: string,
locales?: Array<string>,
options?: Intl.CollatorOptions
) => (self: string) => Ordering.Ordering
Computes locale-aware ordering for two strings, with optional locales and
collator options, and returns the result as an Ordering (-1, 0, or
1).
Example (Comparing strings by locale)
import { pipe, String } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(pipe("a", String.localeCompare("b")), -1)
assert.deepStrictEqual(pipe("b", String.localeCompare("a")), 1)
assert.deepStrictEqual(pipe("a", String.localeCompare("a")), 0)
localeCompare =
(that: stringthat: string, locales: string[] | undefinedlocales?: interface Array<T>Array<string>, options: Intl.CollatorOptions | undefinedoptions?: Intl.interface Intl.CollatorOptionsCollatorOptions) => (self: stringself: string): import OrderingOrdering.type Ordering = 0 | 1 | -1Represents the result of comparing two values.
When to use
Use to model a normalized comparison result that is exactly less than,
equal to, or greater than.
Details
-1 indicates the first value is less than the second
0 indicates the values are equal
1 indicates the first value is greater than the second
Example (Defining comparison results)
import type { Ordering } from "effect"
// Custom comparison function
const compareNumbers = (a: number, b: number): Ordering.Ordering => {
if (a < b) return -1
if (a > b) return 1
return 0
}
console.log(compareNumbers(5, 10)) // -1 (5 < 10)
console.log(compareNumbers(10, 5)) // 1 (10 > 5)
console.log(compareNumbers(5, 5)) // 0 (5 == 5)
// Using with string comparison
const compareStrings = (a: string, b: string): Ordering.Ordering => {
return a.localeCompare(b) as Ordering.Ordering
}
Ordering =>
import numbernumber.const sign: (n: number) => OrderingDetermines the sign of a given number.
When to use
Use to classify a number as negative, zero, or positive.
Example (Determining the sign)
import { Number } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(Number.sign(-5), -1)
assert.deepStrictEqual(Number.sign(0), 0)
assert.deepStrictEqual(Number.sign(5), 1)
sign(self: stringself.String.localeCompare(that: string, locales?: Intl.LocalesArgument, options?: Intl.CollatorOptions): number (+2 overloads)Determines whether two strings are equivalent in the current or specified locale.
localeCompare(that: stringthat, locales: string[] | undefinedlocales, options: Intl.CollatorOptions | undefinedoptions))