Last active
October 15, 2019 19:30
-
-
Save idkjs/c48dda9f1dba9ae17080403e3bda973f to your computer and use it in GitHub Desktop.
example for discussion https://dev.to/yawaramin/how-to-handle-a-nodeback-in-reasonml-in7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Encoding = { | |
type norm = | |
| HEX | |
| UTF8 | |
| ASCII | |
| LATIN1 | |
| BASE64; | |
let getNorm = (e: norm) => | |
switch (e) { | |
| UTF8 => "utf8" | |
| HEX => "hex" | |
| ASCII => "ascii" | |
| LATIN1 => "latin1" | |
| BASE64 => "base64" | |
}; | |
}; | |
let dirname: option(string) = [%bs.node __dirname]; | |
[@bs.module "path"] external resolve: (string, string) => 'path = "resolve"; | |
[@bs.module "fs"] | |
external fsReadFile: ('test, string, 'callback) => 'nodeCallback = "readFile"; | |
let readFile = (path: string, norm: Encoding.norm, callback) => | |
fsReadFile(path, Encoding.getNorm(norm), callback); | |
// let dirname = Some("./src/test.txt"); | |
switch (dirname) { | |
| None => print_endline("no __dirname") | |
| Some(path) => | |
readFile(resolve(path, "test.txt"), UTF8, (err, result) => | |
err ? print_endline("Error") : print_endline(result) | |
) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is text from text.txt. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment