Skip to content

Commit a4d14b2

Browse files
committed
Added string conversion functions to Js_int
1 parent 6e39020 commit a4d14b2

File tree

10 files changed

+521
-73
lines changed

10 files changed

+521
-73
lines changed

Diff for: jscomp/runtime/js.ml

+2
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ module Boolean = Js_boolean
104104
module Math = Js_math
105105
module Date = Js_date
106106
module Global = Js_global
107+
module Float = Js_float
108+
module Int = Js_int

Diff for: jscomp/runtime/js.mli

+2
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ module Boolean = Js_boolean
104104
module Math = Js_math
105105
module Date = Js_date
106106
module Global = Js_global
107+
module Float = Js_float
108+
module Int = Js_int

Diff for: jscomp/runtime/js_float.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
(** Provides functions for inspecting and manipulating floats
25+
(** Provides functions for inspecting and manipulating [float]s
2626
*)
2727

2828
(** The special value "Not a Number"
@@ -169,7 +169,7 @@ external toPrecision : float -> string = "" [@@bs.send] (* equivalent to `toStri
169169
170170
{b digits} specifies how many digits should appear in total. The
171171
value must between 0 and some arbitrary number that's hopefully at least larger
172-
than 20 (for Node it's 21. Why? WHo knows).
172+
than 20 (for Node it's 21. Why? Who knows).
173173
174174
{b Returns} a [string] representing the given value in fixed-point or scientific notation
175175

Diff for: jscomp/runtime/js_int.ml

+126
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,136 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
(** Provides functions for inspecting and manipulating [int]s *)
2526

2627
(** If we use number, we need coerce to int32 by adding `|0`,
2728
otherwise `+0` can be wrong.
2829
Most JS API is float oriented, it may overflow int32 or
2930
comes with [NAN]
3031
*)
3132
(* + conversion*)
33+
34+
(** Formats an [int] using exponential (scientific) notation
35+
36+
{b Returns} a [string] representing the given value in exponential notation
37+
38+
@raise RangeError if digits is not in the range \[0, 20\] (inclusive)
39+
40+
@example {[
41+
(* prints "7.7e+1" *)
42+
let _ = Js.log \@\@ Js.Int.toExponential 77
43+
]}
44+
45+
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential> MDN
46+
*)
47+
external toExponential : int -> string = "" [@@bs.send]
48+
49+
(** Formats an [int] using exponential (scientific) notation
50+
51+
{b digits} specifies how many digits should appear after the decimal point. The
52+
value must be in the range \[0, 20\] (inclusive).
53+
54+
{b Returns} a [string] representing the given value in exponential notation
55+
56+
The output will be rounded or padded with zeroes if necessary.
57+
58+
@raise RangeError if digits is not in the range \[0, 20\] (inclusive)
59+
60+
@example {[
61+
(* prints "7.70e+1" *)
62+
let _ = Js.log \@\@ Js.Int.toExponentialWithPrecision 77 ~digits:2
63+
64+
(* prints "5.68e+3" *)
65+
let _ = Js.log \@\@ Js.Int.toExponentialWithPrecision 5678 ~digits:2
66+
]}
67+
68+
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toExponential> MDN
69+
*)
70+
external toExponentialWithPrecision : int -> digits:int -> string = "toExponential" [@@bs.send]
71+
72+
(** Formats a [int] using some fairly arbitrary rules
73+
74+
{b Returns} a [string] representing the given value in fixed-point (usually)
75+
76+
[toPrecision] differs from [toFixed] in that the former will format the number
77+
with full precision, while the latter will not output any digits after the
78+
decimal point.
79+
80+
@raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?)
81+
82+
@example {[
83+
(* prints "123456789" *)
84+
let _ = Js.log \@\@ Js.Int.toPrecision 123456789
85+
]}
86+
87+
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision> MDN
88+
*)
89+
external toPrecision : int -> string = "" [@@bs.send] (* equivalent to `toString` I think *)
90+
91+
(** Formats an [int] using some fairly arbitrary rules
92+
93+
{b digits} specifies how many digits should appear in total. The
94+
value must between 0 and some arbitrary number that's hopefully at least larger
95+
than 20 (for Node it's 21. Why? Who knows).
96+
97+
{b Returns} a [string] representing the given value in fixed-point or scientific notation
98+
99+
The output will be rounded or padded with zeroes if necessary.
100+
101+
[toPrecisionWithPrecision] differs from [toFixedWithPrecision] in that the former
102+
will count all digits against the precision, while the latter will count only
103+
the digits after the decimal point. [toPrecisionWithPrecision] will also use
104+
scientific notation if the specified precision is less than the number for digits
105+
before the decimal point.
106+
107+
@raise RangeError if digits is not in the range accepted by this function (what do you mean "vague"?)
108+
109+
@example {[
110+
(* prints "1.2e+8" *)
111+
let _ = Js.log \@\@ Js.Int.toPrecisionWithPrecision 123456789 ~digits:2
112+
113+
(* prints "0.0" *)
114+
let _ = Js.log \@\@ Js.Int.toPrecisionWithPrecision 0 ~digits:2
115+
]}
116+
117+
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision> MDN
118+
*)
119+
external toPrecisionWithPrecision : int -> digits:int -> string = "toPrecision" [@@bs.send]
120+
121+
122+
(** Formats a [int] as a string
123+
124+
{b Returns} a [string] representing the given value in fixed-point (usually)
125+
126+
@example {[
127+
(* prints "123456789" *)
128+
let _ = Js.log \@\@ Js.Int.toString 123456789
129+
]}
130+
131+
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString> MDN
132+
*)
133+
external toString : int -> string = "" [@@bs.send]
134+
135+
(** Formats an [int] as a string
136+
137+
{b radix} specifies the radix base to use for the formatted number. The
138+
value must be in the range \[2, 36\] (inclusive).
139+
140+
{b Returns} a [string] representing the given value in fixed-point (usually)
141+
142+
@raise RangeError if radix is not in the range \[2, 36\] (inclusive)
143+
144+
@example {[
145+
(* prints "110" *)
146+
let _ = Js.log \@\@ Js.Int.toStringWithRadix 6 ~radix:2
147+
148+
(* prints "deadbeef" *)
149+
let _ = Js.log \@\@ Js.Int.toStringWithRadix 3735928559 ~radix:16
150+
151+
(* prints "2n9c" *)
152+
let _ = Js.log \@\@ Js.Int.toStringWithRadix 123456 ~radix:36
153+
]}
154+
155+
@see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString> MDN
156+
*)
157+
external toStringWithRadix : int -> radix:int -> string = "toString" [@@bs.send]

Diff for: jscomp/test/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ OTHERS := literals a test_ari test_export2 test_internalOO test_obj_simple_ffi t
113113
undef_regression2_test\
114114
js_global_test\
115115
js_float_test\
116+
js_int_test\
116117
bang_primitive\
117118
compare_test
118119

Diff for: jscomp/test/js_float_test.js

+36-36
Original file line numberDiff line numberDiff line change
@@ -76,140 +76,140 @@ var suites_001 = /* :: */[
7676
],
7777
/* :: */[
7878
/* tuple */[
79-
"toFixed",
79+
"toExponential",
8080
function () {
8181
return /* Eq */Block.__(0, [
82-
"123",
83-
(123.456).toFixed()
82+
"1.23456e+2",
83+
(123.456).toExponential()
8484
]);
8585
}
8686
],
8787
/* :: */[
8888
/* tuple */[
89-
"toFixed - large number",
89+
"toExponential - large number",
9090
function () {
9191
return /* Eq */Block.__(0, [
9292
"1.2e+21",
93-
(1.2e21).toFixed()
93+
(1.2e21).toExponential()
9494
]);
9595
}
9696
],
9797
/* :: */[
9898
/* tuple */[
99-
"toFixedWithPrecision - digits:2",
99+
"toExponentialWithPrecision - digits:2",
100100
function () {
101101
return /* Eq */Block.__(0, [
102-
"123.46",
103-
(123.456).toFixed(2)
102+
"1.23e+2",
103+
(123.456).toExponential(2)
104104
]);
105105
}
106106
],
107107
/* :: */[
108108
/* tuple */[
109-
"toFixedWithPrecision - digits:4",
109+
"toExponentialWithPrecision - digits:4",
110110
function () {
111111
return /* Eq */Block.__(0, [
112-
"123.4560",
113-
(123.456).toFixed(4)
112+
"1.2346e+2",
113+
(123.456).toExponential(4)
114114
]);
115115
}
116116
],
117117
/* :: */[
118118
/* tuple */[
119-
"toFixedWithPrecision - digits:20",
119+
"toExponentialWithPrecision - digits:20",
120120
function () {
121121
return /* Eq */Block.__(0, [
122-
"0.00000000000000000000",
123-
(0).toFixed(20)
122+
"0.00000000000000000000e+0",
123+
(0).toExponential(20)
124124
]);
125125
}
126126
],
127127
/* :: */[
128128
/* tuple */[
129-
"toFixedWithPrecision - digits:21",
129+
"toExponentialWithPrecision - digits:21",
130130
function () {
131131
return /* ThrowAny */Block.__(7, [function () {
132-
(0).toFixed(21);
132+
(0).toExponential(21);
133133
return /* () */0;
134134
}]);
135135
}
136136
],
137137
/* :: */[
138138
/* tuple */[
139-
"toFixedWithPrecision - digits:-1",
139+
"toExponentialWithPrecision - digits:-1",
140140
function () {
141141
return /* ThrowAny */Block.__(7, [function () {
142-
(0).toFixed(-1);
142+
(0).toExponential(-1);
143143
return /* () */0;
144144
}]);
145145
}
146146
],
147147
/* :: */[
148148
/* tuple */[
149-
"toExponential",
149+
"toFixed",
150150
function () {
151151
return /* Eq */Block.__(0, [
152-
"1.23456e+2",
153-
(123.456).toExponential()
152+
"123",
153+
(123.456).toFixed()
154154
]);
155155
}
156156
],
157157
/* :: */[
158158
/* tuple */[
159-
"toExponential - large number",
159+
"toFixed - large number",
160160
function () {
161161
return /* Eq */Block.__(0, [
162162
"1.2e+21",
163-
(1.2e21).toExponential()
163+
(1.2e21).toFixed()
164164
]);
165165
}
166166
],
167167
/* :: */[
168168
/* tuple */[
169-
"toExponentialWithPrecision - digits:2",
169+
"toFixedWithPrecision - digits:2",
170170
function () {
171171
return /* Eq */Block.__(0, [
172-
"1.23e+2",
173-
(123.456).toExponential(2)
172+
"123.46",
173+
(123.456).toFixed(2)
174174
]);
175175
}
176176
],
177177
/* :: */[
178178
/* tuple */[
179-
"toExponentialWithPrecision - digits:4",
179+
"toFixedWithPrecision - digits:4",
180180
function () {
181181
return /* Eq */Block.__(0, [
182-
"1.2346e+2",
183-
(123.456).toExponential(4)
182+
"123.4560",
183+
(123.456).toFixed(4)
184184
]);
185185
}
186186
],
187187
/* :: */[
188188
/* tuple */[
189-
"toExponentialWithPrecision - digits:20",
189+
"toFixedWithPrecision - digits:20",
190190
function () {
191191
return /* Eq */Block.__(0, [
192-
"0.00000000000000000000e+0",
193-
(0).toExponential(20)
192+
"0.00000000000000000000",
193+
(0).toFixed(20)
194194
]);
195195
}
196196
],
197197
/* :: */[
198198
/* tuple */[
199-
"toExponentialWithPrecision - digits:21",
199+
"toFixedWithPrecision - digits:21",
200200
function () {
201201
return /* ThrowAny */Block.__(7, [function () {
202-
(0).toExponential(21);
202+
(0).toFixed(21);
203203
return /* () */0;
204204
}]);
205205
}
206206
],
207207
/* :: */[
208208
/* tuple */[
209-
"toExponentialWithPrecision - digits:-1",
209+
"toFixedWithPrecision - digits:-1",
210210
function () {
211211
return /* ThrowAny */Block.__(7, [function () {
212-
(0).toExponential(-1);
212+
(0).toFixed(-1);
213213
return /* () */0;
214214
}]);
215215
}

0 commit comments

Comments
 (0)