mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-02 23:27:45 +00:00
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?
This commit is contained in:
parent
7d662ad5c5
commit
707cda3450
7 changed files with 61 additions and 38 deletions
|
@ -1,6 +1,6 @@
|
||||||
# modules
|
# modules
|
||||||
export module record/
|
export module record/
|
||||||
export module str.nu
|
export module str/
|
||||||
# commands
|
# commands
|
||||||
export use fs.nu *
|
export use fs.nu *
|
||||||
export use set-env.nu *
|
export use set-env.nu *
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
export def 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export def 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
|
|
||||||
}
|
|
||||||
}
|
|
1
stdlib-candidate/std-rfc/str/mod.nu
Normal file
1
stdlib-candidate/std-rfc/str/mod.nu
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export use xpend.nu *
|
39
stdlib-candidate/std-rfc/str/xpend.nu
Normal file
39
stdlib-candidate/std-rfc/str/xpend.nu
Normal file
|
@ -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<string> -> list<string>] {
|
||||||
|
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<string> -> list<string>] {
|
||||||
|
let input = $in
|
||||||
|
let prepend = { $prefix + $in }
|
||||||
|
if ($input | describe) == string {
|
||||||
|
$input | do $prepend
|
||||||
|
} else {
|
||||||
|
$input | each $prepend
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,3 +1,3 @@
|
||||||
export module fs.nu
|
export module fs.nu
|
||||||
export module record.nu
|
export module record.nu
|
||||||
export module str.nu
|
export module str_xpend.nu
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue