-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathjs.res
262 lines (193 loc) · 8.12 KB
/
js.res
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
/* 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. */
@@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/array2) 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:
## Examples
```rescript
/* 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.
*/
/** Provide utilities for `Js.null<'a>` */
module Null = Js_null
/** Provide utilities for `Js.undefined<'a>` */
module Undefined = Js_undefined
/** Provide utilities for `Js.null_undefined` */
module Nullable = Js_null_undefined
module Null_undefined = Js_null_undefined
/** Provide utilities for dealing with Js exceptions */
module Exn = Js_exn
/** Provide bindings to JS array*/
module Array = Js_array
/** Provide bindings to JS array*/
module Array2 = Js_array2
/** Provide bindings to JS string */
module String = Js_string
/** Provide bindings to JS string */
module String2 = Js_string2
/** Provide bindings to JS regex expression */
module Re = Js_re
/** Provide bindings to JS Promise */
module Promise = Js_promise
/** Provide bindings to JS Promise */
module Promise2 = Js_promise2
/** Provide bindings for JS Date */
module Date = Js_date
/** Provide utilities for JS dictionary object */
module Dict = Js_dict
/** Provide bindings to JS global functions in global namespace*/
module Global = Js_global
/** Provide utilities for json */
module Json = Js_json
/** Provide bindings for JS `Math` object */
module Math = Js_math
/** Provide utilities for `Js.t` */
module Obj = Js_obj
/** Provide bindings for JS typed array */
module Typed_array = Js_typed_array
/** Provide bindings for JS typed array */
module TypedArray2 = Js_typed_array2
/** Provide utilities for manipulating JS types */
module Types = Js_types
/** Provide utilities for JS float */
module Float = Js_float
/** Provide utilities for int */
module Int = Js_int
/** Provide utilities for bigint */
module BigInt = Js_bigint
/** Provide utilities for File */
module File = Js_file
/** Provide utilities for Blob */
module Blob = Js_blob
/** Provide utilities for option */
module Option = Js_option
/** Define the interface for result */
module Result = Js_result
/** Provides bindings for console */
module Console = Js_console
/** Provides bindings for ES6 Set */
module Set = Js_set
/** Provides bindings for ES6 WeakSet */
module WeakSet = Js_weakset
/** Provides bindings for ES6 Map */
module Map = Js_map
/** Provides bindings for ES6 WeakMap */
module WeakMap = Js_weakmap
/** JS object type */
type t<'a> = {..} as 'a
/** JS global object reference */
@val
external globalThis: t<'a> = "globalThis"
@unboxed
type null<+'a> = Js_null.t<'a> = Value('a) | @as(null) Null
type undefined<+'a> = Js_undefined.t<'a>
@unboxed
type nullable<+'a> = Js_null_undefined.t<'a> = Value('a) | @as(null) Null | @as(undefined) Undefined
type null_undefined<+'a> = nullable<'a>
external toOption: nullable<'a> => option<'a> = "%nullable_to_opt"
external undefinedToOption: undefined<'a> => option<'a> = "%undefined_to_opt"
external nullToOption: null<'a> => option<'a> = "%null_to_opt"
external isNullable: nullable<'a> => bool = "%is_nullable"
external import: 'a => promise<'a> = "%import"
/** The same as {!test} except that it is more permissive on the types of input */
external testAny: 'a => bool = "%is_nullable"
/**
The promise type, defined here for interoperation across packages.
*/
type promise<+'a, +'e>
/**
The same as empty in `Js.Null`. Compiles to `null`.
*/
external null: null<'a> = "%null"
/**
The same as empty `Js.Undefined`. Compiles to `undefined`.
*/
external undefined: undefined<'a> = "%undefined"
/**
`typeof x` will be compiled as `typeof x` in JS. Please consider functions in
`Js.Types` for a type safe way of reflection.
*/
external typeof: 'a => string = "%typeof"
@val @scope("console") /** Equivalent to console.log any value. */
external log: 'a => unit = "log"
@val @scope("console") external log2: ('a, 'b) => unit = "log"
@val @scope("console") external log3: ('a, 'b, 'c) => unit = "log"
@val @scope("console") external log4: ('a, 'b, 'c, 'd) => unit = "log"
@val @scope("console") @variadic /** A convenience function to console.log more than 4 arguments */
external logMany: array<'a> => unit = "log"
external eqNull: ('a, null<'a>) => bool = "%equal_null"
external eqUndefined: ('a, undefined<'a>) => bool = "%equal_undefined"
external eqNullable: ('a, nullable<'a>) => bool = "%equal_nullable"
/* ## Operators */
/**
`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_lt: ('a, 'a) => bool = "%unsafe_lt"
/**
`unsafe_le(a, b)` will be compiled as `a <= b`.
See also `Js.unsafe_lt`.
*/
external unsafe_le: ('a, 'a) => bool = "%unsafe_le"
/**
`unsafe_gt(a, b)` will be compiled as `a > b`.
See also `Js.unsafe_lt`.
*/
external unsafe_gt: ('a, 'a) => bool = "%unsafe_gt"
/**
`unsafe_ge(a, b)` will be compiled as `a >= b`.
See also `Js.unsafe_lt`.
*/
external unsafe_ge: ('a, 'a) => bool = "%unsafe_ge"