From 0ae1145e877c556208448a5437e2b98084f02f9e Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 6 Dec 2023 08:22:08 -0800 Subject: [PATCH 1/6] feat: improve Js.Int and change some functions to pipe-last --- jscomp/runtime/js_int.ml | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/jscomp/runtime/js_int.ml b/jscomp/runtime/js_int.ml index 5c2895a5a5..9178309880 100644 --- a/jscomp/runtime/js_int.ml +++ b/jscomp/runtime/js_int.ml @@ -26,6 +26,8 @@ open Melange_mini_stdlib (** Provides functions for inspecting and manipulating [int]s *) +type t = int + (** If we use number, we need coerce to int32 by adding `|0`, otherwise `+0` can be wrong. Most JS API is float oriented, it may overflow int32 or @@ -33,7 +35,7 @@ open Melange_mini_stdlib *) (* + conversion*) -external toExponential : int -> string = "toExponential" +external toExponential : t -> string = "toExponential" [@@mel.send] (** Formats an [int] using exponential (scientific) notation @@ -49,9 +51,8 @@ external toExponential : int -> string = "toExponential" @see MDN *) -external toExponentialWithPrecision : int -> digits:int -> string - = "toExponential" -[@@mel.send] +external toExponentialWithPrecision : digits:t -> string = "toExponential" +[@@mel.send.pipe: t] (** Formats an [int] using exponential (scientific) notation {b digits} specifies how many digits should appear after the decimal point. The @@ -74,7 +75,7 @@ The output will be rounded or padded with zeroes if necessary. @see MDN *) -external toPrecision : int -> string = "toPrecision" +external toPrecision : t -> string = "toPrecision" [@@mel.send] (** Formats a [int] using some fairly arbitrary rules @@ -95,8 +96,8 @@ decimal point. *) (* equivalent to `toString` I think *) -external toPrecisionWithPrecision : int -> digits:int -> string = "toPrecision" -[@@mel.send] +external toPrecisionWithPrecision : digits:t -> string = "toPrecision" +[@@mel.send.pipe: t] (** Formats an [int] using some fairly arbitrary rules {b digits} specifies how many digits should appear in total. The @@ -126,7 +127,7 @@ before the decimal point. @see MDN *) -external toString : int -> string = "toString" +external toString : t -> string = "toString" [@@mel.send] (** Formats a [int] as a string @@ -140,8 +141,8 @@ external toString : int -> string = "toString" @see MDN *) -external toStringWithRadix : int -> radix:int -> string = "toString" -[@@mel.send] +external toStringWithRadix : radix:t -> string = "toString" +[@@mel.send.pipe: t] (** Formats an [int] as a string {b radix} specifies the radix base to use for the formatted number. The @@ -165,8 +166,8 @@ value must be in the range \[2, 36\] (inclusive). @see MDN *) -external toFloat : int -> float = "%floatofint" +external toFloat : t -> float = "%floatofint" -let equal (x : int) y = x = y -let max : int = 2147483647 -let min : int = -2147483648 +let equal (x : t) y = x = y +let max : t = 2147483647 +let min : t = -2147483648 From 481b5584ddda75598cec90dd270d417d3c6ed53c Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 6 Dec 2023 08:23:12 -0800 Subject: [PATCH 2/6] chore: add changelog entry --- Changes.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changes.md b/Changes.md index 340a9f0a5c..571d076a5d 100644 --- a/Changes.md +++ b/Changes.md @@ -90,6 +90,8 @@ Unreleased [#956](https://github.com/melange-re/melange/pull/956), [#958](https://github.com/melange-re/melange/pull/958), [#961](https://github.com/melange-re/melange/pull/961)) +- Improve `Js.Int` and change some of its functions to pipe-last + ([#966](https://github.com/melange-re/melange/pull/966)) 2.1.0 2023-10-22 --------------- From be42cd04eacf947150c117b31e767c58ed3fba4f Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 6 Dec 2023 08:53:31 -0800 Subject: [PATCH 3/6] fix tests and consolidate functions --- jscomp/runtime/js_int.ml | 85 ++++----------------- jscomp/runtime/js_promise.ml | 2 +- jscomp/test/dist/jscomp/test/js_int_test.js | 6 +- jscomp/test/js_int_test.ml | 32 ++++---- 4 files changed, 33 insertions(+), 92 deletions(-) diff --git a/jscomp/runtime/js_int.ml b/jscomp/runtime/js_int.ml index 9178309880..03f5f67b6d 100644 --- a/jscomp/runtime/js_int.ml +++ b/jscomp/runtime/js_int.ml @@ -35,23 +35,7 @@ type t = int *) (* + conversion*) -external toExponential : t -> string = "toExponential" -[@@mel.send] -(** Formats an [int] using exponential (scientific) notation - -{b Returns} a [string] representing the given value in exponential notation - -@raise RangeError if digits is not in the range \[0, 20\] (inclusive) - -{[ - (* prints "7.7e+1" *) - let _ = Js.log (Js.Int.toExponential 77) -]} - -@see MDN -*) - -external toExponentialWithPrecision : digits:t -> string = "toExponential" +external toExponential : ?digits:t -> string = "toExponential" [@@mel.send.pipe: t] (** Formats an [int] using exponential (scientific) notation @@ -65,38 +49,15 @@ The output will be rounded or padded with zeroes if necessary. @raise RangeError if digits is not in the range \[0, 20\] (inclusive) {[ - (* prints "7.70e+1" *) - let _ = Js.log (Js.Int.toExponentialWithPrecision 77 ~digits:2) - - (* prints "5.68e+3" *) - let _ = Js.log (Js.Int.toExponentialWithPrecision 5678 ~digits:2) + Js.Int.toExponential 77 = "7.7e+1" + Js.Int.toExponential ~digits:2 77 = "7.70e+1" + Js.Int.toExponential ~digits:2 5678 = "5.68e+3" ]} @see MDN *) -external toPrecision : t -> string = "toPrecision" -[@@mel.send] -(** Formats a [int] using some fairly arbitrary rules - -{b Returns} a [string] representing the given value in fixed-point (usually) - -[toPrecision] differs from [toFixed] in that the former will format the number -with full precision, while the latter will not output any digits after the -decimal point. - -@raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?) - -{[ - (* prints "123456789" *) - let _ = Js.log (Js.Int.toPrecision 123456789) -]} - -@see MDN -*) -(* equivalent to `toString` I think *) - -external toPrecisionWithPrecision : digits:t -> string = "toPrecision" +external toPrecision : ?digits:t -> string = "toPrecision" [@@mel.send.pipe: t] (** Formats an [int] using some fairly arbitrary rules @@ -117,31 +78,15 @@ before the decimal point. @raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?) {[ - (* prints "1.2e+8" *) - let _ = Js.log (Js.Int.toPrecisionWithPrecision 123456789 ~digits:2) - - (* prints "0.0" *) - let _ = Js.log (Js.Int.toPrecisionWithPrecision 0 ~digits:2) + Js.Int.toPrecision 123456789 = "123456789" + Js.Int.toPrecision ~digits:2 123456789 = "1.2e+8" + Js.Int.toPrecision ~digits:2 0 = "0.0" ]} @see MDN *) -external toString : t -> string = "toString" -[@@mel.send] -(** Formats a [int] as a string - -{b Returns} a [string] representing the given value in fixed-point (usually) - -{[ - (* prints "123456789" *) - let _ = Js.log (Js.Int.toString 123456789) -]} - -@see MDN -*) - -external toStringWithRadix : radix:t -> string = "toString" +external toString : ?radix:t -> string = "toString" [@@mel.send.pipe: t] (** Formats an [int] as a string @@ -153,14 +98,10 @@ value must be in the range \[2, 36\] (inclusive). @raise RangeError if radix is not in the range \[2, 36\] (inclusive) {[ - (* prints "110" *) - let _ = Js.log (Js.Int.toStringWithRadix 6 ~radix:2) - - (* prints "deadbeef" *) - let _ = Js.log (Js.Int.toStringWithRadix 3735928559 ~radix:16) - - (* prints "2n9c" *) - let _ = Js.log (Js.Int.toStringWithRadix 123456 ~radix:36) + Js.Int.toString 123456789 = "123456789" + Js.Int.toString ~radix:2 6 = "110" + Js.Int.toString ~radix:16 3735928559 = "deadbeef" + Js.Int.toString ~radix:36 123456 = "2n9c" ]} @see MDN diff --git a/jscomp/runtime/js_promise.ml b/jscomp/runtime/js_promise.ml index 2c0805c64f..e3a787d519 100644 --- a/jscomp/runtime/js_promise.ml +++ b/jscomp/runtime/js_promise.ml @@ -69,7 +69,7 @@ external then_ : (('a -> 'b t)[@mel.uncurry]) -> 'b t = "then" external catch : ((error -> 'a t)[@mel.uncurry]) -> 'a t = "catch" [@@mel.send.pipe: 'a t] -(* [ p|> catch handler] +(* [ p |> catch handler] Note in JS the returned promise type is actually runtime dependent, if promise is rejected, it will pick the [handler] otherwise the original promise, to make it strict we enforce reject handler diff --git a/jscomp/test/dist/jscomp/test/js_int_test.js b/jscomp/test/dist/jscomp/test/js_int_test.js index e82894eab5..bbefcb2ac8 100644 --- a/jscomp/test/dist/jscomp/test/js_int_test.js +++ b/jscomp/test/dist/jscomp/test/js_int_test.js @@ -9,7 +9,7 @@ var suites_0 = [ return { TAG: /* Eq */0, _0: "1.23456e+5", - _1: (123456).toExponential() + _1: (123456).toExponential(undefined) }; }) ]; @@ -78,7 +78,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "123456", - _1: (123456).toPrecision() + _1: (123456).toPrecision(undefined) }; }) ], @@ -146,7 +146,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "123", - _1: (123).toString() + _1: (123).toString(undefined) }; }) ], diff --git a/jscomp/test/js_int_test.ml b/jscomp/test/js_int_test.ml index b95a05707e..9d1313c507 100644 --- a/jscomp/test/js_int_test.ml +++ b/jscomp/test/js_int_test.ml @@ -4,43 +4,43 @@ let suites = Mt.[ ("toExponential", (fun _ -> Eq("1.23456e+5", toExponential 123456))); ("toExponentialWithPrecision - digits:2", (fun _ -> - Eq("1.23e+5", toExponentialWithPrecision 123456 ~digits:2))); + Eq("1.23e+5", toExponential 123456 ~digits:2))); ("toExponentialWithPrecision - digits:4", (fun _ -> - Eq("1.2346e+5", toExponentialWithPrecision 123456 ~digits:4))); + Eq("1.2346e+5", toExponential 123456 ~digits:4))); ("toExponentialWithPrecision - digits:20", (fun _ -> - Eq("0.00000000000000000000e+0", toExponentialWithPrecision 0 ~digits:20))); + Eq("0.00000000000000000000e+0", toExponential 0 ~digits:20))); (__LOC__, (fun _ -> - ThrowAny(fun () -> ignore @@ toExponentialWithPrecision 0 ~digits:101))); + ThrowAny(fun () -> ignore @@ toExponential 0 ~digits:101))); ("toExponentialWithPrecision - digits:-1", (fun _ -> - ThrowAny(fun () -> ignore @@ toExponentialWithPrecision 0 ~digits:(-1)))); + ThrowAny(fun () -> ignore @@ toExponential 0 ~digits:(-1)))); ("toPrecision", (fun _ -> Eq("123456", toPrecision 123456))); ("toPrecisionWithPrecision - digits:2", (fun _ -> - Eq("1.2e+5", toPrecisionWithPrecision 123456 ~digits:2))); + Eq("1.2e+5", toPrecision 123456 ~digits:2))); ("toPrecisionWithPrecision - digits:4", (fun _ -> - Eq("1.235e+5", toPrecisionWithPrecision 123456 ~digits:4))); + Eq("1.235e+5", toPrecision 123456 ~digits:4))); ("toPrecisionWithPrecision - digits:20", (fun _ -> - Eq("0.0000000000000000000", toPrecisionWithPrecision 0 ~digits:20))); + Eq("0.0000000000000000000", toPrecision 0 ~digits:20))); (__LOC__, (fun _ -> - ThrowAny(fun () -> ignore @@ toPrecisionWithPrecision 0 ~digits:101))); + ThrowAny(fun () -> ignore @@ toPrecision 0 ~digits:101))); ("toPrecisionWithPrecision - digits:-1", (fun _ -> - ThrowAny(fun () -> ignore @@ toPrecisionWithPrecision 0 ~digits:(-1)))); + ThrowAny(fun () -> ignore @@ toPrecision 0 ~digits:(-1)))); ("toString", (fun _ -> Eq("123", toString 123))); ("toStringWithRadix - radix:2", (fun _ -> - Eq("11110001001000000", toStringWithRadix 123456 ~radix:2))); + Eq("11110001001000000", toString 123456 ~radix:2))); ("toStringWithRadix - radix:16", (fun _ -> - Eq("1e240", toStringWithRadix 123456 ~radix:16))); + Eq("1e240", toString 123456 ~radix:16))); ("toStringWithRadix - radix:36", (fun _ -> - Eq("2n9c", toStringWithRadix 123456 ~radix:36))); + Eq("2n9c", toString 123456 ~radix:36))); ("toStringWithRadix - radix:37", (fun _ -> - ThrowAny(fun () -> ignore @@ toStringWithRadix 0 ~radix:37))); + ThrowAny(fun () -> ignore @@ toString 0 ~radix:37))); ("toStringWithRadix - radix:1", (fun _ -> - ThrowAny(fun () -> ignore @@ toStringWithRadix 0 ~radix:1))); + ThrowAny(fun () -> ignore @@ toString 0 ~radix:1))); ("toStringWithRadix - radix:-1", (fun _ -> - ThrowAny(fun () -> ignore @@ toStringWithRadix 0 ~radix:(-1)))); + ThrowAny(fun () -> ignore @@ toString 0 ~radix:(-1)))); ] ;; Mt.from_pair_suites __MODULE__ suites From 087e9859a119222d8a88841302edc09ed6fe8858 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Mon, 11 Dec 2023 13:17:42 +0000 Subject: [PATCH 4/6] fix docstring for toPrecision --- jscomp/runtime/js_int.ml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/jscomp/runtime/js_int.ml b/jscomp/runtime/js_int.ml index 03f5f67b6d..508af849c7 100644 --- a/jscomp/runtime/js_int.ml +++ b/jscomp/runtime/js_int.ml @@ -69,11 +69,11 @@ than 20 (for Node it's 21. Why? Who knows). The output will be rounded or padded with zeroes if necessary. -[toPrecisionWithPrecision] differs from [toFixedWithPrecision] in that the former -will count all digits against the precision, while the latter will count only -the digits after the decimal point. [toPrecisionWithPrecision] will also use -scientific notation if the specified precision is less than the number for digits -before the decimal point. +[toPrecision] differs from {!Js.Float.toFixed} in that the former will count +all digits against the precision, while the latter will count only the digits +after the decimal point. [toPrecision] will also use scientific notation if the +specified precision is less than the number for digits before the decimal +point. @raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?) From 6fa2c467f11d33b9d2819454d6357679eea287d7 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Mon, 11 Dec 2023 13:20:00 +0000 Subject: [PATCH 5/6] fix docstring for toPrecision relating to number of digits --- jscomp/runtime/js_int.ml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/jscomp/runtime/js_int.ml b/jscomp/runtime/js_int.ml index 508af849c7..848be9a2e0 100644 --- a/jscomp/runtime/js_int.ml +++ b/jscomp/runtime/js_int.ml @@ -61,9 +61,8 @@ external toPrecision : ?digits:t -> string = "toPrecision" [@@mel.send.pipe: t] (** Formats an [int] using some fairly arbitrary rules -{b digits} specifies how many digits should appear in total. The -value must between 0 and some arbitrary number that's hopefully at least larger -than 20 (for Node it's 21. Why? Who knows). +{b digits} specifies how many digits should appear in total. The value must +between 1 and some 100. {b Returns} a [string] representing the given value in fixed-point or scientific notation @@ -75,7 +74,7 @@ after the decimal point. [toPrecision] will also use scientific notation if the specified precision is less than the number for digits before the decimal point. -@raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?) +@raise RangeError if digits is not between 1 and 100. {[ Js.Int.toPrecision 123456789 = "123456789" From 45c11df2892bbb8f5d9f5041294ad756424267b0 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Mon, 11 Dec 2023 05:48:32 -0800 Subject: [PATCH 6/6] Update Changes.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Javier Chávarri --- Changes.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Changes.md b/Changes.md index 571d076a5d..33c0aad5bd 100644 --- a/Changes.md +++ b/Changes.md @@ -90,7 +90,7 @@ Unreleased [#956](https://github.com/melange-re/melange/pull/956), [#958](https://github.com/melange-re/melange/pull/958), [#961](https://github.com/melange-re/melange/pull/961)) -- Improve `Js.Int` and change some of its functions to pipe-last +- BREAKING(runtime): Improve `Js.Int` and change some of its functions to pipe-last ([#966](https://github.com/melange-re/melange/pull/966)) 2.1.0 2023-10-22