From 707cda345078553f3e878a100ca103a28f440705 Mon Sep 17 00:00:00 2001 From: Texas Toland Date: Wed, 20 Mar 2024 10:46:12 -0500 Subject: [PATCH] Clean up str append/prepend a little (#797) - Move to directory for other `str` contributions - Add simple `help` docs - Simplify type check since only 2 cases are possible @savente93 Are these okay? --- stdlib-candidate/std-rfc/mod.nu | 2 +- stdlib-candidate/std-rfc/set-env.nu | 38 +++++++++--------- stdlib-candidate/std-rfc/str.nu | 17 -------- stdlib-candidate/std-rfc/str/mod.nu | 1 + stdlib-candidate/std-rfc/str/xpend.nu | 39 +++++++++++++++++++ stdlib-candidate/tests/mod.nu | 2 +- .../tests/{str.nu => str_xpend.nu} | 0 7 files changed, 61 insertions(+), 38 deletions(-) delete mode 100644 stdlib-candidate/std-rfc/str.nu create mode 100644 stdlib-candidate/std-rfc/str/mod.nu create mode 100644 stdlib-candidate/std-rfc/str/xpend.nu rename stdlib-candidate/tests/{str.nu => str_xpend.nu} (100%) diff --git a/stdlib-candidate/std-rfc/mod.nu b/stdlib-candidate/std-rfc/mod.nu index e4e473b6..d2c7dc9e 100644 --- a/stdlib-candidate/std-rfc/mod.nu +++ b/stdlib-candidate/std-rfc/mod.nu @@ -1,6 +1,6 @@ # modules export module record/ -export module str.nu +export module str/ # commands export use fs.nu * export use set-env.nu * diff --git a/stdlib-candidate/std-rfc/set-env.nu b/stdlib-candidate/std-rfc/set-env.nu index 1e0b3c49..cf7f5a4e 100644 --- a/stdlib-candidate/std-rfc/set-env.nu +++ b/stdlib-candidate/std-rfc/set-env.nu @@ -13,25 +13,25 @@ # Add a config hook # > set-env -a config.hooks.pre_prompt 'ellie | print' export def --env main [ - field: cell-path # The environment variable name or nested option cell path - value: any # The value to set or append - --append (-a) # Append to the previous value or wrap in a new list + field: cell-path # The environment variable name or nested option cell path + value: any # The value to set or append + --append (-a) # Append to the previous value or wrap in a new list ]: nothing -> nothing { - def 'get or' [default field] { - get --ignore-errors $field | default $default - } - let value = if $append { - $env | get or [] $field | append $value - } else { - $value - } - let field = $field | to text | split row . - let value = match $field { - [_] => $value - [$root, ..$field] => { - let field = $field | into cell-path - $env | get or {} $root | upsert $field $value + def 'get or' [default field] { + get --ignore-errors $field | default $default } - } - load-env { ($field | first): $value } + let value = if $append { + $env | get or [] $field | append $value + } else { + $value + } + let field = $field | to text | split row . + let value = match $field { + [_] => $value + [$root, ..$field] => { + let field = $field | into cell-path + $env | get or {} $root | upsert $field $value + } + } + load-env { ($field | first): $value } } diff --git a/stdlib-candidate/std-rfc/str.nu b/stdlib-candidate/std-rfc/str.nu deleted file mode 100644 index 9c182b1e..00000000 --- a/stdlib-candidate/std-rfc/str.nu +++ /dev/null @@ -1,17 +0,0 @@ -export def append [tail: string]: [string -> string, list -> list] { - let input = $in - match ($input | describe | str replace --regex '<.*' '') { - "string" => { $input ++ $tail }, - "list" => { $input | each {|el| $el ++ $tail} }, - _ => $input - } -} - -export def prepend [head: string]: [string -> string, list -> list] { - let input = $in - match ($input | describe | str replace --regex '<.*' '') { - "string" => { $head ++ $input }, - "list" => { $input | each {|el| $head ++ $el } }, - _ => $input - } -} diff --git a/stdlib-candidate/std-rfc/str/mod.nu b/stdlib-candidate/std-rfc/str/mod.nu new file mode 100644 index 00000000..fbff9d99 --- /dev/null +++ b/stdlib-candidate/std-rfc/str/mod.nu @@ -0,0 +1 @@ +export use xpend.nu * diff --git a/stdlib-candidate/std-rfc/str/xpend.nu b/stdlib-candidate/std-rfc/str/xpend.nu new file mode 100644 index 00000000..8968def7 --- /dev/null +++ b/stdlib-candidate/std-rfc/str/xpend.nu @@ -0,0 +1,39 @@ +# Append a suffix to an input string or list of strings. +# +# Examples: +# Output 'hello world' +# > 'hello' | str append ' world' +# +# Output file names suffixed with '_world' +# > ls | get name | str append _world +export def append [ + suffix: string +]: [string -> string, list -> list] { + let input = $in + let append = { $in + $suffix } + if ($input | describe) == string { + $input | do $append + } else { + $input | each $append + } +} + +# Prepend a prefix to an input string or list of strings. +# +# Examples: +# Output 'hello world' +# > 'world' | str prepend 'hello ' +# +# Output file names prefixed with 'hello_' +# > ls | get name | str prepend hello_ +export def prepend [ + prefix: string +]: [string -> string, list -> list] { + let input = $in + let prepend = { $prefix + $in } + if ($input | describe) == string { + $input | do $prepend + } else { + $input | each $prepend + } +} diff --git a/stdlib-candidate/tests/mod.nu b/stdlib-candidate/tests/mod.nu index 8f03760e..1a40e7e0 100644 --- a/stdlib-candidate/tests/mod.nu +++ b/stdlib-candidate/tests/mod.nu @@ -1,3 +1,3 @@ export module fs.nu export module record.nu -export module str.nu +export module str_xpend.nu diff --git a/stdlib-candidate/tests/str.nu b/stdlib-candidate/tests/str_xpend.nu similarity index 100% rename from stdlib-candidate/tests/str.nu rename to stdlib-candidate/tests/str_xpend.nu