diff --git a/CHANGELOG.md b/CHANGELOG.md index ec45a637..17163c57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,8 @@ - `fillAllInPlace` -> `fillAll`, `fillInPlaceToEnd` -> `fillToEnd`, `fillInPlace` -> `fill` - And `List`: - `shuffle` -> `toShuffled` +- Use `float` instead of `int` for ordering to avoid premature overflow. https://github.com/rescript-association/rescript-core/pull/149 +- Add `Ordering` module. https://github.com/rescript-association/rescript-core/pull/149 **Note 1**: These changes should all produce the correct type errors. Though `TypedArray`'s `reverse` and `sort` previously mutated _and_ returned the mutated array itself, whereas now they'd be copies. Please be careful refactoring these 2. diff --git a/src/Core__Array.mjs b/src/Core__Array.mjs index cbe561c4..c74602a9 100644 --- a/src/Core__Array.mjs +++ b/src/Core__Array.mjs @@ -47,10 +47,10 @@ function equal(a, b, eq) { function compare(a, b, cmp) { var lenA = a.length; var lenB = b.length; - if (lenA > lenB) { - return 1; - } else if (lenA < lenB) { + if (lenA < lenB) { return -1; + } else if (lenA > lenB) { + return 1; } else { var _i = 0; while(true) { diff --git a/src/Core__Array.res b/src/Core__Array.res index 5c45b83b..0f8be4e5 100644 --- a/src/Core__Array.res +++ b/src/Core__Array.res @@ -57,10 +57,10 @@ let equal = (a, b, eq) => { let rec compareFromIndex = (a, b, i, cmp, len) => if i === len { - 0 + Core__Ordering.equal } else { let c = cmp(a->getUnsafe(i), b->getUnsafe(i)) - if c === 0 { + if c == Core__Ordering.equal { compareFromIndex(a, b, i + 1, cmp, len) } else { c @@ -70,13 +70,11 @@ let rec compareFromIndex = (a, b, i, cmp, len) => let compare = (a, b, cmp) => { let lenA = a->length let lenB = b->length - if lenA > lenB { - 1 - } else if lenA < lenB { - -1 - } else { - compareFromIndex(a, b, 0, cmp, lenA) - } + lenA < lenB + ? Core__Ordering.less + : lenA > lenB + ? Core__Ordering.greater + : compareFromIndex(a, b, 0, cmp, lenA) } @send external copyAllWithin: (array<'a>, ~target: int) => array<'a> = "copyWithin" @@ -139,8 +137,8 @@ let lastIndexOfOpt = (arr, item) => @send external sliceToEnd: (array<'a>, ~start: int) => array<'a> = "slice" @send external copy: array<'a> => array<'a> = "slice" -@send external sort: (array<'a>, ('a, 'a) => int) => unit = "sort" -@send external toSorted: (array<'a>, ('a, 'a) => int) => array<'a> = "toSorted" +@send external sort: (array<'a>, ('a, 'a) => Core__Ordering.t) => unit = "sort" +@send external toSorted: (array<'a>, ('a, 'a) => Core__Ordering.t) => array<'a> = "toSorted" @send external toString: array<'a> => string = "toString" @send external toLocaleString: array<'a> => string = "toLocaleString" diff --git a/src/Core__Array.resi b/src/Core__Array.resi index 0984fe1e..c4fbd938 100644 --- a/src/Core__Array.resi +++ b/src/Core__Array.resi @@ -29,7 +29,7 @@ let fromInitializer: (~length: int, int => 'a) => array<'a> let equal: (array<'a>, array<'a>, ('a, 'a) => bool) => bool -let compare: (array<'a>, array<'a>, ('a, 'a) => int) => int +let compare: (array<'a>, array<'a>, ('a, 'a) => Core__Ordering.t) => Core__Ordering.t @val external isArray: 'a => bool = "Array.isArray" @@ -212,14 +212,14 @@ See [`Array.toSorted`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/R ## Examples ```rescript let someArray = [3, 2, 1] -let sorted = someArray->Array.toSorted((a, b) => a - b) +let sorted = someArray->Array.toSorted(Int.compare) Console.log(sorted) // [1, 2, 3] Console.log(someArray) // [3, 2, 1]. Original unchanged ``` */ @send -external toSorted: (array<'a>, ('a, 'a) => int) => array<'a> = "toSorted" +external toSorted: (array<'a>, ('a, 'a) => Core__Ordering.t) => array<'a> = "toSorted" /** `sort(array, comparator)` sorts `array` in-place using the `comparator` function. @@ -231,13 +231,13 @@ See [`Array.sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer ## Examples ```rescript let someArray = [3, 2, 1] -someArray->Array.sort((a, b) => a - b) +someArray->Array.sort((a, b) => float(a - b)) Console.log(someArray) // [1, 2, 3] ``` */ @send -external sort: (array<'a>, ('a, 'a) => int) => unit = "sort" +external sort: (array<'a>, ('a, 'a) => Core__Ordering.t) => unit = "sort" @variadic @send external splice: (array<'a>, ~start: int, ~remove: int, ~insert: array<'a>) => unit = "splice" diff --git a/src/Core__Date.resi b/src/Core__Date.resi index 38145e6e..06bbe7ba 100644 --- a/src/Core__Date.resi +++ b/src/Core__Date.resi @@ -2,12 +2,12 @@ Functions for interacting with JavaScript Dates. */ -/** +/** A type representing a JavaScript date. */ type t = Js.Date.t -/** +/** Time, in milliseconds, since / until the UNIX epoch (January 1, 1970 00:00:00 UTC). Positive numbers represent dates after, negative numbers dates before epoch. */ @@ -43,7 +43,7 @@ type localeOptions = { timeZoneName?: [#long | #short], } -/** +/** `make()` Creates a date object with the current date time as value. @@ -56,8 +56,8 @@ Date.make() @new external make: unit => t = "Date" -/** -`fromString(dateTimeString)` +/** +`fromString(dateTimeString)` Creates a date object from given date time string. The string has to be in the ISO 8601 format YYYY-MM-DDTHH:mm:ss.sssZ (https://tc39.es/ecma262/#sec-date-time-string-format). @@ -86,8 +86,8 @@ Date.fromString("")->getTime @new external fromString: string => t = "Date" -/** -`fromTime(msSinceEpoch)` +/** +`fromTime(msSinceEpoch)` Creates a date object from the given time in milliseconds since / until UNIX epoch (January 1, 1970 00:00:00 UTC). Positive numbers create dates after epoch, negative numbers create dates before epoch. @@ -107,7 +107,7 @@ Date.fromTime(86_400_000.0) @new external fromTime: msSinceEpoch => t = "Date" -/** +/** Creates a date object with the given year and month. Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -135,7 +135,7 @@ Date.makeWithYM(~year=2023, ~month=-1) @new external makeWithYM: (~year: int, ~month: int) => t = "Date" -/** +/** Creates a date object with the given year, month and date (day of month). Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -156,7 +156,7 @@ Date.makeWithYMD(~year=2023, ~month=1, ~date=29) @new external makeWithYMD: (~year: int, ~month: int, ~date: int) => t = "Date" -/** +/** Creates a date object with the given year, month, date (day of month) and hours. Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -181,7 +181,7 @@ Date.makeWithYMDH(~year=2023, ~month=1, ~date=20, ~hours=-1) @new external makeWithYMDH: (~year: int, ~month: int, ~date: int, ~hours: int) => t = "Date" -/** +/** Creates a date object with the given year, month, date (day of month), hours and minutes. Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -207,7 +207,7 @@ Date.makeWithYMDHM(~year=2023, ~month=1, ~date=20, ~hours=16, ~minutes=-1) external makeWithYMDHM: (~year: int, ~month: int, ~date: int, ~hours: int, ~minutes: int) => t = "Date" -/** +/** Creates a date object with the given year, month, date (day of month), hours, minutes and seconds. Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -239,7 +239,7 @@ external makeWithYMDHMS: ( ~seconds: int, ) => t = "Date" -/** +/** Creates a date object with the given year, month, date (day of month), hours, minutes, seconds and milliseconds. Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -272,7 +272,7 @@ external makeWithYMDHMSM: ( ~milliseconds: int, ) => t = "Date" module UTC: { - /** + /** Returns the time, in milliseconds, since UNIX epoch (January 1, 1970 00:00:00 UTC). Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -296,7 +296,7 @@ module UTC: { @val external makeWithYM: (~year: int, ~month: int) => msSinceEpoch = "Date.UTC" - /** + /** Returns the time, in milliseconds, since UNIX epoch (January 1, 1970 00:00:00 UTC). Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -317,7 +317,7 @@ module UTC: { @val external makeWithYMD: (~year: int, ~month: int, ~date: int) => msSinceEpoch = "Date.UTC" - /** + /** Returns the time, in milliseconds, since UNIX epoch (January 1, 1970 00:00:00 UTC). Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -339,7 +339,7 @@ module UTC: { external makeWithYMDH: (~year: int, ~month: int, ~date: int, ~hours: int) => msSinceEpoch = "Date.UTC" - /** + /** Returns the time, in milliseconds, since UNIX epoch (January 1, 1970 00:00:00 UTC). Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -366,7 +366,7 @@ module UTC: { ~minutes: int, ) => msSinceEpoch = "Date.UTC" - /** + /** Returns the time, in milliseconds, since UNIX epoch (January 1, 1970 00:00:00 UTC). Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -394,7 +394,7 @@ module UTC: { ~seconds: int, ) => msSinceEpoch = "Date.UTC" - /** + /** Returns the time, in milliseconds, since UNIX epoch (January 1, 1970 00:00:00 UTC). Be aware of using a value for year < 100, because it behaves inconsistent (see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#interpretation_of_two-digit_years). Months are 0-indexed (0 = January, 11 = December). @@ -434,9 +434,9 @@ external now: unit => msSinceEpoch = "Date.now" let equal: (t, t) => bool -let compare: (t, t) => int +let compare: (t, t) => Core__Ordering.t -/** +/** `getTime(date)` Returns the time, in milliseconds, between UNIX epoch (January 1, 1970 00:00:00 UTC) and the current date time. @@ -452,7 +452,7 @@ Date.fromString("2023-02-20")->Date.getTime @send external getTime: t => msSinceEpoch = "getTime" -/** +/** `getTimezoneOffset(date)` Returns the time in minutes between the UTC time and the locale time. @@ -470,7 +470,7 @@ Date.fromString("2023-06-01")->Date.getTimezoneOffset @send external getTimezoneOffset: t => int = "getTimezoneOffset" -/** +/** `getFullYear(date)` Returns the year of a given date (according to local time). @@ -484,7 +484,7 @@ Date.fromString("2023-02-20")->Date.getFullYear @send external getFullYear: t => int = "getFullYear" -/** +/** `getMonth(date)` Returns the month (0-indexed) of a given date (according to local time). @@ -498,7 +498,7 @@ Date.fromString("2023-01-01")->Date.getMonth @send external getMonth: t => int = "getMonth" -/** +/** `getDate(date)` Returns the date (day of month) of a given date (according to local time). @@ -512,7 +512,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.getDate @send external getDate: t => int = "getDate" -/** +/** `getHours(date)` Returns the hours of a given date (according to local time). @@ -526,7 +526,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.getHours @send external getHours: t => int = "getHours" -/** +/** `getMinutes(date)` Returns the minutes of a given date (according to local time). @@ -540,7 +540,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.getMinutes @send external getMinutes: t => int = "getMinutes" -/** +/** `getSeconds(date)` Returns the seconds of a given date (according to local time). @@ -554,7 +554,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.getSeconds @send external getSeconds: t => int = "getSeconds" -/** +/** `getMilliseconds(date)` Returns the milliseconds of a given date (according to local time). @@ -568,7 +568,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.getMilliseconds @send external getMilliseconds: t => int = "getMilliseconds" -/** +/** `getDay(date)` Returns the day of week of a given date (according to local time). @@ -583,7 +583,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.getDay @send external getDay: t => int = "getDay" -/** +/** `setFullYear(date, year)` Sets the year of a date (according to local time). @@ -597,7 +597,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setFullYear(2024) @send external setFullYear: (t, int) => unit = "setFullYear" -/** +/** `setFullYearM(date, ~year, ~month)` Sets the year and month of a date (according to local time). @@ -611,7 +611,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setFullYearM(~year=2024, ~month= @send external setFullYearM: (t, ~year: int, ~month: int) => unit = "setFullYear" -/** +/** `setFullYearMD(date, ~year, ~month, ~date)` Sets the year, month and date (day of month) of a date (according to local time). @@ -625,7 +625,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setFullYearMD(~year=2024, ~month @send external setFullYearMD: (t, ~year: int, ~month: int, ~date: int) => unit = "setFullYear" -/** +/** `setMonth(date, month)` Sets the month of a date (according to local time). @@ -639,7 +639,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setMonth(0) @send external setMonth: (t, int) => unit = "setMonth" -/** +/** `setDate(date, day)` Sets the date (day of month) of a date (according to local time). @@ -653,7 +653,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setDate(1) @send external setDate: (t, int) => unit = "setDate" -/** +/** `setHours(date, hours)` Sets the hours of a date (according to local time). @@ -667,7 +667,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setHours(0) @send external setHours: (t, int) => unit = "setHours" -/** +/** `setHoursM(date, ~hours, ~minutes)` Sets the hours and minutes of a date (according to local time). @@ -681,7 +681,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setHoursM(~hours=0, ~minutes=0) @send external setHoursM: (t, ~hours: int, ~minutes: int) => unit = "setHours" -/** +/** `setHoursMS(date, ~hours, ~minutes, ~seconds)` Sets the hours, minutes and seconds of a date (according to local time). @@ -695,7 +695,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setHoursMS(~hours=0, ~minutes=0, @send external setHoursMS: (t, ~hours: int, ~minutes: int, ~seconds: int) => unit = "setHours" -/** +/** `setHoursMSMs(date, ~hours, ~minutes, ~seconds, ~milliseconds)` Sets the hours, minutes, seconds and milliseconds of a date (according to local time). @@ -710,7 +710,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setHoursMSMs(~hours=0, ~minutes= external setHoursMSMs: (t, ~hours: int, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit = "setHours" -/** +/** `setMinutes(date, minutes)` Sets the minutes of a date (according to local time). @@ -724,7 +724,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setMinutes(0) @send external setMinutes: (t, int) => unit = "setMinutes" -/** +/** `setMinutesS(date, ~minutes, ~seconds)` Sets the minutes and seconds of a date (according to local time). @@ -738,7 +738,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setMinutesS(~minutes=0, ~seconds @send external setMinutesS: (t, ~minutes: int, ~seconds: int) => unit = "setMinutes" -/** +/** `setMinutesSMs(date, ~minutes, ~seconds, ~milliseconds)` Sets the minutes, seconds and milliseconds of a date (according to local time). @@ -752,7 +752,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setMinutesSMs(~minutes=0, ~secon @send external setMinutesSMs: (t, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit = "setMinutes" -/** +/** `setSeconds(date, seconds)` Sets the seconds of a date (according to local time). @@ -766,7 +766,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setSeconds(0) @send external setSeconds: (t, int) => unit = "setSeconds" -/** +/** `setSecondsMs(date, ~seconds, ~milliseconds)` Sets the seconds and milliseconds of a date (according to local time). @@ -780,7 +780,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setSecondsMs(~seconds=0, ~millis @send external setSecondsMs: (t, ~seconds: int, ~milliseconds: int) => unit = "setSeconds" -/** +/** `setMilliseconds(date, milliseconds)` Sets the milliseconds of a date (according to local time). @@ -794,7 +794,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setMilliseconds(0) @send external setMilliseconds: (t, int) => unit = "setMilliseconds" -/** +/** `getUTCFullYear(date)` Returns the year of a given date (according to UTC time). @@ -808,7 +808,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCFullYear @send external getUTCFullYear: t => int = "getUTCFullYear" -/** +/** `getUTCMonth(date)` Returns the month of a given date (according to UTC time). @@ -822,7 +822,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCMonth @send external getUTCMonth: t => int = "getUTCMonth" -/** +/** `getUTCDate(date)` Returns the date (day of month) of a given date (according to UTC time). @@ -836,7 +836,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCDate @send external getUTCDate: t => int = "getUTCDate" -/** +/** `getUTCHours(date)` Returns the hours of a given date (according to UTC time). @@ -850,7 +850,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCHours @send external getUTCHours: t => int = "getUTCHours" -/** +/** `getUTCMinutes(date)` Returns the minutes of a given date (according to UTC time). @@ -864,7 +864,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCMinutes @send external getUTCMinutes: t => int = "getUTCMinutes" -/** +/** `getUTCSeconds(date)` Returns the seconds of a given date (according to UTC time). @@ -878,7 +878,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCSeconds @send external getUTCSeconds: t => int = "getUTCSeconds" -/** +/** `getUTCMilliseconds(date)` Returns the milliseconds of a given date (according to UTC time). @@ -892,7 +892,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCMilliseconds @send external getUTCMilliseconds: t => int = "getUTCMilliseconds" -/** +/** `getUTCDay(date)` Returns the day (day of week) of a given date (according to UTC time). @@ -907,7 +907,7 @@ Date.fromString("2023-01-01T00:00:00.00+01:00").getUTCDay @send external getUTCDay: t => int = "getUTCDay" -/** +/** `setUTCFullYear(date, year)` Sets the year of a date (according to UTC time). @@ -921,7 +921,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCFullYear(2024) @send external setUTCFullYear: (t, int) => unit = "setUTCFullYear" -/** +/** `setUTCFullYearM(date, ~year, ~month)` Sets the year and month of a date (according to UTC time). @@ -935,7 +935,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCFullYearM(~year=2024, ~mon @send external setUTCFullYearM: (t, ~year: int, ~month: int) => unit = "setUTCFullYear" -/** +/** `setUTCFullYearMD(date, ~year, ~month, ~date)` Sets the year, month and date (day of month) of a date (according to UTC time). @@ -949,7 +949,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCFullYearMD(~year=2024, ~mo @send external setUTCFullYearMD: (t, ~year: int, ~month: int, ~date: int) => unit = "setUTCFullYear" -/** +/** `setUTCMonth(date, month)` Sets the month of a date (according to UTC time). @@ -963,7 +963,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCMonth(0) @send external setUTCMonth: (t, int) => unit = "setUTCMonth" -/** +/** `setDate(date, day)` Sets the date (day of month) of a date (according to UTC time). @@ -977,7 +977,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCDate(1) @send external setUTCDate: (t, int) => unit = "setUTCDate" -/** +/** `setUTCHours(date, hours)` Sets the hours of a date (according to UTC time). @@ -991,7 +991,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCHours(0) @send external setUTCHours: (t, int) => unit = "setUTCHours" -/** +/** `setHoursM(date, ~hours, ~minutes)` Sets the hours and minutes of a date (according to UTC time). @@ -1005,7 +1005,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCHoursM(~hours=0, ~minutes= @send external setUTCHoursM: (t, ~hours: int, ~minutes: int) => unit = "setUTCHours" -/** +/** `setUTCHoursMS(date, ~hours, ~minutes, ~seconds)` Sets the hours, minutes and seconds of a date (according to UTC time). @@ -1019,7 +1019,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCHoursMS(~hours=0, ~minutes @send external setUTCHoursMS: (t, ~hours: int, ~minutes: int, ~seconds: int) => unit = "setUTCHours" -/** +/** `setUTCHoursMSMs(date, ~hours, ~minutes, ~seconds, ~milliseconds)` Sets the hours, minutes, seconds and milliseconds of a date (according to UTC time). @@ -1039,7 +1039,7 @@ external setUTCHoursMSMs: ( ~milliseconds: int, ) => unit = "setUTCHours" -/** +/** `setUTCMinutes(date, minutes)` Sets the minutes of a date (according to UTC time). @@ -1053,7 +1053,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCMinutes(0) @send external setUTCMinutes: (t, int) => unit = "setUTCMinutes" -/** +/** `setUTCMinutesS(date, ~minutes, ~seconds)` Sets the minutes and seconds of a date (according to UTC time). @@ -1067,7 +1067,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCMinutesS(~minutes=0, ~seco @send external setUTCMinutesS: (t, ~minutes: int, ~seconds: int) => unit = "setUTCMinutes" -/** +/** `setUTCMinutesSMs(date, ~minutes, ~seconds, ~milliseconds)` Sets the minutes, seconds and milliseconds of a date (according to UTC time). @@ -1082,7 +1082,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCMinutesSMs(~minutes=0, ~se external setUTCMinutesSMs: (t, ~minutes: int, ~seconds: int, ~milliseconds: int) => unit = "setUTCMinutes" -/** +/** `setUTCSeconds(date, seconds)` Sets the seconds of a date (according to UTC time). @@ -1096,7 +1096,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCSeconds(0) @send external setUTCSeconds: (t, int) => unit = "setUTCSeconds" -/** +/** `setUTCSecondsMs(date, ~seconds, ~milliseconds)` Sets the seconds and milliseconds of a date (according to UTC time). @@ -1110,7 +1110,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCSecondsMs(~seconds=0, ~mil @send external setUTCSecondsMs: (t, ~seconds: int, ~milliseconds: int) => unit = "setUTCSeconds" -/** +/** `setUTCMilliseconds(date, milliseconds)` Sets the milliseconds of a date (according to UTC time). @@ -1124,7 +1124,7 @@ Date.fromString("2023-02-20T16:40:00.00")->Date.setUTCMilliseconds(0) @send external setUTCMilliseconds: (t, int) => unit = "setUTCMilliseconds" -/** +/** `toDateString(date)` Converts a JavaScript date to a standard date string. The date will be mapped to the current time zone. @@ -1142,7 +1142,7 @@ Date.fromString("2023-01-01T00:00:00.00+08:00")->Date.toDateString->Console.log @send external toDateString: t => string = "toDateString" -/** +/** `toString(date)` Converts a JavaScript date to a standard date time string. The date will be mapped to the current time zone. @@ -1160,7 +1160,7 @@ Date.fromString("2023-06-01T00:00:00.00+01:00")->Date.toString->Console.log @send external toString: t => string = "toString" -/** +/** `toTimeString(date)` Converts a JavaScript date to a standard time string. The date will be mapped to the current time zone. @@ -1178,7 +1178,7 @@ Date.fromString("2023-01-01T00:00:00.00+08:00")->Date.toTimeString->Console.log @send external toTimeString: t => string = "toTimeString" -/** +/** `toLocaleDateString(date)` Converts a JavaScript date to a localized date string. It will use the current locale. @@ -1192,7 +1192,7 @@ Date.make()->Date.toLocaleDateString->Console.log @send external toLocaleDateString: t => string = "toLocaleDateString" -/** +/** `toLocaleDateStringWithLocale(date, locale)` Converts a JavaScript date to a localized date string. It will use the specified locale. @@ -1206,7 +1206,7 @@ Date.make()->Date.toLocaleDateStringWithLocale("en-US")->Console.log @send external toLocaleDateStringWithLocale: (t, string) => string = "toLocaleDateString" -/** +/** `toLocaleDateStringWithLocaleAndOptions(date, locale, options)` Converts a JavaScript date to a localized date string. It will use the specified locale and formatting options. @@ -1227,7 +1227,7 @@ Date.make()->Date.toLocaleDateStringWithLocaleAndOptions("de", { year: #numeric external toLocaleDateStringWithLocaleAndOptions: (t, string, localeOptions) => string = "toLocaleDateString" -/** +/** `toLocaleString(date)` Converts a JavaScript date to a localized date-time string. It will use the current locale. @@ -1241,7 +1241,7 @@ Date.make()->Date.toLocaleString->Console.log @send external toLocaleString: t => string = "toLocaleString" -/** +/** `toLocaleStringWithLocale(date, locale)` Converts a JavaScript date to a localized date-time string. It will use the specified locale. @@ -1255,7 +1255,7 @@ Date.make()->Date.toLocaleStringWithLocale("en-US")->Console.log @send external toLocaleStringWithLocale: (t, string) => string = "toLocaleString" -/** +/** `toLocaleStringWithLocaleAndOptions(date, locale, options)` Converts a JavaScript date to a localized date-time string. It will use the specified locale and formatting options. @@ -1272,7 +1272,7 @@ Date.make()->Date.toLocaleStringWithLocaleAndOptions("en", { era: #long, year: # @send external toLocaleStringWithLocaleAndOptions: (t, string, localeOptions) => string = "toLocaleString" -/** +/** `toLocaleTimeString(date)` Converts a JavaScript date to a localized time string. It will use the current locale. @@ -1286,7 +1286,7 @@ Date.make()->Date.toLocaleTimeString->Console.log @send external toLocaleTimeString: t => string = "toLocaleTimeString" -/** +/** `toLocaleTimeStringWithLocale(date, locale)` Converts a JavaScript date to a localized time string. It will use the specified locale. @@ -1300,7 +1300,7 @@ Date.make()->Date.toLocaleTimeStringWithLocale("en-US")->Console.log @send external toLocaleTimeStringWithLocale: (t, string) => string = "toLocaleTimeString" -/** +/** `toLocaleTimeStringWithLocaleAndOptions(date, locale, options)` Converts a JavaScript date to a localized time string. It will use the specified locale and formatting options. @@ -1318,7 +1318,7 @@ Date.make()->Date.toLocaleTimeStringWithLocaleAndOptions("de", { hour: #"2-digit external toLocaleTimeStringWithLocaleAndOptions: (t, string, localeOptions) => string = "toLocaleTimeString" -/** +/** `toISOString(date)` Converts a JavaScript date to a ISO 8601 string (YYYY-MM-DDTHH:mm:ss.sssZ). The date will be mapped to the UTC time. @@ -1335,7 +1335,7 @@ Date.fromString("2023-01-01T00:00:00.00+08:00")->Date.toISOString->Console.log @send external toISOString: t => string = "toISOString" -/** +/** `toUTCString(date)` Converts a JavaScript date to date time string. The date will be mapped to the UTC time. @@ -1352,7 +1352,7 @@ Date.fromString("2023-01-01T00:00:00.00+08:00")->Date.toUTCString->Console.log @send external toUTCString: t => string = "toUTCString" -/** +/** `toJSON(date)` Converts a JavaScript date to a string. diff --git a/src/Core__Float.mjs b/src/Core__Float.mjs index 88c808e0..6dd2171c 100644 --- a/src/Core__Float.mjs +++ b/src/Core__Float.mjs @@ -10,10 +10,10 @@ function equal(a, b) { function compare(a, b) { if (a < b) { return -1; - } else if (a === b) { - return 0; - } else { + } else if (a > b) { return 1; + } else { + return 0; } } diff --git a/src/Core__Float.res b/src/Core__Float.res index dd04ddf7..4332faee 100644 --- a/src/Core__Float.res +++ b/src/Core__Float.res @@ -10,13 +10,7 @@ module Constants = { let equal = (a: float, b: float) => a === b let compare = (a: float, b: float) => - if a < b { - -1 - } else if a === b { - 0 - } else { - 1 - } + a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal @val external isNaN: float => bool = "isNaN" @val external isFinite: float => bool = "isFinite" diff --git a/src/Core__Float.resi b/src/Core__Float.resi index 5109f8f0..d77a1110 100644 --- a/src/Core__Float.resi +++ b/src/Core__Float.resi @@ -111,7 +111,7 @@ module Constants: { let equal: (float, float) => bool -let compare: (float, float) => int +let compare: (float, float) => Core__Ordering.t /** `isNaN(v)` tests if the given `v` is `NaN`. diff --git a/src/Core__Int.mjs b/src/Core__Int.mjs index 1b8538ba..d96a042c 100644 --- a/src/Core__Int.mjs +++ b/src/Core__Int.mjs @@ -10,10 +10,10 @@ function equal(a, b) { function compare(a, b) { if (a < b) { return -1; - } else if (a === b) { - return 0; - } else { + } else if (a > b) { return 1; + } else { + return 0; } } diff --git a/src/Core__Int.res b/src/Core__Int.res index e2162ba2..743aaf64 100644 --- a/src/Core__Int.res +++ b/src/Core__Int.res @@ -6,13 +6,7 @@ module Constants = { let equal = (a: int, b: int) => a === b let compare = (a: int, b: int) => - if a < b { - -1 - } else if a === b { - 0 - } else { - 1 - } + a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal @send external toExponential: int => string = "toExponential" @send external toExponentialWithPrecision: (int, ~digits: int) => string = "toExponential" diff --git a/src/Core__Int.resi b/src/Core__Int.resi index e34dc9ac..6c182c85 100644 --- a/src/Core__Int.resi +++ b/src/Core__Int.resi @@ -58,7 +58,7 @@ module Constants: { let equal: (int, int) => bool -let compare: (int, int) => int +let compare: (int, int) => Core__Ordering.t /** `toExponential(n)` return a `string` representing the given value in exponential diff --git a/src/Core__List.res b/src/Core__List.res index 2e720bef..44d92857 100644 --- a/src/Core__List.res +++ b/src/Core__List.res @@ -682,20 +682,20 @@ let every2 = (l1, l2, p) => every2U(l1, l2, (. a, b) => p(a, b)) let rec compareLength = (l1, l2) => switch (l1, l2) { - | (list{}, list{}) => 0 - | (_, list{}) => 1 - | (list{}, _) => -1 + | (list{}, list{}) => Core__Ordering.equal + | (_, list{}) => Core__Ordering.greater + | (list{}, _) => Core__Ordering.less | (list{_, ...l1s}, list{_, ...l2s}) => compareLength(l1s, l2s) } let rec compare = (l1, l2, p) => switch (l1, l2) { - | (list{}, list{}) => 0 - | (_, list{}) => 1 - | (list{}, _) => -1 + | (list{}, list{}) => Core__Ordering.equal + | (_, list{}) => Core__Ordering.greater + | (list{}, _) => Core__Ordering.less | (list{a1, ...l1}, list{a2, ...l2}) => let c = p(a1, a2) - if c == 0 { + if c == Core__Ordering.equal { compare(l1, l2, p) } else { c diff --git a/src/Core__List.resi b/src/Core__List.resi index d12659ed..5733ad17 100644 --- a/src/Core__List.resi +++ b/src/Core__List.resi @@ -615,21 +615,21 @@ List.some2(list{0, 1}, list{5, 0}, (a, b) => a > b) // true let some2: (t<'a>, t<'b>, ('a, 'b) => bool) => bool /** -`compareLength(list1, list2)` compare two lists solely by length. Returns `-1` if -`length(list1)` is less than `length(list2)`, `0` if `length(list1)` equals -`length(list2)`, and `1` if `length(list1)` is greater than `length(list2)`. +`compareLength(list1, list2)` compare two lists solely by length. Returns `-1.` if +`length(list1)` is less than `length(list2)`, `0.` if `length(list1)` equals +`length(list2)`, and `1.` if `length(list1)` is greater than `length(list2)`. ## Examples ```rescript -List.compareLength(list{1, 2}, list{3, 4, 5, 6}) // -1 +List.compareLength(list{1, 2}, list{3, 4, 5, 6}) // -1. -List.compareLength(list{1, 2, 3}, list{4, 5, 6}) // 0 +List.compareLength(list{1, 2, 3}, list{4, 5, 6}) // 0. -List.compareLength(list{1, 2, 3, 4}, list{5, 6}) // 1 +List.compareLength(list{1, 2, 3, 4}, list{5, 6}) // 1. ``` */ -let compareLength: (t<'a>, t<'a>) => int +let compareLength: (t<'a>, t<'a>) => Core__Ordering.t /** `compare(list1, list2, f)` compare elements one by one `f`. `f` returns a negative @@ -639,28 +639,28 @@ a positive number if `list1` is "greater than" `list2`. The comparison returns the first non-zero result of `f`, or zero if `f` returns zero for all `list1` and `list2`. -- If all items have compared equal, but `list1` is exhausted first, return `-1`. (`list1` is shorter). -- If all items have compared equal, but `list2` is exhausted first, return `1` (`list1` is longer). +- If all items have compared equal, but `list1` is exhausted first, return `-1.`. (`list1` is shorter). +- If all items have compared equal, but `list2` is exhausted first, return `1.` (`list1` is longer). ## Examples ```rescript -List.compare(list{3}, list{3, 7}, (a, b) => compare(a, b)) /* (-1) */ +List.compare(list{3}, list{3, 7}, (a, b) => compare(a, b)) /* (-1.) */ -List.compare(list{5, 3}, list{5}, (a, b) => compare(a, b)) /* 1 */ +List.compare(list{5, 3}, list{5}, (a, b) => compare(a, b)) /* 1. */ -List.compare(list{1, 3, 5}, list{1, 4, 2}, (a, b) => compare(a, b)) /* (-1) */ +List.compare(list{1, 3, 5}, list{1, 4, 2}, (a, b) => compare(a, b)) /* (-1.) */ -List.compare(list{1, 3, 5}, list{1, 2, 3}, (a, b) => compare(a, b)) /* 1 */ +List.compare(list{1, 3, 5}, list{1, 2, 3}, (a, b) => compare(a, b)) /* 1. */ -List.compare(list{1, 3, 5}, list{1, 3, 5}, (a, b) => compare(a, b)) /* 0 */ +List.compare(list{1, 3, 5}, list{1, 3, 5}, (a, b) => compare(a, b)) /* 0. */ ``` **Please note:** The total ordering of List is different from Array, for Array, we compare the length first and, only if the lengths are equal, elements one by one. For lists, we just compare elements one by one. */ -let compare: (t<'a>, t<'a>, ('a, 'a) => int) => int +let compare: (t<'a>, t<'a>, ('a, 'a) => Core__Ordering.t) => Core__Ordering.t /** `equal(list1, list2, f)` check equality of `list2` and `list2` using `f` for @@ -874,7 +874,7 @@ let setAssoc: (t<('a, 'c)>, 'a, 'c, ('a, 'a) => bool) => t<('a, 'c)> ## Examples ```rescript -List.sort(list{5, 4, 9, 3, 7}, (a, b) => a - b) // list{3, 4, 5, 7, 9} +List.sort(list{5, 4, 9, 3, 7}, Int.compare) // list{3, 4, 5, 7, 9} ``` */ -let sort: (t<'a>, ('a, 'a) => int) => t<'a> +let sort: (t<'a>, ('a, 'a) => Core__Ordering.t) => t<'a> diff --git a/src/Core__Null.resi b/src/Core__Null.resi index 50b1e79f..f2e917fc 100644 --- a/src/Core__Null.resi +++ b/src/Core__Null.resi @@ -33,7 +33,7 @@ Console.log(null) // Logs `null` to the console. external null: t<'a> = "#null" /** -Creates a new `Null.t` from the provided value. +Creates a new `Null.t` from the provided value. This means the compiler will enforce null checks for the new value. ## Examples @@ -46,7 +46,7 @@ external make: 'a => t<'a> = "%identity" let equal: (t<'a>, t<'b>, ('a, 'b) => bool) => bool -let compare: (t<'a>, t<'b>, ('a, 'b) => int) => int +let compare: (t<'a>, t<'b>, ('a, 'b) => Core__Ordering.t) => Core__Ordering.t /** Converts a nullable value into an option, so it can be pattern matched on. diff --git a/src/Core__Nullable.resi b/src/Core__Nullable.resi index 821554d1..4ca135ac 100644 --- a/src/Core__Nullable.resi +++ b/src/Core__Nullable.resi @@ -5,7 +5,7 @@ Primarily useful when interoping with JavaScript when you don't know whether you */ /** -Type representing a nullable value. +Type representing a nullable value. A nullable value can be the value `'a`, `null` or `undefined`. */ type t<'a> = Js.Nullable.t<'a> @@ -35,7 +35,7 @@ Console.log(undefined) // Logs `undefined` to the console. external undefined: t<'a> = "#undefined" /** -Creates a new nullable value from the provided value. +Creates a new nullable value from the provided value. This means the compiler will enforce null checks for the new value. ## Examples @@ -57,7 +57,7 @@ external make: 'a => t<'a> = "%identity" let equal: (t<'a>, t<'b>, ('a, 'b) => bool) => bool -let compare: (t<'a>, t<'b>, ('a, 'b) => int) => int +let compare: (t<'a>, t<'b>, ('a, 'b) => Core__Ordering.t) => Core__Ordering.t /** Converts a nullable value into an option, so it can be pattern matched on. diff --git a/src/Core__Option.res b/src/Core__Option.res index 64171450..bc181ded 100644 --- a/src/Core__Option.res +++ b/src/Core__Option.res @@ -100,7 +100,7 @@ let equal = (a, b, eq) => let compare = (a, b, cmp) => switch (a, b) { | (Some(a), Some(b)) => cmp(a, b) - | (None, Some(_)) => -1 - | (Some(_), None) => 1 - | (None, None) => 0 + | (None, Some(_)) => Core__Ordering.less + | (Some(_), None) => Core__Ordering.greater + | (None, None) => Core__Ordering.equal } diff --git a/src/Core__Option.resi b/src/Core__Option.resi index 28cabcfd..8ab0abec 100644 --- a/src/Core__Option.resi +++ b/src/Core__Option.resi @@ -222,14 +222,14 @@ let equal: (option<'a>, option<'b>, ('a, 'b) => bool) => bool /** `compare(opt1, opt2, f)` compares two optional values with respect to given `f`. -If both `opt1` and `opt2` are `None`, it returns `0`. If the first argument is `Some(value1)` and the second is `None`, returns `1` (something is greater than nothing). +If both `opt1` and `opt2` are `None`, it returns `0.`. If the first argument is `Some(value1)` and the second is `None`, returns `1.` (something is greater than nothing). -If the first argument is `None` and the second is `Some(value2)`, returns `-1` +If the first argument is `None` and the second is `Some(value2)`, returns `-1.` (nothing is less than something). If the arguments are `Some(value1)` and `Some(value2)`, returns the result of -`f(value1, value2)`, `f` takes two arguments and returns `-1` if the first -argument is less than the second, `0` if the arguments are equal, and `1` if +`f(value1, value2)`, `f` takes two arguments and returns `-1.` if the first +argument is less than the second, `0.` if the arguments are equal, and `1.` if the first argument is greater than the second. ## Examples @@ -239,12 +239,12 @@ let clockCompare = (a, b) => compare(mod(a, 12), mod(b, 12)) open Option -compare(Some(3), Some(15), clockCompare) // 0 -compare(Some(3), Some(14), clockCompare) // 1 -compare(Some(2), Some(15), clockCompare) // (-1) -compare(None, Some(15), clockCompare) // (-1) -compare(Some(14), None, clockCompare) // 1 -compare(None, None, clockCompare) // 0 +compare(Some(3), Some(15), clockCompare) // 0. +compare(Some(3), Some(14), clockCompare) // 1. +compare(Some(2), Some(15), clockCompare) // (-1.) +compare(None, Some(15), clockCompare) // (-1.) +compare(Some(14), None, clockCompare) // 1. +compare(None, None, clockCompare) // 0. ``` */ -let compare: (option<'a>, option<'b>, ('a, 'b) => int) => int +let compare: (option<'a>, option<'b>, ('a, 'b) => Core__Ordering.t) => Core__Ordering.t diff --git a/src/Core__Ordering.mjs b/src/Core__Ordering.mjs new file mode 100644 index 00000000..4b76033e --- /dev/null +++ b/src/Core__Ordering.mjs @@ -0,0 +1,37 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +function isLess(ord) { + return ord < 0; +} + +function isEqual(ord) { + return ord === 0; +} + +function isGreater(ord) { + return ord > 0; +} + +function invert(ord) { + return - ord; +} + +function fromInt(n) { + if (n < 0) { + return -1; + } else if (n > 0) { + return 1; + } else { + return 0; + } +} + +export { + isLess , + isEqual , + isGreater , + invert , + fromInt , +} +/* No side effect */ diff --git a/src/Core__Ordering.res b/src/Core__Ordering.res new file mode 100644 index 00000000..c22a9ec4 --- /dev/null +++ b/src/Core__Ordering.res @@ -0,0 +1,13 @@ +type t = float + +@inline let less = -1. +@inline let equal = 0. +@inline let greater = 1. + +let isLess = ord => ord < equal +let isEqual = ord => ord == equal +let isGreater = ord => ord > equal + +let invert = ord => -.ord + +let fromInt = n => n < 0 ? less : n > 0 ? greater : equal diff --git a/src/Core__Result.res b/src/Core__Result.res index 681729c8..f0305e56 100644 --- a/src/Core__Result.res +++ b/src/Core__Result.res @@ -83,9 +83,9 @@ let equal = (a, b, f) => let compare = (a, b, f) => switch (a, b) { | (Ok(a), Ok(b)) => f(a, b) - | (Error(_), Ok(_)) => -1 - | (Ok(_), Error(_)) => 1 - | (Error(_), Error(_)) => 0 + | (Error(_), Ok(_)) => Core__Ordering.less + | (Ok(_), Error(_)) => Core__Ordering.greater + | (Error(_), Error(_)) => Core__Ordering.equal } let forEach = (r, f) => diff --git a/src/Core__Result.resi b/src/Core__Result.resi index 0a6bc37c..b82d1959 100644 --- a/src/Core__Result.resi +++ b/src/Core__Result.resi @@ -164,15 +164,15 @@ let equal: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool /** `compare(res1, res2, f)`: Compare two `Result` variables with respect to a - comparison function. The comparison function returns -1 if the first variable - is "less than" the second, 0 if the two variables are equal, and 1 if the first + comparison function. The comparison function returns -1. if the first variable + is "less than" the second, 0. if the two variables are equal, and 1. if the first is "greater than" the second. If `res1` and `res2` are of the form `Ok(n)` and `Ok(m)`, return the result of `f(n, m)`. If `res1` is of the form `Error(e)` and `res2` of the form `Ok(n)`, - return -1 (nothing is less than something) If `res1` is of the form `Ok(n)` and - `res2` of the form `Error(e)`, return 1 (something is greater than nothing) If - both `res1` and `res2` are of the form `Error(e)`, return 0 (equal) + return -1. (nothing is less than something) If `res1` is of the form `Ok(n)` and + `res2` of the form `Error(e)`, return 1. (something is greater than nothing) If + both `res1` and `res2` are of the form `Error(e)`, return 0. (equal) ```res example let good1 = Result.Ok(59) @@ -185,21 +185,21 @@ let equal: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => bool) => bool let mod10cmp = (a, b) => Pervasives.compare(mod(a, 10), mod(b, 10)) - Result.compare(Ok(39), Ok(57), mod10cmp) == 1 + Result.compare(Ok(39), Ok(57), mod10cmp) == 1. - Result.compare(Ok(57), Ok(39), mod10cmp) == (-1) + Result.compare(Ok(57), Ok(39), mod10cmp) == (-1.) - Result.compare(Ok(39), Error("y"), mod10cmp) == 1 + Result.compare(Ok(39), Error("y"), mod10cmp) == 1. - Result.compare(Error("x"), Ok(57), mod10cmp) == (-1) + Result.compare(Error("x"), Ok(57), mod10cmp) == (-1.) - Result.compare(Error("x"), Error("y"), mod10cmp) == 0 + Result.compare(Error("x"), Error("y"), mod10cmp) == 0. ``` */ -let compare: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => int) => int +let compare: (t<'a, 'c>, t<'b, 'd>, ('a, 'b) => Core__Ordering.t) => Core__Ordering.t /** -`forEach(res, f)` runs the provided function `f` on the `Ok` value. If `res` is `Error`, nothing happens. +`forEach(res, f)` runs the provided function `f` on the `Ok` value. If `res` is `Error`, nothing happens. ## Examples diff --git a/src/Core__String.mjs b/src/Core__String.mjs index fb79fae4..c6f60393 100644 --- a/src/Core__String.mjs +++ b/src/Core__String.mjs @@ -6,12 +6,12 @@ function equal(a, b) { } function compare(a, b) { - if (a === b) { - return 0; + if (a < b) { + return -1; } else if (a > b) { return 1; } else { - return -1; + return 0; } } diff --git a/src/Core__String.res b/src/Core__String.res index dee4498a..15d39583 100644 --- a/src/Core__String.res +++ b/src/Core__String.res @@ -9,13 +9,7 @@ let equal = (a: string, b: string) => a === b let compare = (a: string, b: string) => - if a === b { - 0 - } else if a > b { - 1 - } else { - -1 - } + a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal @get external length: string => int = "length" @get_index external get: (string, int) => option = "" diff --git a/src/Core__String.resi b/src/Core__String.resi index cb0a5593..78425ccb 100644 --- a/src/Core__String.resi +++ b/src/Core__String.resi @@ -120,7 +120,7 @@ external fromCodePointMany: array => string = "String.fromCodePoint" let equal: (string, string) => bool -let compare: (string, string) => int +let compare: (string, string) => Core__Ordering.t /** `length(str)` returns the length of the given `string`. @@ -653,9 +653,9 @@ let searchOpt: (string, Core__RegExp.t) => option /** `slice(str, ~start, ~end)` returns the substring of `str` starting at character `start` up to but not including `end`. -- If either `start` or `end` is negative, then it is evaluated as +- If either `start` or `end` is negative, then it is evaluated as `length(str - start)` or `length(str - end)`. -- If `end` is greater than the length of `str`, then it is treated as +- If `end` is greater than the length of `str`, then it is treated as `length(str)`. - If `start` is greater than `end`, slice returns the empty string. See [`String.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) on MDN. diff --git a/src/RescriptCore.mjs b/src/RescriptCore.mjs index 334f6e2d..aad5ebf3 100644 --- a/src/RescriptCore.mjs +++ b/src/RescriptCore.mjs @@ -28,6 +28,8 @@ var Nullable; var $$Object; +var Ordering; + var $$Promise; var $$RegExp; @@ -110,6 +112,7 @@ export { Null , Nullable , $$Object , + Ordering , $$Promise , $$RegExp , $$String , diff --git a/src/RescriptCore.res b/src/RescriptCore.res index 74fdad58..0cdb0025 100644 --- a/src/RescriptCore.res +++ b/src/RescriptCore.res @@ -13,6 +13,7 @@ module Math = Core__Math module Null = Core__Null module Nullable = Core__Nullable module Object = Core__Object +module Ordering = Core__Ordering module Promise = Core__Promise module RegExp = Core__RegExp module String = Core__String diff --git a/src/typed-arrays/Core__TypedArray.res b/src/typed-arrays/Core__TypedArray.res index e93191e5..0ce5ab7a 100644 --- a/src/typed-arrays/Core__TypedArray.res +++ b/src/typed-arrays/Core__TypedArray.res @@ -24,8 +24,8 @@ external copyWithin: (t<'a>, ~target: int, ~start: int, ~end: int) => array<'a> @send external reverse: t<'a> => unit = "reverse" @send external toReversed: t<'a> => t<'a> = "toReversed" -@send external sort: (t<'a>, ('a, 'a) => int) => unit = "sort" -@send external toSorted: (t<'a>, ('a, 'a) => int) => t<'a> = "toSorted" +@send external sort: (t<'a>, ('a, 'a) => Core__Ordering.t) => unit = "sort" +@send external toSorted: (t<'a>, ('a, 'a) => Core__Ordering.t) => t<'a> = "toSorted" @send external with: (t<'a>, int, 'a) => t<'a> = "with"