nushell/crates/nu-utils/standard_library/std.nu
Antoine Stevan 2ccbefe01e
REFACTOR: move the standard library to a less-confusing place (#8265)
# Description
we've discussed a bit about the location of the standard library in the
#standard-library channel of the discord server => **the previous
location, `crates/nu-utils/src/sample_config/`, was a bit confusing**
- is `std.nu` a config file, just as `default_config.nu` or
`default_env.nu`?
- what is this `tests.nu` file inside the `sample_config/`?

in this PR, i propose moving the standard library to
`crates/nu-utils/standard_library/` for a few reasons:
- `std.nu` is not a config file, so it should not be next to config
files in a `sample_config/` directory
- `tests.nu` is confusing if mixed with other unrelated files
- `crates/nu-utils/` appears to be a good place for the standard library
as it is meant to be a tool for `nushell`
- i thought it would be strange to have `std.nu` inside
`crates/nu-utils/src/` as this directory is generally filled with `rust`
files, right?

these are the reasons why i choose to propose
`crates/nu-utils/standard_library/` 😋

# User-Facing Changes
the standard library is now used with
```bash
use crates/nu-utils/standard_library/std.nu
```
and the tests are run with
```bash
nu crates/nu-utils/standard_library/tests.nu
```

# Tests + Formatting
```bash
$nothing
```

# After Submitting
```bash
$nothing
```
2023-03-01 16:40:50 -06:00

125 lines
3.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

def _assert [
cond: bool
msg: string
] {
if not $cond {
error make {msg: $msg}
}
}
# ```nushell
# >_ assert ($a == 3)
# >_ assert ($a != 3)
# Error:
# × condition given to `assert` does not hold
# ╭─[entry #12:5:1]
# 5 │ if not $cond {
# 6 │ error make {msg: $msg}
# · ─────┬────
# · ╰── originates from here
# 7 │ }
# ╰────
# ```
export def assert [cond: bool] {
_assert $cond "condition given to `assert` does not hold"
}
# ```nushell
# >_ assert_eq $a "a string"
# Error:
# × left and right operand of `assert eq` should have the same type
# ╭─[entry #12:5:1]
# 5 │ if not $cond {
# 6 │ error make {msg: $msg}
# · ─────┬────
# · ╰── originates from here
# 7 │ }
# ╰────
#
# >_ assert_eq $a 3
# >_ assert_eq $a 1
# Error:
# × left is not equal to right
# ╭─[entry #12:5:1]
# 5 │ if not $cond {
# 6 │ error make {msg: $msg}
# · ─────┬────
# · ╰── originates from here
# 7 │ }
# ╰────
# ```
export def "assert eq" [left: any, right: any] {
_assert (($left | describe) == ($right | describe)) $"left and right operand of `assert eq` should have the same type"
_assert ($left == $right) "left is not equal to right"
}
# ```nushell
# >_ assert_ne $a "a string"
# Error:
# × left and right operand of `assert eq` should have the same type
# ╭─[entry #12:5:1]
# 5 │ if not $cond {
# 6 │ error make {msg: $msg}
# · ─────┬────
# · ╰── originates from here
# 7 │ }
# ╰────
#
# >_ assert_ne $a 1
# >_ assert_ne $a 3
# Error:
# × left is equal to right
# ╭─[entry #12:5:1]
# 5 │ if not $cond {
# 6 │ error make {msg: $msg}
# · ─────┬────
# · ╰── originates from here
# 7 │ }
# ╰────
# ```
export def "assert ne" [left: any, right: any] {
_assert (($left | describe) == ($right | describe)) $"left and right operand of `assert eq` should have the same type"
_assert ($left != $right) "left is equal to right"
}
# ```nushell
# >_ let branches = {
# ))) 1: { print "this is the 1st branch"}
# ))) 2: { print "this is the 2nd branch" }
# ))) 3: { print "this is the 3rd branch" }
# ))) 4: { print "this is the 4th branch" }
# ))) }
#
# >_ match 1 $branches
# ))) match 2 $branches
# ))) match 3 $branches
# ))) match 4 $branches
# ))) match 5 $branches
# this is the 1st branch
# this is the 2nd branch
# this is the 3rd branch
# this is the 4th branch
#
# >_ match 1 $branches { "this is the default branch" }
# ))) match 2 $branches { "this is the default branch" }
# ))) match 3 $branches { "this is the default branch" }
# ))) match 4 $branches { "this is the default branch" }
# ))) match 5 $branches { "this is the default branch" }
# this is the 1st branch
# this is the 2nd branch
# this is the 3rd branch
# this is the 4th branch
# this is the default branch
# ```
export def match [
input:string
matchers:record
default?: block
] {
if (($matchers | get -i $input) != null) {
$matchers | get $input | do $in
} else if ($default != null) {
do $default
}
}