mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 22:27:43 +02:00
Implement chunk_by operation (#14410)
# Description This pull requests implements a new ~~partition-by~~ `chunk-by` command. The operation takes a closure and partitions the input list into sublists based on the return value of the closure. - fixes #14149 Examples, tests and and documentation were added accordingly.  
This commit is contained in:
58
crates/nu-command/tests/commands/chunk_by.rs
Normal file
58
crates/nu-command/tests/commands/chunk_by.rs
Normal file
@ -0,0 +1,58 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn chunk_by_on_empty_input_returns_empty_list() {
|
||||
let actual = nu!("[] | chunk-by {|it| $it} | to nuon");
|
||||
assert!(actual.err.is_empty());
|
||||
assert_eq!(actual.out, "[]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chunk_by_strings_works() {
|
||||
let sample = r#"
|
||||
[a a a b b b c c c a a a]
|
||||
"#;
|
||||
|
||||
let actual = nu!(pipeline(&format!(
|
||||
r#"
|
||||
{sample}
|
||||
| chunk-by {{|it| $it}}
|
||||
| to nuon
|
||||
"#
|
||||
)));
|
||||
|
||||
assert_eq!(actual.out, "[[a, a, a], [b, b, b], [c, c, c], [a, a, a]]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chunk_by_field_works() {
|
||||
let sample = r#"[
|
||||
{
|
||||
name: bob,
|
||||
age: 20,
|
||||
cool: false
|
||||
},
|
||||
{
|
||||
name: jane,
|
||||
age: 30,
|
||||
cool: false
|
||||
},
|
||||
{
|
||||
name: marie,
|
||||
age: 19,
|
||||
cool: true
|
||||
},
|
||||
{
|
||||
name: carl,
|
||||
age: 36,
|
||||
cool: true
|
||||
} ]"#;
|
||||
|
||||
let actual = nu!(pipeline(&format!(
|
||||
r#"{sample}
|
||||
| chunk-by {{|it| $it.cool}}
|
||||
| length"#
|
||||
)));
|
||||
|
||||
assert_eq!(actual.out, "2");
|
||||
}
|
Reference in New Issue
Block a user