The BuckleScript Cookbook is a collection of simple examples intended to both showcase BuckleScript by example, and to demonstrate good practices for accomplishing common tasks.
This has been heavily inspired by the Rust Cookbook.
There are primarily two ways to contribute:
- Suggest an example to include in the cookbook by creating an issue to describe the task.
- Add (or edit) an example by editing this file directly and creating a pull request.
Uses bs-json
type line = {
start: point;
end_: point;
thickness: int option
}
and point = {
x: float;
y: float
}
module Decode = struct
let point json =
let open! Json.Decode in {
x = json |> field "x" float;
y = json |> field "y" float
}
let line json =
Json.Decode.{
start = json |> field "start" point;
end_ = json |> field "end" point;
thickness = json |> optional (field "thickness" int)
}
end
let data = {| {
"start": { "x": 1.1, "y": -0.4 },
"end": { "x": 5.3, "y": 3.8 }
} |}
let line = data |> Js.Json.parseExn
|> Decode.line
To encode and decode Base64, you can bind to Javascript functions btoa
and atob
, respectively:
external btoa : string -> string = "window.btoa" [@@bs.val]
external atob : string -> string = "window.atob" [@@bs.val]
let _ = "Hello World!" |> btoa |> atob |> Js.log
Alternatively, if you have bs-webapi installed:
open ReasonJs.Base64
let _ = "Hello World!" |> btoa |> atob |> Js.log
Use Random module to generate random numbers
Js.log (Random.int 5)
Js.log "Hello BuckleScript!"
let () =
let world = "World" in
Js.log {j|Hello,$world!|j}
Use Printf module
# Printf.printf ("Foo %d %s") 2 "bar"
To create a Map, use the Map.Make functor. It expects a module with the folowing signature:
module type OrderedType = sig type t val compare : t -> t -> int end
For instance, to create the map which associate 1 to "ocaml" and 2 to "bs":
let () =
(* create a module IntMap *)
let module IntMap =
Map.Make(struct type t = int let compare = compare end) in
let open IntMap in
(* create a map with keys 1 and 2 *)
let map12 = empty |> add 1 "ocaml" |> add 2 "bs" in
(* print each key, value pair *)
let printKV k v =
let k = string_of_int k in
Js.log {j|key:$k, val:$v|j} in
iter printKV map12;
external random : unit -> float = "Math.random" [@@bs.val]
external leftpad : string -> int -> char -> string = "" [@@bs.val] [@@bs.module "left-pad"]
open ReasonJs.Dom
let printAllLinks () =
document
|> Document.querySelectorAll "a"
|> NodeList.toArray
|> Array.iter (fun n ->
n
|> Element.ofNode
|> (function
| None -> failwith "Not an Element"
| Some el -> Element.innerHTML el)
|> Js.log)
Window.setOnLoad window printAllLinks
open Bs_fetch
(* given an array of repositories object as a JSON string *)
(* returns an array of names *)
let names text =
match Js.Json.parseExn text with
| arr ->
Json.Decode.(array (field "name" string) arr)
| exception _ -> failwith ("Error parsing: " ^ text)
(* fetch all public repositories of user [BuckleTypes] *)
(* print their names to the console *)
let printGithubRepos () = Js.Promise.(
fetch "https://api.github.com/users/BuckleTypes/repos"
|> then_ Response.text
|> then_ (fun text ->
text
|> names
|> Array.iter Js.log
|> resolve)
|> ignore
)
let () = printGithubRepos ()
Uses bs-node
let () =
Node.Fs.readFileAsUtf8Sync "README.md"
|> Js.String.split "\n"
|> Array.iter Js.log
let decodeName text =
Js.Json.parseExn text
|> Json.Decode.(field "name" string)
let () =
(* read [package.json] file *)
Node.Fs.readFileAsUtf8Sync "package.json"
|> decodeName
|> Js.log
Uses bs-glob
let () =
(* find and list all javascript files in subfolders *)
Glob.glob "**/*.js" (fun _ files -> Array.iter Js.log files)
Uses bs-node
let () =
(* prints node's version *)
Node.(ChildProcess.execSync "node -v" (Options.options ~encoding:"utf8" ()))
|> Js.log