aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-06-08 19:36:47 +0000
committerGitHub <noreply@github.com>2022-06-08 20:36:47 +0100
commit28ebbd29a171ceebe74c075f6a05ef86b727e002 (patch)
treef0b7862513577ac58eb2c64aef954c094f08bf2c
parentdd5a95548f43097e1aea624fc1e1576f4140f897 (diff)
downloadgleam_stdlib-28ebbd29a171ceebe74c075f6a05ef86b727e002.tar.gz
gleam_stdlib-28ebbd29a171ceebe74c075f6a05ef86b727e002.zip
fix io.debug to behave the same on Erlang and JavaScript (#308)
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/gleam/io.gleam25
-rw-r--r--src/gleam/string.gleam11
-rw-r--r--test/gleam/io_test.gleam11
4 files changed, 25 insertions, 23 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6d2ca82..117efb1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@
- The `string_builder` module loses the `from_float` function. Use `float.to_string` instead.
- Fixed the `int.power` and `float.power` functions by properly handling error cases.
- The grapheme iterator used by `string.graphemes` is now locale independent on target JavaScript.
+- Unified `io.debug` to yield Gleam syntax to standard output (stdout) not just on JavaScript but also Erlang.
## v0.21.0 - 2022-04-24
diff --git a/src/gleam/io.gleam b/src/gleam/io.gleam
index 5a485c9..3e4fadb 100644
--- a/src/gleam/io.gleam
+++ b/src/gleam/io.gleam
@@ -1,3 +1,5 @@
+import gleam/string
+
/// Writes a string to standard output.
///
/// If you want your output to be printed on its own line see `println`.
@@ -48,7 +50,7 @@ if javascript {
"../gleam_stdlib.mjs" "log"
}
-/// Prints a value to standard output using Erlang syntax.
+/// Prints a value to standard output (stdout) yielding Gleam syntax.
///
/// The value is returned after being printed so it can be used in pipelines.
///
@@ -73,24 +75,9 @@ if javascript {
/// ```
///
pub fn debug(term: anything) -> anything {
- debug_print(term)
term
-}
-
-if erlang {
- fn debug_print(term: anything) -> DoNotLeak {
- erlang_fwrite("~tp\n", [term])
- }
-}
+ |> string.inspect
+ |> println
-if javascript {
- external fn debug_print(anything) -> Nil =
- "../gleam_stdlib.mjs" "debug"
-}
-
-if erlang {
- external type DoNotLeak
-
- external fn erlang_fwrite(String, List(a)) -> DoNotLeak =
- "io" "fwrite"
+ term
}
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index d06e2dd..a25bdd7 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -6,6 +6,7 @@ import gleam/list
import gleam/option.{None, Option, Some}
import gleam/order
import gleam/string_builder
+import gleam/string_builder.{StringBuilder}
if erlang {
import gleam/result
@@ -824,17 +825,19 @@ pub fn capitalise(s: String) -> String {
}
}
-pub fn inspect(value: a) -> String {
- do_inspect(value)
+/// Returns a `String` representation of values in Gleam syntax.
+///
+pub fn inspect(term: anything) -> String {
+ do_inspect(term)
|> string_builder.to_string
}
if javascript {
- external fn do_inspect(value: a) -> string_builder.StringBuilder =
+ external fn do_inspect(term: anything) -> StringBuilder =
"../gleam.mjs" "inspect"
}
if erlang {
- external fn do_inspect(value: a) -> string_builder.StringBuilder =
+ external fn do_inspect(term: anything) -> StringBuilder =
"gleam_stdlib" "inspect"
}
diff --git a/test/gleam/io_test.gleam b/test/gleam/io_test.gleam
new file mode 100644
index 0000000..6408127
--- /dev/null
+++ b/test/gleam/io_test.gleam
@@ -0,0 +1,11 @@
+if erlang {
+ import gleam/io
+ import gleam/should
+
+ pub fn debug_test() {
+ "io.debug-test"
+ // prints to stdout, but EUnit will suppress that:
+ |> io.debug()
+ |> should.equal("io.debug-test")
+ }
+}