diff --git a/misc_docs/syntax/builtinfunctions_ignore.mdx b/misc_docs/syntax/builtinfunctions_ignore.mdx index d43b07c88..c2e8d0e6d 100644 --- a/misc_docs/syntax/builtinfunctions_ignore.mdx +++ b/misc_docs/syntax/builtinfunctions_ignore.mdx @@ -15,7 +15,7 @@ The `ignore()` function discards the value of its argument and returns `()`. ```res mySideEffect()->Promise.catch(handleError)->ignore -Js.Global.setTimeout(myFunc, 1000)->ignore +setTimeout(myFunc, 1000)->ignore ``` ```js diff --git a/misc_docs/syntax/decorator_val.mdx b/misc_docs/syntax/decorator_val.mdx index 396621b71..f8d0f387f 100644 --- a/misc_docs/syntax/decorator_val.mdx +++ b/misc_docs/syntax/decorator_val.mdx @@ -18,7 +18,7 @@ type timeoutID @val external setTimeout: (unit => unit, int) => timeoutID = "setTimeout" -let timeoutID = setTimeout(() => Js.log("Hello"), 1000) +let timeoutID = setTimeout(() => Console.log("Hello"), 1000) ``` ```js diff --git a/misc_docs/syntax/extension_regular_expression.mdx b/misc_docs/syntax/extension_regular_expression.mdx index eed9f0c77..a45ce5e9d 100644 --- a/misc_docs/syntax/extension_regular_expression.mdx +++ b/misc_docs/syntax/extension_regular_expression.mdx @@ -12,7 +12,7 @@ category: "extensionpoints" ```res let regex = %re("/^hello/") -let result = regex->Js.Re.test_("hello world") +let result = regex->Re.test("hello world") ``` ```js @@ -26,4 +26,4 @@ var result = regex.test("hello world"); * [Regular Expressions](/docs/manual/latest/primitive-types#regular-expression) * [Extension Point Attributes](/docs/manual/latest/attribute#extension-point) -* [Js.Re API](/docs/manual/latest/api/js/re) +* [Re API](/docs/manual/latest/api/core/re) diff --git a/misc_docs/syntax/language_async.mdx b/misc_docs/syntax/language_async.mdx index 616b9eff3..826fe261d 100644 --- a/misc_docs/syntax/language_async.mdx +++ b/misc_docs/syntax/language_async.mdx @@ -26,7 +26,7 @@ let logUserDetails = async (userId: string) => { await sendAnalytics(`User details have been logged for ${userId}`) - Js.log(`Email address for user ${userId}: ${email}`) + Console.log(`Email address for user ${userId}: ${email}`) } ``` diff --git a/misc_docs/syntax/language_await.mdx b/misc_docs/syntax/language_await.mdx index 611993648..94c9827eb 100644 --- a/misc_docs/syntax/language_await.mdx +++ b/misc_docs/syntax/language_await.mdx @@ -20,7 +20,7 @@ Use the `await` within an `async` function to unwrap a promise value in a seamin let fetchMessages = async () => { let message = await queryMessagesApi("message-id-1") - Js.log(message) + Console.log(message) } ``` diff --git a/misc_docs/syntax/language_open.mdx b/misc_docs/syntax/language_open.mdx index 0e52bc1b1..8df2b7fc5 100644 --- a/misc_docs/syntax/language_open.mdx +++ b/misc_docs/syntax/language_open.mdx @@ -15,10 +15,10 @@ In some cases, `open` will cause a "shadow warning" due to existing identifiers ```res -open Js.Math +open Math -// Use _PI and pow_float from the Js.Math module -let area = radius => _PI *. pow_float(~base=radius, ~exp=2.0) +// Use _PI and pow_float from the Math module +let area = radius => Constants.pi *. pow(radius, ~exp=2.0) ``` ```js diff --git a/misc_docs/syntax/language_placeholder.mdx b/misc_docs/syntax/language_placeholder.mdx index 7c016453e..26bd698cf 100644 --- a/misc_docs/syntax/language_placeholder.mdx +++ b/misc_docs/syntax/language_placeholder.mdx @@ -14,13 +14,13 @@ Placeholders may be used for [ignoring parts of values](/docs/manual/latest/patt ```res switch person1 { -| Teacher(_) => Js.log("Hi teacher") -| Student(_) => Js.log("Hey student") +| Teacher(_) => Console.log("Hi teacher") +| Student(_) => Console.log("Hey student") } ``` ```js -if (person1.TAG === /* Teacher */ 0) { +if (person1.TAG === "Teacher") { console.log("Hi teacher"); } else { console.log("Hey student"); diff --git a/misc_docs/syntax/language_switch.mdx b/misc_docs/syntax/language_switch.mdx index f04d74c21..751898f61 100644 --- a/misc_docs/syntax/language_switch.mdx +++ b/misc_docs/syntax/language_switch.mdx @@ -18,23 +18,20 @@ type shape = Circle(float) | Square(float) let shape = Square(3.0) let message = switch shape { -| Circle(radius) => "Circle with radius " ++ Js.Float.toString(radius) -| Square(length) => "Square with sides of length " ++ Js.Float.toString(length) +| Circle(radius) => "Circle with radius " ++ Float.toString(radius) +| Square(length) => "Square with sides of length " ++ Float.toString(length) } ``` ```js var shape = { - TAG: /* Square */ 1, - _0: 3.0, + TAG: "Square", + _0: 3.0 }; var message; -message = - shape.TAG === /* Circle */ 0 - ? "Circle with radius " + (3.0).toString() - : "Square with sides of length " + (3.0).toString(); +message = shape.TAG === "Circle" ? "Circle with radius " + (3.0).toString() : "Square with sides of length " + (3.0).toString(); ``` diff --git a/misc_docs/syntax/operators_pipe.mdx b/misc_docs/syntax/operators_pipe.mdx index 7b746878f..754205e72 100644 --- a/misc_docs/syntax/operators_pipe.mdx +++ b/misc_docs/syntax/operators_pipe.mdx @@ -14,11 +14,11 @@ The `->` operator passes a value into the first argument position of a function. ```res let dieRoll = size => { - Js.Math.random_int(1, size) + Math.Int.random(1, size) } let dieRollMessage = (value, name) => { - "Hi " ++ name ++ ", you rolled a " ++ Js.Int.toString(value) + "Hi " ++ name ++ ", you rolled a " ++ Int.toString(value) } let message = dieRoll(6)->dieRollMessage("Marshall") @@ -26,14 +26,14 @@ let message = dieRoll(6)->dieRollMessage("Marshall") ```js function dieRoll(size) { - return Js_math.random_int(1, size); + return Core__Math.Int.random(1, size); } function dieRollMessage(value, name) { return "Hi " + name + ", you rolled a " + value.toString(); } -var message = dieRollMessage(Js_math.random_int(1, 6), "Marshall"); +var message = dieRollMessage(dieRoll(6), "Marshall"); ``` @@ -46,7 +46,7 @@ You can also explicitly define the argument position of a piped value by using t ```res example let logMsg = (user: string, datestr: string, msg: string): unit => { - Js.log(`${user}|${datestr}|${msg}`) + Console.log(`${user}|${datestr}|${msg}`) } let datestr = "01-01-2021" @@ -54,20 +54,21 @@ let user = "admin" // Here, we put the result of toUpperCase into the last position // denoted with an _ -Js.String2.toUpperCase("example message")->logMsg(user, datestr, _) +String.toUpperCase("example message")->logMsg(user, datestr, _) ``` ```js function logMsg(user, datestr, msg) { console.log(user + "|" + datestr + "|" + msg); - } var datestr = "01-01-2021"; var user = "admin"; -logMsg(user, datestr, "example message".toUpperCase()); +((function (__x) { + logMsg(user, datestr, __x); + })("example message".toUpperCase())); ``` diff --git a/misc_docs/syntax/operators_triangle_pipe.mdx b/misc_docs/syntax/operators_triangle_pipe.mdx index b3db593ae..3bbfef0fc 100644 --- a/misc_docs/syntax/operators_triangle_pipe.mdx +++ b/misc_docs/syntax/operators_triangle_pipe.mdx @@ -16,11 +16,11 @@ The `|>` operator passes a value to a function as its last argument. ```res let dieRoll = size => { - Js.Math.random_int(1, size) + Math.Int.random(1, size) } let dieRollMessage = (name, value) => { - "Hi " ++ name ++ ", you rolled a " ++ Js.Int.toString(value) + "Hi " ++ name ++ ", you rolled a " ++ Int.toString(value) } let message = dieRoll(6) |> dieRollMessage("Jeremy") @@ -28,14 +28,14 @@ let message = dieRoll(6) |> dieRollMessage("Jeremy") ```js function dieRoll(size) { - return Js_math.random_int(1, size); + return Core__Math.Int.random(1, size); } function dieRollMessage(name, value) { return "Hi " + name + ", you rolled a " + value.toString(); } -var message = dieRollMessage("Jeremy", Js_math.random_int(1, 6)); +var message = dieRollMessage("Jeremy", dieRoll(6)); ``` diff --git a/misc_docs/syntax/specialvalues_file.mdx b/misc_docs/syntax/specialvalues_file.mdx index 7e116bd29..41242bcd6 100644 --- a/misc_docs/syntax/specialvalues_file.mdx +++ b/misc_docs/syntax/specialvalues_file.mdx @@ -12,7 +12,7 @@ category: "specialvalues" ```res example // testmodule.res -__FILE__->Js.log +__FILE__->Console.log ``` ```js diff --git a/misc_docs/syntax/specialvalues_line.mdx b/misc_docs/syntax/specialvalues_line.mdx index 878fa1909..c7038316f 100644 --- a/misc_docs/syntax/specialvalues_line.mdx +++ b/misc_docs/syntax/specialvalues_line.mdx @@ -13,7 +13,7 @@ category: "specialvalues" ```res example // Line 1 // Line 2 -__LINE__->Js.log +__LINE__->Console.log ``` ```js diff --git a/misc_docs/syntax/specialvalues_line_of.mdx b/misc_docs/syntax/specialvalues_line_of.mdx index 69067121c..909e5aa4c 100644 --- a/misc_docs/syntax/specialvalues_line_of.mdx +++ b/misc_docs/syntax/specialvalues_line_of.mdx @@ -16,16 +16,23 @@ category: "specialvalues" let f = () => None let (line, f') = __LINE_OF__(f) -line->Js.log -f'->Js.log +line->Console.log +f'->Console.log ``` ```js -function f(param) {} +function f() { + +} console.log(2); + console.log(f); + +var line = 2; + +var f$p = f; ``` \ No newline at end of file diff --git a/misc_docs/syntax/specialvalues_loc.mdx b/misc_docs/syntax/specialvalues_loc.mdx index 33444903b..df6c3cc4e 100644 --- a/misc_docs/syntax/specialvalues_loc.mdx +++ b/misc_docs/syntax/specialvalues_loc.mdx @@ -12,7 +12,7 @@ The string format is `"File %S, line %d, characters %d-%d"` ```res example -__LOC__->Js.log +__LOC__->Console.log ``` ```js diff --git a/misc_docs/syntax/specialvalues_loc_of.mdx b/misc_docs/syntax/specialvalues_loc_of.mdx index 0894a0efb..02e6ec9d7 100644 --- a/misc_docs/syntax/specialvalues_loc_of.mdx +++ b/misc_docs/syntax/specialvalues_loc_of.mdx @@ -16,17 +16,20 @@ category: "specialvalues" let f = () => None let (loc, f') = __LOC_OF__(f) -loc->Js.log -f'->Js.log +loc->Console.log +f'->Console.log ``` ```js -function f(param) {} +function f() { + +} var loc = "File \"testmodule.res\", line 2, characters 27-28"; console.log(loc); console.log(f); +var f$p = f; ``` \ No newline at end of file diff --git a/misc_docs/syntax/specialvalues_module.mdx b/misc_docs/syntax/specialvalues_module.mdx index 4f5d62eb4..fed675941 100644 --- a/misc_docs/syntax/specialvalues_module.mdx +++ b/misc_docs/syntax/specialvalues_module.mdx @@ -11,7 +11,7 @@ category: "specialvalues" ```res example -__MODULE__->Js.log +__MODULE__->Console.log ``` ```js diff --git a/misc_docs/syntax/specialvalues_pos.mdx b/misc_docs/syntax/specialvalues_pos.mdx index e1035473b..078e5e80b 100644 --- a/misc_docs/syntax/specialvalues_pos.mdx +++ b/misc_docs/syntax/specialvalues_pos.mdx @@ -15,10 +15,10 @@ It basically provides the same information as `__LOC__`, but separated into mult ```res example let (fileName, lineNumber, columnNumberStart, columnNumberEnd) = __POS__ -fileName->Js.log -lineNumber->Js.log -columnNumberStart->Js.log -columnNumberEnd->Js.log +fileName->Console.log +lineNumber->Console.log +columnNumberStart->Console.log +columnNumberEnd->Console.log ``` ```js diff --git a/misc_docs/syntax/specialvalues_pos_of.mdx b/misc_docs/syntax/specialvalues_pos_of.mdx index 74a3d23c8..c8524de33 100644 --- a/misc_docs/syntax/specialvalues_pos_of.mdx +++ b/misc_docs/syntax/specialvalues_pos_of.mdx @@ -19,8 +19,8 @@ It basically provides the same information as `__LOC_OF__`, but separated into m let f = () => None let (pos, f') = __POS_OF__(f) -pos->Js.log -f'->Js.log +pos->Console.log +f'->Console.log ``` ```js @@ -35,6 +35,7 @@ var pos = [ console.log(pos); console.log(f); +var f$p = f; ``` \ No newline at end of file diff --git a/pages/docs/gentype/latest/supported-types.mdx b/pages/docs/gentype/latest/supported-types.mdx index 7b9624619..d1c5a77a5 100644 --- a/pages/docs/gentype/latest/supported-types.mdx +++ b/pages/docs/gentype/latest/supported-types.mdx @@ -32,7 +32,7 @@ So the option type is exported to JS type `null` or `undefined` or `number`. ## Nullables -ReScript values of type e.g. `Js.Nullable.t`, such as `Js.Nullable.null`, `Js.Nullable.undefined`, `Js.Nullable.return(0)`, `Js.Nullable.return(1)`, `Js.Nullable.return(2)`, are exported to JS values `null`, `undefined`, `0`, `1`, `2`. +ReScript values of type e.g. `Nullable.t`, such as `Nullable.null`, `Nullable.undefined`, `Nullable.make(0)`, `Nullable.make(1)`, `Nullable.make(2)`, are exported to JS values `null`, `undefined`, `0`, `1`, `2`. The JS values are identical: there is no conversion unless the argument type needs conversion. ## Records @@ -191,5 +191,5 @@ const none = (a: T1): ?T1 => OptionBS.none; ## Promises -Values of type `Js.Promise.t` are exported to JS promises of type `Promise` where `argJS` is the JS type corresponding to `arg`. +Values of type `Promise.t` are exported to JS promises of type `Promise` where `argJS` is the JS type corresponding to `arg`. If a conversion for the argument is required, the conversion functions are chained via `.then(promise => ...)`. diff --git a/pages/docs/gentype/latest/usage.mdx b/pages/docs/gentype/latest/usage.mdx index fa61f9502..b8744e31b 100644 --- a/pages/docs/gentype/latest/usage.mdx +++ b/pages/docs/gentype/latest/usage.mdx @@ -32,7 +32,7 @@ To export a function `callback` to JS: ```res @genType -let callback = _ => Js.log("Clicked"); +let callback = _ => Console.log("Clicked"); ``` To rename the function and export it as `CB` on the JS side, use @@ -40,14 +40,14 @@ To rename the function and export it as `CB` on the JS side, use ```res @genType @genType.as("CB") -let callback = _ => Js.log("Clicked"); +let callback = _ => Console.log("Clicked"); ``` or the more compact ```res @genType("CB") -let callback = _ => Js.log("Clicked"); +let callback = _ => Console.log("Clicked"); ``` diff --git a/pages/docs/manual/latest/array-and-list.mdx b/pages/docs/manual/latest/array-and-list.mdx index fdad27373..20a77784b 100644 --- a/pages/docs/manual/latest/array-and-list.mdx +++ b/pages/docs/manual/latest/array-and-list.mdx @@ -87,7 +87,7 @@ You'd use list for its resizability, its fast prepend (adding at the head), and Do **not** use list if you need to randomly access an item or insert at non-head position. Your code would end up obtuse and/or slow. -The standard lib provides a [List module](api/belt/list). +The standard lib provides a [List module](api/core/list). #### Immutable Prepend @@ -133,7 +133,7 @@ var anotherList = { let message = switch myList { | list{} => "This list is empty" - | list{a, ...rest} => "The head of the list is the string " ++ Js.Int.toString(a) + | list{a, ...rest} => "The head of the list is the string " ++ Int.toString(a) } ``` ```js diff --git a/pages/docs/manual/latest/async-await.mdx b/pages/docs/manual/latest/async-await.mdx index 816fdfcb7..e41e685a3 100644 --- a/pages/docs/manual/latest/async-await.mdx +++ b/pages/docs/manual/latest/async-await.mdx @@ -247,7 +247,7 @@ We can utilize the `Promise` module to handle multiple promises. E.g. let's use ```res let pauseReturn = (value, timeout) => { Promise.make((resolve, _reject) => { - Js.Global.setTimeout(() => { + setTimeout(() => { resolve(value) }, timeout)->ignore }) @@ -287,7 +287,7 @@ module Response = { external fetch: ( string, 'params, -) => promise, "error": Js.Nullable.t}>> = +) => promise, "error": Nullable.t}>> = "fetch" // We now use our asynchronous `fetch` function to simulate a login. @@ -303,17 +303,17 @@ let login = async (email: string, password: string) => { "headers": { "Content-Type": "application/json", }, - "body": Js.Json.stringifyAny(body), + "body": Json.stringifyAny(body), } try { let response = await fetch("https://reqres.in/api/login", params) let data = await response->Response.json - switch Js.Nullable.toOption(data["error"]) { + switch Nullable.toOption(data["error"]) { | Some(msg) => Error(msg) | None => - switch Js.Nullable.toOption(data["token"]) { + switch Nullable.toOption(data["token"]) { | Some(token) => Ok(token) | None => Error("Didn't return a token") } diff --git a/pages/docs/manual/latest/bind-to-global-js-values.mdx b/pages/docs/manual/latest/bind-to-global-js-values.mdx index d857dc0b8..c08d45a1e 100644 --- a/pages/docs/manual/latest/bind-to-global-js-values.mdx +++ b/pages/docs/manual/latest/bind-to-global-js-values.mdx @@ -6,7 +6,7 @@ canonical: "/docs/manual/latest/bind-to-global-js-values" # Bind to Global JS Values -**First**, make sure the value you'd like to model doesn't already exist in our [provided API](api/js). +**First**, make sure the value you'd like to model doesn't already exist in our [provided API](api/core). Some JS values, like `setTimeout`, live in the global scope. You can bind to them like so: @@ -22,7 +22,7 @@ Some JS values, like `setTimeout`, live in the global scope. You can bind to the -(We already provide `setTimeout`, `clearTimeout` and others in the [Js.Global](api/js/global) module). +(We already provide `setTimeout`, `clearTimeout` and others in the [Core API](api/core) module). This binds to the JavaScript [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrworkerGlobalScope/setTimeout) methods and the corresponding `clearTimeout`. The `external`'s type annotation specifies that `setTimeout`: diff --git a/pages/docs/manual/latest/bind-to-js-function.mdx b/pages/docs/manual/latest/bind-to-js-function.mdx index 0e26ddeb4..2c8e60b03 100644 --- a/pages/docs/manual/latest/bind-to-js-function.mdx +++ b/pages/docs/manual/latest/bind-to-js-function.mdx @@ -313,7 +313,7 @@ external processOnExit: ( ) => unit = "process.on" processOnExit(exitCode => - Console.log("error code: " ++ Js.Int.toString(exitCode)) + Console.log("error code: " ++ Int.toString(exitCode)) ); ``` ```js diff --git a/pages/docs/manual/latest/bind-to-js-object.mdx b/pages/docs/manual/latest/bind-to-js-object.mdx index ddc6275a5..09a21c226 100644 --- a/pages/docs/manual/latest/bind-to-js-object.mdx +++ b/pages/docs/manual/latest/bind-to-js-object.mdx @@ -160,7 +160,7 @@ If your JavaScript object: - might or might not add/remove keys - contains only values that are of the same type -Then it's not really an object, it's a hash map. Use [Js.Dict](api/js/dict), which contains operations like `get`, `set`, etc. and cleanly compiles to a JavaScript object still. +Then it's not really an object, it's a hash map. Use [Dict](api/core/dict), which contains operations like `get`, `set`, etc. and cleanly compiles to a JavaScript object still. ## Bind to a JS Object That's a Class diff --git a/pages/docs/manual/latest/control-flow.mdx b/pages/docs/manual/latest/control-flow.mdx index 45f4cafd6..0609725a6 100644 --- a/pages/docs/manual/latest/control-flow.mdx +++ b/pages/docs/manual/latest/control-flow.mdx @@ -187,7 +187,7 @@ There's no loop-breaking `break` keyword (nor early `return` from functions, for let break = ref(false) while !break.contents { - if Js.Math.random() > 0.3 { + if Math.random() > 0.3 { break := true } else { Console.log("Still running") diff --git a/pages/docs/manual/latest/function.mdx b/pages/docs/manual/latest/function.mdx index 3ec35d0ad..70005e323 100644 --- a/pages/docs/manual/latest/function.mdx +++ b/pages/docs/manual/latest/function.mdx @@ -490,7 +490,7 @@ Occasionally you may want to ignore the return value of a function. ReScript pro ```res mySideEffect()->Promise.catch(handleError)->ignore -Js.Global.setTimeout(myFunc, 1000)->ignore +setTimeout(myFunc, 1000)->ignore ``` ```js diff --git a/pages/docs/manual/latest/interop-cheatsheet.mdx b/pages/docs/manual/latest/interop-cheatsheet.mdx index 0e619b751..54b2b14b3 100644 --- a/pages/docs/manual/latest/interop-cheatsheet.mdx +++ b/pages/docs/manual/latest/interop-cheatsheet.mdx @@ -117,25 +117,29 @@ var b; -Handling a value that can be `undefined` and `null`, by ditching the `option` type and using `Js.Nullable.t`: +Handling a value that can be `undefined` and `null`, by ditching the `option` type and using `Nullable.t`: ```res example -let jsNull = Js.Nullable.null -let jsUndefined = Js.Nullable.undefined -let result1: Js.Nullable.t = Js.Nullable.return("hello") -let result2: Js.Nullable.t = Js.Nullable.fromOption(Some(10)) -let result3: option = Js.Nullable.toOption(Js.Nullable.return(10)) +let jsNull = Nullable.null +let jsUndefined = Nullable.undefined +let result1: Nullable.t = Nullable.make("hello") +let result2: Nullable.t = Nullable.fromOption(Some(10)) +let result3: option = Nullable.toOption(Nullable.make(10)) ``` ```js -var Caml_option = require("./stdlib/caml_option.js"); -var Js_null_undefined = require("./stdlib/js_null_undefined.js"); +import * as Caml_option from "./stdlib/caml_option.js"; +import * as Core__Nullable from "./stdlib/core__Nullable.js"; + +var result2 = Core__Nullable.fromOption(10); var jsNull = null; + var jsUndefined; + var result1 = "hello"; -var result2 = Js_null_undefined.fromOption(10); + var result3 = Caml_option.nullable_to_opt(10); ``` diff --git a/pages/docs/manual/latest/json.mdx b/pages/docs/manual/latest/json.mdx index d59332954..6f56d93de 100644 --- a/pages/docs/manual/latest/json.mdx +++ b/pages/docs/manual/latest/json.mdx @@ -34,12 +34,12 @@ Where `data` can be any type you assume the JSON is. As you can see, this compil ## Stringify -Use `Js.Json.stringify`: +Use `Json.stringify`: ```res example -Console.log(Js.Json.stringifyAny(["Amy", "Joe"])) +Console.log(JSON.stringifyAny(["Amy", "Joe"])) ``` ```js console.log(JSON.stringify([ @@ -52,4 +52,4 @@ console.log(JSON.stringify([ ## Advanced -The [Js.Json](api/js/json) module provides slightly safer, low-level building blocks for power users who want to parse JSON on a per-field basis. See the examples in the API docs. +The [Json](api/core/json) module provides slightly safer, low-level building blocks for power users who want to parse JSON on a per-field basis. See the examples in the API docs. diff --git a/pages/docs/manual/latest/libraries.mdx b/pages/docs/manual/latest/libraries.mdx index 57876b799..302f80fb1 100644 --- a/pages/docs/manual/latest/libraries.mdx +++ b/pages/docs/manual/latest/libraries.mdx @@ -23,5 +23,5 @@ Search `rescript`-related packages on NPM, or use our [Package Index](/packages) If you can't find what you're looking for, remember that **you don't need a wrapper** to use a JS library: - Most JS data types, such as array and objects, [map over cleanly to ReScript and vice-versa](shared-data-types.md). -- You also have access to the familiar [JS API](api/js). +- You also have access to the familiar [Core API](api/core). - You can use a JavaScript library without needing to install dedicated binding libraries. Check the [`external`](external) page. diff --git a/pages/docs/manual/latest/module.mdx b/pages/docs/manual/latest/module.mdx index 3590a346e..3dbf4dfe1 100644 --- a/pages/docs/manual/latest/module.mdx +++ b/pages/docs/manual/latest/module.mdx @@ -149,9 +149,9 @@ let map = (arr, value) => { value } -// opening Js.Array2 would shadow our previously defined `map` +// opening Array would shadow our previously defined `map` // `open!` will explicitly turn off the automatic warning -open! Js.Array2 +open! Array let arr = map([1,2,3], (a) => { a + 1}) ``` diff --git a/pages/docs/manual/latest/newcomer-examples.mdx b/pages/docs/manual/latest/newcomer-examples.mdx index 49c4baa77..87d4168e4 100644 --- a/pages/docs/manual/latest/newcomer-examples.mdx +++ b/pages/docs/manual/latest/newcomer-examples.mdx @@ -106,13 +106,13 @@ See [here](import-from-export-to-js.md#import-a-javascript-module-itself-es6-mod ## Checking for JS nullable types using the `option` type -For a function whose argument is passed a JavaScript value that's potentially `null` or `undefined`, it's idiomatic to convert it to an `option`. The conversion is done through the helper functions in ReScript's [`Js.Nullable`](api/js/nullable#t) module. In this case, `toOption`: +For a function whose argument is passed a JavaScript value that's potentially `null` or `undefined`, it's idiomatic to convert it to an `option`. The conversion is done through the helper functions in ReScript's [`Nullable`](api/core/nullable#value-toOption) module. In this case, `toOption`: ```res example let greetByName = (possiblyNullName) => { - let optionName = Js.Nullable.toOption(possiblyNullName) + let optionName = Nullable.toOption(possiblyNullName) switch optionName { | None => "Hi" | Some(name) => "Hello " ++ name diff --git a/pages/docs/manual/latest/null-undefined-option.mdx b/pages/docs/manual/latest/null-undefined-option.mdx index c2d83ddc4..f49153ce2 100644 --- a/pages/docs/manual/latest/null-undefined-option.mdx +++ b/pages/docs/manual/latest/null-undefined-option.mdx @@ -69,7 +69,7 @@ switch licenseNumber { | None => Console.log("The person doesn't have a car") | Some(number) => - Console.log("The person's license number is " ++ Js.Int.toString(number)) + Console.log("The person's license number is " ++ Int.toString(number)) } ``` ```js @@ -159,18 +159,18 @@ Unfortunately, lots of times, your JavaScript value might be _both_ `null` or `u #### Solution: More Sophisticated `undefined` & `null` Interop -To solve this, we provide access to more elaborate `null` and `undefined` helpers through the [`Js.Nullable`](api/js/nullable) module. This somewhat works like an `option` type, but is different from it. +To solve this, we provide access to more elaborate `null` and `undefined` helpers through the [`Nullable`](api/core/nullable) module. This somewhat works like an `option` type, but is different from it. #### Examples -To create a JS `null`, use the value `Js.Nullable.null`. To create a JS `undefined`, use `Js.Nullable.undefined` (you can naturally use `None` too, but that's not the point here; the `Js.Nullable.*` helpers wouldn't work with it). +To create a JS `null`, use the value `Nullable.null`. To create a JS `undefined`, use `Nullable.undefined` (you can naturally use `None` too, but that's not the point here; the `Nullable.*` helpers wouldn't work with it). If you're receiving, for example, a JS string that can be `null` and `undefined`, type it as: ```res example -@module("MyConstant") external myId: Js.Nullable.t = "myId" +@module("MyConstant") external myId: Nullable.t = "myId" ``` ```js // Empty output @@ -183,8 +183,8 @@ To create such a nullable string from our side (presumably to pass it to the JS ```res example -@module("MyIdValidator") external validate: Js.Nullable.t => bool = "validate" -let personId: Js.Nullable.t = Js.Nullable.return("abc123") +@module("MyIdValidator") external validate: Nullable.t => bool = "validate" +let personId: Nullable.t = Nullable.make("abc123") let result = validate(personId) ``` @@ -200,4 +200,4 @@ The `return` part "wraps" a string into a nullable string, to make the type syst #### Convert to/from `option` -`Js.Nullable.fromOption` converts from a `option` to `Js.Nullable.t`. `Js.Nullable.toOption` does the opposite. +`Nullable.fromOption` converts from a `option` to `Nullable.t`. `Nullable.toOption` does the opposite. diff --git a/pages/docs/manual/latest/pattern-matching-destructuring.mdx b/pages/docs/manual/latest/pattern-matching-destructuring.mdx index 9303aaa14..2aa70d210 100644 --- a/pages/docs/manual/latest/pattern-matching-destructuring.mdx +++ b/pages/docs/manual/latest/pattern-matching-destructuring.mdx @@ -176,20 +176,20 @@ switch data { | GoodResult(theMessage) => Console.log("Success! " ++ theMessage) | BadResult(errorCode) => - Console.log("Something's wrong. The error code is: " ++ Js.Int.toString(errorCode)) + Console.log("Something's wrong. The error code is: " ++ Int.toString(errorCode)) | NoResult => Console.log("Bah.") } ``` ```js var data = { - TAG: /* GoodResult */1, + TAG: "GoodResult", _0: "Product shipped!" }; -if (typeof data === "number") { +if (typeof data !== "object") { console.log("Bah."); -} else if (data.TAG === /* BadResult */ 0) { +} else if (data.TAG === "BadResult") { console.log("Something's wrong. The error code is: " + "Product shipped!".toString()); } else { console.log("Success! Product shipped!"); @@ -244,12 +244,12 @@ let message = switch person1 { // this is matched only if `name` isn't "Mary" or "Joe" `Hello ${name}.` | Student({name, reportCard: {passing: true, gpa}}) => - `Congrats ${name}, nice GPA of ${Js.Float.toString(gpa)} you got there!` + `Congrats ${name}, nice GPA of ${Float.toString(gpa)} you got there!` | Student({ reportCard: {gpa: 0.0}, status: Vacations(daysLeft) | Sabbatical(daysLeft) }) => - `Come back in ${Js.Int.toString(daysLeft)} days!` + `Come back in ${Int.toString(daysLeft)} days!` | Student({status: Sick}) => `How are you feeling?` | Student({name}) => @@ -258,39 +258,33 @@ let message = switch person1 { ``` ```js var person1 = { - TAG: /* Teacher */0, + TAG: "Teacher", name: "Jane", age: 35 }; var message; -if (person1.TAG) { - var match$1 = person1.status; - var name = person1.name; - var match$2 = person1.reportCard; - message = match$2.passing - ? "Congrats " + - name + - ", nice GPA of " + - match$2.gpa.toString() + - " you got there!" - : typeof match$1 === "number" - ? match$1 !== 0 - ? "Good luck next semester " + name + "!" - : "How are you feeling?" - : person1.reportCard.gpa !== 0.0 - ? "Good luck next semester " + name + "!" - : "Come back in " + match$1._0.toString() + " days!"; +if (person1.TAG === "Teacher") { + message = "Hello Jane."; } else { - var name$1 = person1.name; - switch (name$1) { - case "Joe": - case "Mary": - message = "Hey, still going to the party on Saturday?"; - break; - default: - message = "Hello " + name$1 + "."; + var match = "Jane"; + var match$1 = match.status; + var name = match.name; + var match$2 = match.reportCard; + if (match$2.passing) { + message = "Congrats " + name + ", nice GPA of " + match$2.gpa.toString() + " you got there!"; + } else { + var exit = 0; + if (typeof match$1 !== "object") { + message = match$1 === "Sick" ? "How are you feeling?" : "Good luck next semester " + name + "!"; + } else { + exit = 1; + } + if (exit === 1) { + message = match.reportCard.gpa !== 0.0 ? "Good luck next semester " + name + "!" : "Come back in " + match$1._0.toString() + " days!"; + } + } } ``` @@ -341,7 +335,7 @@ let myStatus = Vacations(10) switch myStatus { | Vacations(days) -| Sabbatical(days) => Console.log(`Come back in ${Js.Int.toString(days)} days!`) +| Sabbatical(days) => Console.log(`Come back in ${Int.toString(days)} days!`) | Sick | Present => Console.log("Hey! How are you?") } @@ -376,10 +370,10 @@ switch person1 { } ``` ```js -if (person1.TAG) { - console.log("Hey student"); -} else { +if (person1.TAG === "Teacher") { console.log("Hi teacher"); +} else { + console.log("Hey student"); } ``` @@ -396,7 +390,7 @@ switch myStatus { } ``` ```js -if (typeof myStatus === "number" || myStatus.TAG) { +if (typeof myStatus !== "object" || myStatus.TAG !== "Vacations") { console.log("Ok."); } else { console.log("Have fun!"); @@ -416,7 +410,7 @@ switch myStatus { } ``` ```js -if (typeof myStatus === "number" || myStatus.TAG) { +if (typeof myStatus !== "object" || myStatus.TAG !== "Vacations") { console.log("Ok."); } else { console.log("Have fun!"); @@ -445,8 +439,8 @@ switch person1 { } ``` ```js -if (person1.TAG) { - if (person1.reportCard.gpa < 0.5) { +if (person1.TAG !== "Teacher") { + if ("Jane".reportCard.gpa < 0.5) { console.log("What's happening"); } else { console.log("Heyo"); @@ -646,12 +640,12 @@ let message = switch person1 { | Teacher({name: "Mary" | "Joe"}) => `Hey, still going to the party on Saturday?` | Student({name, reportCard: {passing: true, gpa}}) => - `Congrats ${name}, nice GPA of ${Js.Float.toString(gpa)} you got there!` + `Congrats ${name}, nice GPA of ${Float.toString(gpa)} you got there!` | Student({ reportCard: {gpa: 0.0}, status: Vacations(daysLeft) | Sabbatical(daysLeft) }) => - `Come back in ${Js.Int.toString(daysLeft)} days!` + `Come back in ${Int.toString(daysLeft)} days!` | Student({status: Sick}) => `How are you feeling?` | Student({name}) => diff --git a/pages/docs/manual/latest/primitive-types.mdx b/pages/docs/manual/latest/primitive-types.mdx index 55831b806..7d3eccdc8 100644 --- a/pages/docs/manual/latest/primitive-types.mdx +++ b/pages/docs/manual/latest/primitive-types.mdx @@ -72,7 +72,7 @@ This is just like JavaScript's backtick string interpolation, except without nee ### Usage -See the familiar `String` API in the [API docs](api/js/string). Since a ReScript string maps to a JavaScript string, you can mix & match the string operations in all standard libraries. +See the familiar `String` API in the [API docs](api/core/string). Since a ReScript string maps to a JavaScript string, you can mix & match the string operations in all standard libraries. ### Tips & Tricks @@ -120,7 +120,7 @@ var r = /b/g; -A regular expression like the above has the type `Js.Re.t`. The [Js.Re](api/js/re) module contains the regular expression helpers you have seen in JS. +A regular expression like the above has the type `Re.t`. The [Re](api/core/re) module contains the regular expression helpers you have seen in JS. ## Boolean @@ -139,7 +139,7 @@ ReScript's `true/false` compiles into a JavaScript `true/false`. ## Integers -32-bits, truncated when necessary. We provide the usual operations on them: `+`, `-`, `*`, `/`, etc. See [Js.Int](api/js/int) for helper functions. +32-bits, truncated when necessary. We provide the usual operations on them: `+`, `-`, `*`, `/`, etc. See [Int](api/core/int) for helper functions. **Be careful when you bind to JavaScript numbers!** Since ReScript integers have a much smaller range than JavaScript numbers, data might get lost when dealing with large numbers. In those cases itโ€™s much safer to bind the numbers as **float**. Be extra mindful of this when binding to JavaScript Dates and their epoch time. @@ -147,7 +147,7 @@ To improve readability, you may place underscores in the middle of numeric liter ## Floats -Float requires other operators: `+.`, `-.`, `*.`, `/.`, etc. Like `0.5 +. 0.6`. See [Js.Float](api/js/float) for helper functions. +Float requires other operators: `+.`, `-.`, `*.`, `/.`, etc. Like `0.5 +. 0.6`. See [Float](api/core/float) for helper functions. As with integers, you may use underscores within literals to improve readability. diff --git a/pages/docs/manual/latest/promise.mdx b/pages/docs/manual/latest/promise.mdx index 5531c35d7..6eb83cecf 100644 --- a/pages/docs/manual/latest/promise.mdx +++ b/pages/docs/manual/latest/promise.mdx @@ -24,36 +24,34 @@ type user = {name: string} let fetchUser: string => promise ``` -To work with promise values (instead of using `async` / `await`) you may want to use the built in `Js.Promise2` module. +To work with promise values (instead of using `async` / `await`) you may want to use the built in `Promise` module. -## Js.Promise2 +## Promise A builtin module to create, chain and manipulate promises. -> **Note:** This is an intermediate replacement for the `Js.Promise` module. It is designed to work with the `->` operator and should be used in favour of it's legacy counterpart. We are aware that the `Belt`, `Js` and `Js.xxx2` situation is confusing; a proper solution will hopefully be part of our upcoming `v11` release. - ### Creating a promise ```res -let p1 = Js.Promise2.make((~resolve, ~reject) => { +let p1 = Promise.make((resolve, reject) => { // We use uncurried functions for resolve / reject // for cleaner JS output without unintended curry calls - resolve(. "hello world") + resolve("hello world") }) -let p2 = Js.Promise2.resolve("some value") +let p2 = Promise.resolve("some value") // You can only reject `exn` values for streamlined catch handling exception MyOwnError(string) -let p3 = Js.Promise2.reject(MyOwnError("some rejection")) +let p3 = Promise.reject(MyOwnError("some rejection")) ``` ### Access the contents and transform a promise ```res let logAsyncMessage = () => { - open Js.Promise2 - Js.Promise2.resolve("hello world") + open Promise + Promise.resolve("hello world") ->then(msg => { // then callbacks require the result to be resolved explicitly resolve("Message: " ++ msg) @@ -72,7 +70,7 @@ For comparison, the `async` / `await` version of the same code would look like t ```res let logAsyncMessage = async () => { - let msg = await Js.Promise2.resolve("hello world") + let msg = await Promise.resolve("hello world") Console.log(`Message: ${msg}`) } ``` @@ -81,7 +79,7 @@ Needless to say, the async / await version offers better ergonomics and less opp ### Run multiple promises in parallel -In case you want to launch multiple promises in parallel, use `Js.Promise2.all`: +In case you want to launch multiple promises in parallel, use `Promise.all`: @@ -91,7 +89,7 @@ In case you want to launch multiple promises in parallel, use `Js.Promise2.all`: external fetchMessage: string => promise = "global.fetchMessage" let logAsyncMessage = async () => { - let messages = await Js.Promise2.all([fetchMessage("message1"), fetchMessage("message2")]) + let messages = await Promise.all([fetchMessage("message1"), fetchMessage("message2")]) Console.log(messages->Array.joinWith(", ")) } @@ -115,7 +113,7 @@ export { ## Js.Promise module (legacy - do not use) -> **Note:** The `Js.Promise` bindings are following the outdated data-last convention from a few years ago. We kept those APIs for backwards compatibility. Either use `Js.Promise2` or a third-party promise binding instead. +> **Note:** The `Js.Promise` bindings are following the outdated data-last convention from a few years ago. We kept those APIs for backwards compatibility. Either use [`Promise`](api/core/promise) or a third-party promise binding instead. ReScript has built-in support for [JavaScript promises](api/js/promise). The 3 functions you generally need are: diff --git a/pages/docs/manual/latest/shared-data-types.mdx b/pages/docs/manual/latest/shared-data-types.mdx index 57f4cb687..8f43f3964 100644 --- a/pages/docs/manual/latest/shared-data-types.mdx +++ b/pages/docs/manual/latest/shared-data-types.mdx @@ -15,10 +15,10 @@ Unlike most compiled-to-js languages, in ReScript, **you don't need to write dat **Shared, bidirectionally usable types**: - String. ReScript strings are JavaScript strings, vice-versa. (Caveat: only our backtick string `` `hello ๐Ÿ‘‹ ${personName}` `` supports unicode and interpolation). - Float. ReScript floats are JS numbers, vice-versa. -- Array. In addition to the [JS Array API](api/js/array), we provide our own [Belt.Array](api/belt/array#set) API too. +- Array. In addition to the [Array API](api/core/array), we provide our own [Belt.Array](api/belt/array#set) API too. - Tuple. Compiles to a JS array. You can treat a fixed-sized, heterogenous JS array as ReScript tuple too. - Boolean. -- Record. Record compiles to JS object. Therefore you can also treat JS objects as records. If they're too dynamic, consider modeling them on the ReScript side as a hashmap/dictionary [`Js.Dict`](api/js/dict) or a ReScript object. +- Record. Record compiles to JS object. Therefore you can also treat JS objects as records. If they're too dynamic, consider modeling them on the ReScript side as a hashmap/dictionary [`Dict`](api/core/dict) or a ReScript object. - Object. ReScript objects are JavaScript objects, vice-versa. - Function. They compile to clean JS functions. - Module. ReScript files are considered top-level modules, and are compiled to JS files 1 to 1. Nested modules are compiled to JavaScript objects. @@ -26,8 +26,8 @@ Unlike most compiled-to-js languages, in ReScript, **you don't need to write dat - Unit. The `unit` type, which has a single value `()`, compiles to `undefined` too. Likewise, you can treat an incoming JS `undefined` as `()` if that's the only value it'll ever be. **Types that are slightly different than JS, but that you can still use from JS**: -- Int. **Ints are 32-bits**! Be careful, you can potentially treat them as JS numbers and vice-versa, but if the number's large, then you better treat JS numbers as floats. For example, we bind to Js.Date using `float`s. -- Option. The `option` type's `None` value compiles into JS `undefined`. The `Some` value, e.g. `Some(5)`, compiles to `5`. Likewise, you can treat an incoming JS `undefined` as `None`. **JS `null` isn't handled here**. If your JS value can be `null`, use [Js.Nullable](api/js/nullable) helpers. +- Int. **Ints are 32-bits**! Be careful, you can potentially treat them as JS numbers and vice-versa, but if the number's large, then you better treat JS numbers as floats. For example, we bind to `Date` using `float`s. +- Option. The `option` type's `None` value compiles into JS `undefined`. The `Some` value, e.g. `Some(5)`, compiles to `5`. Likewise, you can treat an incoming JS `undefined` as `None`. **JS `null` isn't handled here**. If your JS value can be `null`, use [Nullable](api/core/nullable) helpers. - Exception. - Variant. Check the compiled JavaScript output of variant to see its shape. We don't recommend exporting a ReScript variant for pure JS usage, since they're harder to read as plain JS code, but you can do it. - List, which is just a regular variant. diff --git a/pages/docs/manual/latest/variant.mdx b/pages/docs/manual/latest/variant.mdx index 47c7a7312..dfec96bd2 100644 --- a/pages/docs/manual/latest/variant.mdx +++ b/pages/docs/manual/latest/variant.mdx @@ -418,7 +418,7 @@ type rec json = | Boolean(bool) | String(string) | Number(float) - | Object(Js.Dict.t) + | Object(Dict.t) | Array(array) let myValidJsonValue = Array([String("Hi"), Number(123.)]) @@ -433,7 +433,7 @@ type rec json = | Boolean(bool) | String(string) | Number(float) - | Object(Js.Dict.t) + | Object(Dict.t) | Array(array) type rec user = {