allow last to work with ranges (#11906)

# Description

This PR allows `last` to work with ranges in the same way that `first`
does. It also adds a couple examples demonstrating it.

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Darren Schroeder 2024-02-20 06:28:50 -06:00 committed by GitHub
parent c0bac5a440
commit 63ccc62a24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View file

@ -69,6 +69,11 @@ impl Command for First {
example: "0x[01 23 45] | first 2", example: "0x[01 23 45] | first 2",
result: Some(Value::binary(vec![0x01, 0x23], Span::test_data())), result: Some(Value::binary(vec![0x01, 0x23], Span::test_data())),
}, },
Example {
description: "Return the first item of a range",
example: "1..3 | first",
result: Some(Value::test_int(1)),
},
] ]
} }
} }

View file

@ -27,6 +27,7 @@ impl Command for Last {
Type::Any, Type::Any,
), ),
(Type::Binary, Type::Binary), (Type::Binary, Type::Binary),
(Type::Range, Type::Any),
]) ])
.optional( .optional(
"rows", "rows",
@ -60,6 +61,11 @@ impl Command for Last {
description: "Return the last 2 bytes of a binary value", description: "Return the last 2 bytes of a binary value",
result: Some(Value::binary(vec![0x23, 0x45], Span::test_data())), result: Some(Value::binary(vec![0x23, 0x45], Span::test_data())),
}, },
Example {
example: "1..3 | last",
description: "Return the last item of a range",
result: Some(Value::test_int(3)),
},
] ]
} }