A Roc library for converting things to Str
See ./example.roc
:
#!/usr/bin/env roc
app "example"
packages { pf: "./roc/examples/cli/platform" }
imports [ pf.Stdout.{ line }, pf.Task.{ await }, Strify.{ strify } ]
provides [ main ] to pf
main =
_ <- await (line (strify (Str "Six")))
_ <- await (line (strify (Bool True)))
_ <- await (line (strify (Num 6)))
_ <- await (line (strify (ListStr ["Strify", "likes", "Strs"])))
_ <- await (line (strify (ListBool [True, False, True])))
_ <- await (line (strify (ListNum [1, 2, 3])))
_ <- await (line (strify (ListListStr [["Strify"], ["likes"], ["Strs"]])))
_ <- await (line (strify (ListListBool [[True], [False], [True]])))
_ <- await (line (strify (ListListNum [[1], [2], [3]])))
line ":)"
Output:
[nix-shell:~/code/Strify]$ ./roc/target/release/roc example.roc
🔨 Rebuilding host... Done!
"Six"
True
6
["Strify", "likes", "Strs"]
[True, False, True]
[1, 2, 3]
[["Strify"], ["likes"], ["Strs"]]
[[True], [False], [True]]
[[1], [2], [3]]
:)
The Str
/ ListBool
/ ListListNum
tag before your data is (unfortunately) necessary.
Roc doesn't support type introspection,
and abilities aren't implemented yet
(when they are, Str
-ification might be supported natively).
Therefore, Strify can only support specific input types and needs you to tell it your data's type.
strify (Str "") == "\"\""
strify (Str "Text") == "\"Text\""
strify (Bool False) == "False"
strify (Bool True) == "True"
strify (Num 0) == "0"
strify (Num 6) == "6"
strify (ListStr []) == "[]"
strify (ListStr ["a", "b", "c"]) == "[\"a\", \"b\", \"c\"]"
strify (ListBool []) == "[[]]"
strify (ListBool [True, False, True]) == "[True, False, True]"
strify (ListNum []) == "[]"
strify (ListNum [1, 2, 3]) == "[1, 2, 3]"
strify (ListListStr [[]]) == "[[]]"
strify (ListListStr [["a"], ["b"], ["c"]]) == "[[\"a\"], [\"b\"], [\"c\"]]"
strify (ListListBool [[]]) == "[[]]"
strify (ListListBool [[True], [False], [True]]) == "[[True], [False], [True]]"
strify (ListListNum [[]]) == "[[]]"
strify (ListListNum [[1], [2], [3]]) == "[[1], [2], [3]]"
Formatting mimics the behavior of
JavaScript's JSON.stringify,
except that true
and false
are capitalized.