Skip to content

Commit 1783a45

Browse files
authored
Add String.localeCompare options (#8287)
* Add String.localeCompare options * CHANGELOG
1 parent c4c8f4d commit 1783a45

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#### :rocket: New Feature
2020

2121
- Reanalyze: add glob pattern support for suppress/unsuppress configurations (e.g., `"src/generated/**"`). https://github.com/rescript-lang/rescript/pull/8277
22+
- Add optional `~locales` and `~options` parameters to `String.localeCompare`. https://github.com/rescript-lang/rescript/pull/8287
2223

2324
#### :bug: Bug fix
2425

packages/@rescript/runtime/Stdlib_String.res

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,13 @@ external substringToEnd: (string, ~start: int) => string = "substring"
182182
@send external padStart: (string, int, string) => string = "padStart"
183183
@send external padEnd: (string, int, string) => string = "padEnd"
184184

185-
@send external localeCompare: (string, string) => float = "localeCompare"
185+
@send
186+
external localeCompare: (
187+
string,
188+
string,
189+
~locales: array<string>=?,
190+
~options: Stdlib_Intl_Collator.options=?,
191+
) => float = "localeCompare"
186192

187193
let isEmpty = s => length(s) == 0
188194

packages/@rescript/runtime/Stdlib_String.resi

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,22 +1174,32 @@ String.padEnd("abc", 1, "") == "abc"
11741174
external padEnd: (string, int, string) => string = "padEnd"
11751175

11761176
/**
1177-
`localeCompare(referenceStr, compareStr)` returns a float than indicatings
1177+
`localeCompare(referenceStr, compareStr, ~locales=?, ~options=?)` returns a float indicating
11781178
whether a reference string comes before or after, or is the same as the given
1179-
string in sort order. If `referenceStr` occurs before `compareStr` positive if
1180-
the `referenceStr` occurs after `compareStr`, `0` if they are equivalent.
1181-
Do not rely on exact return values of `-1` or `1`
1179+
string in sort order. Returns a negative value if `referenceStr` occurs before `compareStr`,
1180+
positive if `referenceStr` occurs after `compareStr`, `0` if they are equivalent.
1181+
Do not rely on exact return values of `-1` or `1`.
1182+
1183+
Optionally takes `~locales` to specify locale(s) and `~options` to customize comparison behavior
1184+
(e.g., sensitivity, case ordering, numeric sorting). These correspond to the `Intl.Collator` options.
11821185
See [`String.localeCompare`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare) on MDN.
11831186
11841187
## Examples
11851188
11861189
```rescript
11871190
String.localeCompare("a", "c") < 0.0 == true
11881191
String.localeCompare("a", "a") == 0.0
1192+
String.localeCompare("a", "b", ~locales=["en-US"]) < 0.0 == true
1193+
String.localeCompare("a", "A", ~locales=["en-US"], ~options={sensitivity: #base}) == 0.0
11891194
```
11901195
*/
11911196
@send
1192-
external localeCompare: (string, string) => float = "localeCompare"
1197+
external localeCompare: (
1198+
string,
1199+
string,
1200+
~locales: array<string>=?,
1201+
~options: Stdlib_Intl_Collator.options=?,
1202+
) => float = "localeCompare"
11931203

11941204
/**
11951205
`ignore(string)` ignores the provided string and returns unit.

0 commit comments

Comments
 (0)