From e4ade284d8cc28dc5078b13a5b2553a9f2525ba2 Mon Sep 17 00:00:00 2001 From: Antonio Nuno Monteiro Date: Wed, 6 Dec 2023 16:26:13 -0800 Subject: [PATCH] feat: improve Js.Float and change some functions to pipe-last --- Changes.md | 2 + jscomp/runtime/js_float.ml | 182 ++++-------------- jscomp/test/dist/jscomp/test/js_float_test.js | 16 +- jscomp/test/js_float_test.ml | 42 ++-- 4 files changed, 72 insertions(+), 170 deletions(-) diff --git a/Changes.md b/Changes.md index 1c260f4d56..2026d0766a 100644 --- a/Changes.md +++ b/Changes.md @@ -97,6 +97,8 @@ Unreleased - BREAKING(runtime): Improve docstrings in the `Node` library and change some of its functions to pipe-last ([#970](https://github.com/melange-re/melange/pull/970)) +- BREAKING(runtime): Improve `Js.Float` and change some of its functions to + pipe-last ([#968](https://github.com/melange-re/melange/pull/968)) 2.2.0 2023-12-05 --------------- diff --git a/jscomp/runtime/js_float.ml b/jscomp/runtime/js_float.ml index b2ade05093..ebfb96464b 100644 --- a/jscomp/runtime/js_float.ml +++ b/jscomp/runtime/js_float.ml @@ -22,17 +22,18 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -(** Provides functions for inspecting and manipulating [float]s -*) +(** Provides functions for inspecting and manipulating [float]s *) + +type t = float -external _NaN : float = "NaN" +external _NaN : t = "NaN" (** The special value "Not a Number" @see MDN *) -external isNaN : float -> bool = "isNaN" +external isNaN : t -> bool = "isNaN" [@@mel.scope "Number"] (** Tests if the given value is [_NaN] @@ -44,7 +45,7 @@ therefore necessary to test for [_NaN]. @see MDN *) -external isFinite : float -> bool = "isFinite" +external isFinite : t -> bool = "isFinite" [@@mel.scope "Number"] (** Tests if the given value is finite @@ -67,28 +68,8 @@ let _ = Js.Float.isFinite 1234 @see MDN *) -external toExponential : float -> string = "toExponential" -[@@mel.send] -(** Formats a [float] 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.71234e+1" *) - let _ = Js.log (Js.Float.toExponential 77.1234) - - (* prints "7.7e+1" *) - let _ = Js.log (Js.Float.toExponential 77.) -]} - -@see MDN -*) - -external toExponentialWithPrecision : float -> digits:int -> string - = "toExponential" -[@@mel.send] +external toExponential : ?digits:int -> string = "toExponential" +[@@mel.send.pipe: t] (** Formats a [float] using exponential (scientific) notation {b digits} specifies how many digits should appear after the decimal point. The @@ -101,35 +82,16 @@ 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.71e+1" *) - let _ = Js.log (Js.Float.toExponentialWithPrecision 77.1234 ~digits:2) + Js.Float.toExponential 77.1234 = "7.71234e+1" + Js.Float.toExponential 77. = "7.7e+1" + Js.Float.toExponential ~digits:2 77.1234 = "7.71e+1" ]} @see MDN *) -external toFixed : float -> string = "toFixed" -[@@mel.send] -(** Formats a [float] using fixed point notation - -{b Returns} a [string] representing the given value in fixed-point notation (usually) - -@raise RangeError if digits is not in the range \[0, 20\] (inclusive) - -{[ - (* prints "12346" (note the rounding) *) - let _ = Js.log (Js.Float.toFixed 12345.6789) - - (* print "1.2e+21" *) - let _ = Js.log (Js.Float.toFixed 1.2e21) -]} - -@see MDN -*) - -external toFixedWithPrecision : float -> digits:int -> string = "toFixed" -[@@mel.send] +external toFixed : ?digits:int -> string = "toFixed" +[@@mel.send.pipe: t] (** Formats a [float] using fixed point notation {b digits} specifies how many digits should appear after the decimal point. The @@ -142,43 +104,17 @@ The output will be rounded or padded with zeroes if necessary. @raise RangeError if digits is not in the range \[0, 20\] (inclusive) {[ - (* prints "12345.7" (note the rounding) *) - let _ = Js.log (Js.Float.toFixedWithPrecision 12345.6789 ~digits:1) - - (* prints "0.00" (note the added zeroes) *) - let _ = Js.log (Js.Float.toFixedWithPrecision 0. ~digits:2) + Js.Float.toFixed 12345.6789 = "12346" + Js.Float.toFixed 1.2e21 = "1.2e+21" + Js.Float.toFixed ~digits:1 12345.6789 = "12345.7" + Js.Float.toFixed ~digits:2 0. = "0.00" ]} @see MDN *) -external toPrecision : float -> string = "toPrecision" -[@@mel.send] -(** Formats a [float] 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 "12345.6789" *) - let _ = Js.log (Js.Float.toPrecision 12345.6789) - - (* print "1.2e+21" *) - let _ = Js.log (Js.Float.toPrecision 1.2e21) -]} - -@see MDN -*) -(* equivalent to `toString` I think *) - -external toPrecisionWithPrecision : float -> digits:int -> string - = "toPrecision" -[@@mel.send] +external toPrecision : ?digits:int -> string = "toPrecision" +[@@mel.send.pipe: t] (** Formats a [float] using some fairly arbitrary rules {b digits} specifies how many digits should appear in total. The @@ -189,41 +125,26 @@ 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 +[toPrecision] differs from [toFixed] 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 +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"?) {[ - (* prints "1e+4" *) - let _ = Js.log (Js.Float.toPrecisionWithPrecision 12345.6789 ~digits:1) - - (* prints "0.0" *) - let _ = Js.log (Js.Float.toPrecisionWithPrecision 0. ~digits:2) + Js.Float.toPrecision 12345.6789 = "12345.6789" + Js.Float.toPrecision 1.2e21 = "1.2e+21" + Js.Float.toPrecision ~digits:1 12345.6789 = "1e+4" + Js.Float.toPrecision ~digits:2 0. = "0.0" ]} @see MDN *) -external toString : float -> string = "toString" -[@@mel.send] -(** Formats a [float] as a string - -{b Returns} a [string] representing the given value in fixed-point (usually) - -{[ - (* prints "12345.6789" *) - let _ = Js.log (Js.Float.toString 12345.6789) -]} - -@see MDN -*) - -external toStringWithRadix : float -> radix:int -> string = "toString" -[@@mel.send] +external toString : ?radix:int -> string = "toString" +[@@mel.send.pipe: t] (** Formats a [float] as a string {b radix} specifies the radix base to use for the formatted number. The @@ -234,51 +155,30 @@ 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.Float.toStringWithRadix 6. ~radix:2) - - (* prints "11.001000111101011100001010001111010111000010100011111" *) - let _ = Js.log (Js.Float.toStringWithRadix 3.14 ~radix:2) - - (* prints "deadbeef" *) - let _ = Js.log (Js.Float.toStringWithRadix 3735928559. ~radix:16) - - (* prints "3f.gez4w97ry0a18ymf6qadcxr" *) - let _ = Js.log (Js.Float.toStringWithRadix 123.456 ~radix:36) + Js.Float.toString 12345.6789 = "12345.6789" + Js.Float.toString ~radix:2 6. = "110" + Js.Float.toString ~radix:2 3.14 = "11.001000111101011100001010001111010111000010100011111" + Js.Float.toString ~radix:16 3735928559. = "deadbeef" + Js.Float.toString ~radix:36 123.456 = "3f.gez4w97ry0a18ymf6qadcxr" ]} @see MDN *) -external fromString : string -> float = "Number" +external fromString : string -> t = "Number" (** Parses the given [string] into a [float] using JavaScript semantics {b Returns} the number as a [float] if successfully parsed, [_NaN] otherwise. {[ -(* returns 123 *) -let _ = Js.Float.fromString "123" - -(* returns 12.3 *) -let _ = Js.Float.fromString "12.3" - -(* returns 0 *) -let _ = Js.Float.fromString "" - -(* returns 17 *) -let _ = Js.Float.fromString "0x11" - -(* returns 3 *) -let _ = Js.Float.fromString "0b11" - -(* returns 9 *) -let _ = Js.Float.fromString "0o11" - -(* returns [_NaN] *) -let _ = Js.Float.fromString "foo" - -(* returns [_NaN] *) -let _ = Js.Float.fromString "100a" +Js.Float.fromString "123" = 123. +Js.Float.fromString "12.3" = 12.3 +Js.Float.fromString "" = 0. +Js.Float.fromString "0x11" = 17. +Js.Float.fromString "0b11" = 3. +Js.Float.fromString "0o11" = 9. +Js.Float.fromString "foo" = _NaN +Js.Float.fromString "100a" = _NaN ]} *) diff --git a/jscomp/test/dist/jscomp/test/js_float_test.js b/jscomp/test/dist/jscomp/test/js_float_test.js index 7871d2a911..b971f7a4f3 100644 --- a/jscomp/test/dist/jscomp/test/js_float_test.js +++ b/jscomp/test/dist/jscomp/test/js_float_test.js @@ -88,7 +88,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "1.23456e+2", - _1: (123.456).toExponential() + _1: (123.456).toExponential(undefined) }; }) ], @@ -99,7 +99,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "1.2e+21", - _1: (1.2e21).toExponential() + _1: (1.2e21).toExponential(undefined) }; }) ], @@ -167,7 +167,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "123", - _1: (123.456).toFixed() + _1: (123.456).toFixed(undefined) }; }) ], @@ -178,7 +178,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "1.2e+21", - _1: (1.2e21).toFixed() + _1: (1.2e21).toFixed(undefined) }; }) ], @@ -246,7 +246,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "123.456", - _1: (123.456).toPrecision() + _1: (123.456).toPrecision(undefined) }; }) ], @@ -257,7 +257,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "1.2e+21", - _1: (1.2e21).toPrecision() + _1: (1.2e21).toPrecision(undefined) }; }) ], @@ -325,7 +325,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "1.23", - _1: (1.23).toString() + _1: (1.23).toString(undefined) }; }) ], @@ -336,7 +336,7 @@ var suites_1 = { return { TAG: /* Eq */0, _0: "1.2e+21", - _1: (1.2e21).toString() + _1: (1.2e21).toString(undefined) }; }) ], diff --git a/jscomp/test/js_float_test.ml b/jscomp/test/js_float_test.ml index 50fa5f58b2..a6f54a6289 100644 --- a/jscomp/test/js_float_test.ml +++ b/jscomp/test/js_float_test.ml @@ -23,45 +23,45 @@ let suites = Mt.[ ("toExponential - large number", (fun _ -> Eq("1.2e+21", toExponential 1.2e21))); ("toExponentialWithPrecision - digits:2", (fun _ -> - Eq("1.23e+2", toExponentialWithPrecision 123.456 ~digits:2))); + Eq("1.23e+2", toExponential 123.456 ~digits:2))); ("toExponentialWithPrecision - digits:4", (fun _ -> - Eq("1.2346e+2", toExponentialWithPrecision 123.456 ~digits:4))); + Eq("1.2346e+2", toExponential 123.456 ~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)))); ("toFixed", (fun _ -> Eq("123", toFixed 123.456))); ("toFixed - large number", (fun _ -> Eq("1.2e+21", toFixed 1.2e21))); ("toFixedWithPrecision - digits:2", (fun _ -> - Eq("123.46", toFixedWithPrecision 123.456 ~digits:2))); + Eq("123.46", toFixed 123.456 ~digits:2))); ("toFixedWithPrecision - digits:4", (fun _ -> - Eq("123.4560", toFixedWithPrecision 123.456 ~digits:4))); + Eq("123.4560", toFixed 123.456 ~digits:4))); ("toFixedWithPrecision - digits:20", (fun _ -> - Eq("0.00000000000000000000", toFixedWithPrecision 0. ~digits:20))); + Eq("0.00000000000000000000", toFixed 0. ~digits:20))); ("toFixedWithPrecision - digits:101", (fun _ -> - ThrowAny(fun () -> ignore @@ toFixedWithPrecision 0. ~digits:101))); + ThrowAny(fun () -> ignore @@ toFixed 0. ~digits:101))); ("toFixedWithPrecision - digits:-1", (fun _ -> - ThrowAny(fun () -> ignore @@ toFixedWithPrecision 0. ~digits:(-1)))); + ThrowAny(fun () -> ignore @@ toFixed 0. ~digits:(-1)))); ("toPrecision", (fun _ -> Eq("123.456", toPrecision 123.456))); ("toPrecision - large number", (fun _ -> Eq("1.2e+21", toPrecision 1.2e21))); ("toPrecisionWithPrecision - digits:2", (fun _ -> - Eq("1.2e+2", toPrecisionWithPrecision 123.456 ~digits:2))); + Eq("1.2e+2", toPrecision 123.456 ~digits:2))); ("toPrecisionWithPrecision - digits:4", (fun _ -> - Eq("123.5", toPrecisionWithPrecision 123.456 ~digits:4))); + Eq("123.5", toPrecision 123.456 ~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("1.23", toString 1.23))); @@ -69,17 +69,17 @@ let suites = Mt.[ Eq("1.2e+21", toString 1.2e21))); ("toStringWithRadix - radix:2", (fun _ -> Eq( "1111011.0111010010111100011010100111111011111001110111", - toStringWithRadix 123.456 ~radix:2))); + toString 123.456 ~radix:2))); ("toStringWithRadix - radix:16", (fun _ -> - Eq("7b.74bc6a7ef9dc", toStringWithRadix 123.456 ~radix:16))); + Eq("7b.74bc6a7ef9dc", toString 123.456 ~radix:16))); ("toStringWithRadix - radix:36", (fun _ -> - Eq("3f", toStringWithRadix 123. ~radix:36))); + Eq("3f", toString 123. ~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)))); ("fromString - 123", (fun _ -> Eq(123., fromString "123")));