/Strify

A Roc library for converting things to Str

Primary LanguageShellUniversal Permissive License v1.0UPL-1.0

Strify

A Roc library for converting things to Str

Usage

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]]
:)

Why the tag?

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.

Supported input types

Str

strify (Str "") == "\"\""
strify (Str "Text") == "\"Text\""

Bool

strify (Bool False) == "False"
strify (Bool True) == "True"

Num

strify (Num 0) == "0"
strify (Num 6) == "6"

List Str

strify (ListStr []) == "[]"
strify (ListStr ["a", "b", "c"]) == "[\"a\", \"b\", \"c\"]"

List Bool

strify (ListBool []) == "[[]]"
strify (ListBool [True, False, True]) == "[True, False, True]"

List Num

strify (ListNum []) == "[]"
strify (ListNum [1, 2, 3]) == "[1, 2, 3]"

List List Str

strify (ListListStr [[]]) == "[[]]"
strify (ListListStr [["a"], ["b"], ["c"]]) == "[[\"a\"], [\"b\"], [\"c\"]]"

List List Bool

strify (ListListBool [[]]) == "[[]]"
strify (ListListBool [[True], [False], [True]]) == "[[True], [False], [True]]"

List List Num

strify (ListListNum [[]]) == "[[]]"
strify (ListListNum [[1], [2], [3]]) == "[[1], [2], [3]]"

Formatting

Formatting mimics the behavior of JavaScript's JSON.stringify, except that true and false are capitalized.