Js
The Js module mostly contains ReScript bindings to standard JavaScript APIs like console.log, or the JavaScript String, Date, and 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 over Belt.Array
Argument Order
For historical reasons, some APIs in the Js namespace (e.g. Js.String) are using the data-last argument order whereas others (e.g. Js.Date) are using data-first.
For more information about these argument orders and the trade-offs between them, see this blog post.
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.
t
type t<'a> = 'a
constraint 'a = {..}
JS object type
null
type null<'a> = Value('a) | Null
Nullable value of this type can be either null or 'a. This type is equivalent to Js.Null.t.
undefined
type undefined<+'a>
A value of this type can be either undefined or 'a. This type is equivalent to Js.Undefined.t.
nullable
type nullable<'a> = Value('a) | Null | Undefined
null_undefined
type null_undefined<'a> = nullable<'a>
toOption
let toOption: nullable<'a> => option<'a>
undefinedToOption
let undefinedToOption: undefined<'a> => option<'a>
nullToOption
let nullToOption: null<'a> => option<'a>
isNullable
let isNullable: nullable<'a> => bool
import
let import: 'a => promise<'a>
testAny
let testAny: 'a => bool
The same as {!test} except that it is more permissive on the types of input
promise
type promise<+'a, +'e>
The promise type, defined here for interoperation across packages.
null
let null: null<'a>
The same as empty in Js.Null
. Compiles to null
.
undefined
let undefined: undefined<'a>
The same as empty Js.Undefined
. Compiles to undefined
.
typeof
let typeof: 'a => string
typeof x
will be compiled as typeof x
in JS. Please consider functions in
Js.Types
for a type safe way of reflection.
log
let log: 'a => unit
Equivalent to console.log any value.
log2
let log2: ('a, 'b) => unit
log3
let log3: ('a, 'b, 'c) => unit
log4
let log4: ('a, 'b, 'c, 'd) => unit
logMany
let logMany: array<'a> => unit
A convenience function to console.log more than 4 arguments
eqNull
let eqNull: ('a, null<'a>) => bool
eqUndefined
let eqUndefined: ('a, undefined<'a>) => bool
eqNullable
let eqNullable: ('a, nullable<'a>) => bool
unsafe_lt
let unsafe_lt: ('a, 'a) => bool
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
unsafe_le
let unsafe_le: ('a, 'a) => bool
unsafe_le(a, b)
will be compiled as a <= b
.
See also Js.unsafe_lt
.
unsafe_gt
let unsafe_gt: ('a, 'a) => bool
unsafe_gt(a, b)
will be compiled as a > b
.
See also Js.unsafe_lt
.
unsafe_ge
let unsafe_ge: ('a, 'a) => bool
unsafe_ge(a, b)
will be compiled as a >= b
.
See also Js.unsafe_lt
.