nu_scripts/stdlib-candidate/str.nu
Sam Vente dae6115d4d
add tests to str.nu (#627)
As requested by @amtoine 😋

---------

Co-authored-by: Antoine Stevan <44101798+amtoine@users.noreply.github.com>
2023-10-02 19:12:08 +02:00

34 lines
970 B
Plaintext

def "str append" [tail: string]: [string -> string, list<string> -> list<string>] {
let input = $in
match ($input | describe | str replace --regex '<.*' '') {
"string" => { $input ++ $tail },
"list" => { $input | each {|el| $el ++ $tail} },
_ => $input
}
}
def "str prepend" [head: string]: [string -> string, list<string> -> list<string>] {
let input = $in
match ($input | describe | str replace --regex '<.*' '') {
"string" => { $head ++ $input },
"list" => { $input | each {|el| $head ++ $el } },
_ => $input
}
}
#[test]
def test_append [] {
use std assert
assert equal ("foo" | str append "/") "foo/"
assert equal (["foo", "bar", "baz"] | str append "/") ["foo/", "bar/", "baz/"]
}
#[test]
def test_prepend [] {
use std assert
assert equal ("foo" | str prepend "/") "/foo"
assert equal (["foo", "bar", "baz"] | str prepend "/") ["/foo", "/bar", "/baz"]
}