Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib-candidate] Clean up str append/prepend a little #797

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib-candidate/std-rfc/mod.nu
Original file line number Diff line number Diff line change
@@ -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 *
38 changes: 19 additions & 19 deletions stdlib-candidate/std-rfc/set-env.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
17 changes: 0 additions & 17 deletions stdlib-candidate/std-rfc/str.nu

This file was deleted.

1 change: 1 addition & 0 deletions stdlib-candidate/std-rfc/str/mod.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export use xpend.nu *
39 changes: 39 additions & 0 deletions stdlib-candidate/std-rfc/str/xpend.nu
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion stdlib-candidate/tests/mod.nu
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export module fs.nu
export module record.nu
export module str.nu
export module str_xpend.nu
File renamed without changes.