Js
The Js module mostly contains Reason 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, even when their behavior should be considered unsafe (e.g. modifying an array in place using Js.Array.push).
Therefore, when a correpsonding module is available in the Belt standard library, it is recommended to use the Belt version rather than the Js version. For example, you should prefer Belt.Array to Js.Array and Belt.Map.String to Js.Dict.
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:
RE/* Js.String (data-last API used with pipe last operator) */
Js.log("2019-11-10" |> Js.String.split("-"));
Js.log("Reason" |> Js.String.startsWith("Re"));
/* Js.String (data-last API used with pipe first operator) */
Js.log("2019-11-10"->Js.String.split("-", _));
Js.log("Reason"->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", "Reason"));
Js.Xxx2 Modules
For some modules with data-last argument order (e.g. Js.String),
there currently exists a matching module suffixed with "2" (e.g. Js.String2)
that uses data-first argument order.
These Js.Xxx2
modules are non-public API, so their use is discouraged.
Object
REtype t(+'a);
Js object type.
RElet x: {
.
"x": int,
"y": int,
} = [%obj {x: 1, y: 2}];
Nullable and Undefined
REtype null(+'a);
nullable, value of this type can be either null or 'a this type is the same as type t in Js.Null
REtype undefined(+'a);
value of this type can be either undefined or 'a this type is the same as type t in Js.Undefined
REtype nullable(+'a);
value of this type can be undefined, null or 'a this type is the same as type t n Js.Null_undefined
REtype null_undefined('a) = Js.nullable('a);
RElet toOption: Js.nullable('a) => option('a);
RElet undefinedToOption: Js.undefined('a) => option('a);
RElet nullToOption: Js.null('a) => option('a);
RElet test: Js.nullable('a) => bool;
RElet isNullable: Js.nullable('a) => bool;
RElet testAny: 'a => bool;
The same as Js.test
except that it is more permissive on the types of input.
REtype promise(+'a, +'e);
Deprecated. please use Js.Promise
.
The promise type, defined here for interoperation across packages.
RElet null: Js.null('a);
The same as empty in Js.Null
. Will be compiled as null
.
RElet undefined: Js.undefined('a);
The same as empty Js.Undefined
. Will be compiled as undefined
.
TypeOf
RElet 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.
Logging
RElet log: 'a => unit;
let log2: ('a, 'b) => unit;
let log3: ('a, 'b, 'c) => unit;
let log4: ('a, 'b, 'c, 'd) => unit;
A convenience function to log everything.
RElet logMany: array('a) => unit;
A convenience function to log more than 4 arguments
Comparison
RElet eqNull: ('a, null('a)) => bool;
let eqUndefined: ('a, undefined('a)) => bool;
let eqNullable: ('a, nullable('a)) => bool;
RElet 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.
RElet unsafe_le: ('a, 'a) => bool;
unsafe_le a b
will be compiled as a <= b
. See also Js.unsafe_lt
.
RElet unsafe_gt: ('a, 'a) => bool;
unsafe_gt a b
will be compiled as a > b
. See also Js.unsafe_lt
.
RElet unsafe_ge: ('a, 'a) => bool;
unsafe_ge a b
will be compiled as a >= b
. See also Js.unsafe_lt
.