forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.ml
280 lines (209 loc) · 8.65 KB
/
js.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
[@@@bs.config { flags = [| "-unboxed-types"; "-w"; "-49" |] }]
(* DESIGN:
- It does not have any code, all its code will be inlined so that
there will never be
{[ require('js')]}
- Its interface should be minimal
*)
(**
The Js module mostly contains ReScript bindings to _standard JavaScript APIs_
like [console.log](https://developer.mozilla.org/en-US/docs/Web/API/Console/log),
or the JavaScript
[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String),
[Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date), and
[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)
classes.
It is meant as a zero-abstraction interop layer and directly exposes JavaScript functions as they are. If you can find your API in this module, prefer this over an equivalent Belt helper. For example, prefer [Js.Array2](js/array-2) over [Belt.Array](belt/array)
## Argument Order
For historical reasons, some APIs in the Js namespace (e.g. [Js.String](js/string)) are
using the data-last argument order whereas others (e.g. [Js.Date](js/date)) are using data-first.
For more information about these argument orders and the trade-offs between them, see
[this blog post](https://www.javierchavarri.com/data-first-and-data-last-a-comparison/).
_Eventually, all modules in the Js namespace are going to be migrated to data-first though._
In the meantime, there are several options for dealing with the data-last APIs:
```res example
/* Js.String (data-last API used with pipe last operator) */
Js.log("2019-11-10" |> Js.String.split("-"))
Js.log("ReScript" |> Js.String.startsWith("Re"))
/* Js.String (data-last API used with pipe first operator) */
Js.log("2019-11-10"->Js.String.split("-", _))
Js.log("ReScript"->Js.String.startsWith("Re", _))
/* Js.String (data-last API used without any piping) */
Js.log(Js.String.split("-", "2019-11-10"))
Js.log(Js.String.startsWith("Re", "ReScript"))
```
## Js.Xxx2 Modules
Prefer `Js.Array2` over `Js.Array`, `Js.String2` over `Js.String`, etc. The latters are old modules.
*)
type 'a t = < .. > as 'a
(** JS object type *)
module MapperRt = Js_mapperRt
module Internal = struct
external opaqueFullApply : 'a -> 'a = "%uncurried_apply"
(* Use opaque instead of [._n] to prevent some optimizations happening *)
external run : (unit -> 'a [@bs]) -> 'a = "#run"
external opaque : 'a -> 'a = "%opaque"
end
(**/**)
type +'a null
(**
Nullable value of this type can be either null or 'a. This type is equivalent to Js.Null.t.
*)
type +'a undefined
(**
A value of this type can be either undefined or 'a. This type is equivalent to Js.Undefined.t.
*)
type +'a nullable
(**
A value of this type can be undefined, null or 'a. This type is equivalent to Js.Null_undefined.t.
*)
type +'a null_undefined = 'a nullable
external toOption : 'a nullable -> 'a option = "#nullable_to_opt"
external undefinedToOption : 'a undefined -> 'a option = "#undefined_to_opt"
external nullToOption : 'a null -> 'a option = "#null_to_opt"
external isNullable : 'a nullable -> bool = "#is_nullable"
external testAny : 'a -> bool = "#is_nullable"
(** The same as {!test} except that it is more permissive on the types of input *)
type (+'a, +'e) promise
(**
The promise type, defined here for interoperation across packages.
@deprecated please use `Js.Promise`.
*)
external null : 'a null = "#null"
(**
The same as empty in `Js.Null`. Compiles to `null`.
*)
external undefined : 'a undefined = "#undefined"
(**
The same as empty `Js.Undefined`. Compiles to `undefined`.
*)
external typeof : 'a -> string = "#typeof"
(**
`typeof x` will be compiled as `typeof x` in JS. Please consider functions in
`Js.Types` for a type safe way of reflection.
*)
external log : 'a -> unit = "log"
[@@val] [@@scope "console"]
(** Equivalent to console.log any value. *)
external log2 : 'a -> 'b -> unit = "log" [@@bs.val] [@@bs.scope "console"]
external log3 : 'a -> 'b -> 'c -> unit = "log" [@@bs.val] [@@bs.scope "console"]
external log4 : 'a -> 'b -> 'c -> 'd -> unit = "log"
[@@bs.val] [@@bs.scope "console"]
external logMany : 'a array -> unit = "log"
[@@bs.val] [@@bs.scope "console"] [@@bs.splice]
(** A convenience function to console.log more than 4 arguments *)
external eqNull : 'a -> 'a null -> bool = "%bs_equal_null"
external eqUndefined : 'a -> 'a undefined -> bool = "%bs_equal_undefined"
external eqNullable : 'a -> 'a nullable -> bool = "%bs_equal_nullable"
(** ## Operators *)
external unsafe_lt : 'a -> 'a -> bool = "#unsafe_lt"
(**
`unsafe_lt(a, b)` will be compiled as `a < b`.
It is marked as unsafe, since it is impossible
to give a proper semantics for comparision which applies to any type
*)
external unsafe_le : 'a -> 'a -> bool = "#unsafe_le"
(**
`unsafe_le(a, b) will be compiled as `a <= b`.
See also `Js.unsafe_lt`.
*)
external unsafe_gt : 'a -> 'a -> bool = "#unsafe_gt"
(**
`unsafe_gt(a, b)` will be compiled as `a > b`.
See also `Js.unsafe_lt`.
*)
external unsafe_ge : 'a -> 'a -> bool = "#unsafe_ge"
(**
`unsafe_ge(a, b)` will be compiled as `a >= b`.
See also `Js.unsafe_lt`.
*)
(** ## Nested Modules *)
module Null = Js_null
(** Provide utilities for `Js.null<'a>` *)
module Undefined = Js_undefined
(** Provide utilities for `Js.undefined<'a>` *)
module Nullable = Js_null_undefined
(** Provide utilities for `Js.null_undefined` *)
module Null_undefined = Js_null_undefined
(** @deprecated please use `Js.Nullable` *)
module Exn = Js_exn
(** Provide utilities for dealing with Js exceptions *)
module Array = Js_array
(** Provide bindings to JS array*)
module Array2 = Js_array2
(** Provide bindings to JS array*)
module String = Js_string
(** Provide bindings to JS string *)
module String2 = Js_string2
(** Provide bindings to JS string *)
module Re = Js_re
(** Provide bindings to JS regex expression *)
module Promise = Js_promise
(** Provide bindings to JS Promise *)
module Promise2 = Js_promise2
(** Provide bindings to JS Promise *)
module Date = Js_date
(** Provide bindings for JS Date *)
module Dict = Js_dict
(** Provide utilities for JS dictionary object *)
module Global = Js_global
(** Provide bindings to JS global functions in global namespace*)
module Json = Js_json
(** Provide utilities for json *)
module Math = Js_math
(** Provide bindings for JS `Math` object *)
module Obj = Js_obj
(** Provide utilities for `Js.t` *)
module Typed_array = Js_typed_array
(** Provide bindings for JS typed array *)
module TypedArray2 = Js_typed_array2
(** Provide bindings for JS typed array *)
module Types = Js_types
(** Provide utilities for manipulating JS types *)
module Float = Js_float
(** Provide utilities for JS float *)
module Int = Js_int
(** Provide utilities for int *)
module Bigint = Js_bigint
(** Provide utilities for bigint *)
module Option = Js_option
(** Provide utilities for option *)
module Result = Js_result
(** Define the interface for result *)
module List = Js_list
(** Provide utilities for list *)
module Vector = Js_vector
(** Provides bindings for JS Vector *)
module Console = Js_console
(** Provides bindings for console *)
module Set = Js_set
(** Provides bindings for ES6 Set *)
module WeakSet = Js_weakset
(** Provides bindings for ES6 WeakSet *)
module Map = Js_map
(** Provides bindings for ES6 Map *)
module WeakMap = Js_weakmap
(** Provides bindings for ES6 WeakMap *)