Skip to content

voila/bucklescript-cookbook

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The BuckleScript Cookbook

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.

Contributing

There are primarily two ways to contribute:

  1. Suggest an example to include in the cookbook by creating an issue to describe the task.
  2. Add (or edit) an example by editing this file directly and creating a pull request.

General

Serialize a record to JSON

Deserialize JSON to a record

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

Encode and decode Base64

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

Generate random numbers

Use Random module to generate random numbers

Js.log (Random.int 5)

Log a message to the console

Js.log "Hello BuckleScript!"

Use string interpolation

let () =
  let world = "World" in
  Js.log {j|Hello$world!|j}

Format a string using Printf

Use Printf module

# Printf.printf ("Foo %d  %s") 2 "bar"

Make and use a Map

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;

FFI

Bind to a simple function

external random : unit -> float = "Math.random" [@@bs.val]

Bind to a function in another module

external leftpad : string -> int -> char -> string = "" [@@bs.val] [@@bs.module "left-pad"]

Define composable bitflags constants

Browser-specific

Extract all links from a webpage

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

Fetch a json resource from some server (Query the GitHub API?)

Uses bs-json and bs-fetch

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 ()

Node-specific

Read lines from a text file

Uses bs-node

let () =
  Node.Fs.readFileAsUtf8Sync "README.md"
  |> Js.String.split "\n"
  |> Array.iter Js.log

Read and parse a JSON file

Uses bs-json and bs-node

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

Find files using a given predicate

Uses bs-glob

let () =
  (* find and list all javascript files in subfolders *)
  Glob.glob "**/*.js" (fun _ files -> Array.iter Js.log files)

Run an external command

Uses bs-node

let () =
  (* prints node's version *)
  Node.(ChildProcess.execSync "node -v" (Options.options ~encoding:"utf8" ()))
  |> Js.log

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published