mirror of
https://github.com/nushell/nushell.git
synced 2024-12-24 16:09:11 +01:00
Add support for primitive values to sort-by (#1241)
* Remove redundant clone * Add support for primitive values to sort-by #1238
This commit is contained in:
parent
47d987d37f
commit
e059c74a06
@ -1,7 +1,7 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, SyntaxShape, Value};
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_source::Tagged;
|
||||
use nu_value_ext::get_data_by_key;
|
||||
|
||||
@ -41,12 +41,26 @@ fn sort_by(
|
||||
Ok(OutputStream::new(async_stream! {
|
||||
let mut vec = context.input.drain_vec().await;
|
||||
|
||||
if vec.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
match &vec[0] {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(_),
|
||||
..
|
||||
} => {
|
||||
vec.sort();
|
||||
},
|
||||
_ => {
|
||||
let calc_key = |item: &Value| {
|
||||
rest.iter()
|
||||
.map(|f| get_data_by_key(item, f.borrow_spanned()).map(|i| i.clone()))
|
||||
.map(|f| get_data_by_key(item, f.borrow_spanned()))
|
||||
.collect::<Vec<Option<Value>>>()
|
||||
};
|
||||
vec.sort_by_cached_key(calc_key);
|
||||
},
|
||||
};
|
||||
|
||||
for item in vec {
|
||||
yield item.into();
|
||||
|
@ -21,3 +21,21 @@ fn by_column() {
|
||||
|
||||
assert_eq!(actual, "description");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sort_primitive_values() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
open cargo_sample.toml --raw
|
||||
| lines
|
||||
| skip 1
|
||||
| first 6
|
||||
| sort-by
|
||||
| first 1
|
||||
| echo $it
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual, "authors = [\"Yehuda Katz <wycats@gmail.com>\"]");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user