mirror of
https://github.com/nushell/nushell.git
synced 2025-04-08 11:58:32 +02:00
Move 'keep' to 'take' (#5123)
This commit is contained in:
parent
e86c1b118e
commit
4f974efeba
@ -80,11 +80,11 @@ pub fn create_default_context(cwd: impl AsRef<Path>) -> EngineState {
|
|||||||
Headers,
|
Headers,
|
||||||
Insert,
|
Insert,
|
||||||
SplitBy,
|
SplitBy,
|
||||||
Keep,
|
Take,
|
||||||
Merge,
|
Merge,
|
||||||
Move,
|
Move,
|
||||||
KeepUntil,
|
TakeWhile,
|
||||||
KeepWhile,
|
TakeUntil,
|
||||||
Last,
|
Last,
|
||||||
Length,
|
Length,
|
||||||
Lines,
|
Lines,
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
mod keep_;
|
|
||||||
mod keep_until;
|
|
||||||
mod keep_while;
|
|
||||||
|
|
||||||
pub use keep_::Keep;
|
|
||||||
pub use keep_until::KeepUntil;
|
|
||||||
pub use keep_while::KeepWhile;
|
|
@ -17,7 +17,6 @@ mod group;
|
|||||||
mod group_by;
|
mod group_by;
|
||||||
mod headers;
|
mod headers;
|
||||||
mod insert;
|
mod insert;
|
||||||
mod keep;
|
|
||||||
mod last;
|
mod last;
|
||||||
mod length;
|
mod length;
|
||||||
mod lines;
|
mod lines;
|
||||||
@ -38,6 +37,7 @@ mod skip;
|
|||||||
mod sort;
|
mod sort;
|
||||||
mod sort_by;
|
mod sort_by;
|
||||||
mod split_by;
|
mod split_by;
|
||||||
|
mod take;
|
||||||
mod transpose;
|
mod transpose;
|
||||||
mod uniq;
|
mod uniq;
|
||||||
mod update;
|
mod update;
|
||||||
@ -67,7 +67,6 @@ pub use group::Group;
|
|||||||
pub use group_by::GroupBy;
|
pub use group_by::GroupBy;
|
||||||
pub use headers::Headers;
|
pub use headers::Headers;
|
||||||
pub use insert::Insert;
|
pub use insert::Insert;
|
||||||
pub use keep::*;
|
|
||||||
pub use last::Last;
|
pub use last::Last;
|
||||||
pub use length::Length;
|
pub use length::Length;
|
||||||
pub use lines::Lines;
|
pub use lines::Lines;
|
||||||
@ -88,6 +87,7 @@ pub use skip::*;
|
|||||||
pub use sort::Sort;
|
pub use sort::Sort;
|
||||||
pub use sort_by::SortBy;
|
pub use sort_by::SortBy;
|
||||||
pub use split_by::SplitBy;
|
pub use split_by::SplitBy;
|
||||||
|
pub use take::*;
|
||||||
pub use transpose::Transpose;
|
pub use transpose::Transpose;
|
||||||
pub use uniq::*;
|
pub use uniq::*;
|
||||||
pub use update::Update;
|
pub use update::Update;
|
||||||
|
7
crates/nu-command/src/filters/take/mod.rs
Normal file
7
crates/nu-command/src/filters/take/mod.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
mod take_;
|
||||||
|
mod take_until;
|
||||||
|
mod take_while;
|
||||||
|
|
||||||
|
pub use take_::Take;
|
||||||
|
pub use take_until::TakeUntil;
|
||||||
|
pub use take_while::TakeWhile;
|
@ -9,28 +9,28 @@ use nu_protocol::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Keep;
|
pub struct Take;
|
||||||
|
|
||||||
impl Command for Keep {
|
impl Command for Take {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"keep"
|
"take"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build(self.name())
|
Signature::build(self.name())
|
||||||
.optional("n", SyntaxShape::Int, "the number of elements to keep")
|
.optional("n", SyntaxShape::Int, "the number of elements to take")
|
||||||
.category(Category::Filters)
|
.category(Category::Filters)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Keep the first n elements of the input."
|
"Take the first n elements of the input."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Keep two elements",
|
description: "Take two elements",
|
||||||
example: "echo [[editions]; [2015] [2018] [2021]] | keep 2",
|
example: "echo [[editions]; [2015] [2018] [2021]] | take 2",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![
|
vals: vec![
|
||||||
Value::Record {
|
Value::Record {
|
||||||
@ -48,8 +48,8 @@ impl Command for Keep {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Keep the first value",
|
description: "Take the first value",
|
||||||
example: "echo [2 4 6 8] | keep",
|
example: "echo [2 4 6 8] | take",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![Value::test_int(2)],
|
vals: vec![Value::test_int(2)],
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
@ -94,12 +94,12 @@ impl Command for Keep {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::Keep;
|
use crate::Take;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples() {
|
||||||
use crate::test_examples;
|
use crate::test_examples;
|
||||||
|
|
||||||
test_examples(Keep {})
|
test_examples(Take {})
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,11 +7,11 @@ use nu_protocol::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct KeepUntil;
|
pub struct TakeUntil;
|
||||||
|
|
||||||
impl Command for KeepUntil {
|
impl Command for TakeUntil {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"keep until"
|
"take until"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
@ -19,19 +19,19 @@ impl Command for KeepUntil {
|
|||||||
.required(
|
.required(
|
||||||
"predicate",
|
"predicate",
|
||||||
SyntaxShape::RowCondition,
|
SyntaxShape::RowCondition,
|
||||||
"the predicate that kept element must not match",
|
"the predicate that element(s) must not match",
|
||||||
)
|
)
|
||||||
.category(Category::Filters)
|
.category(Category::Filters)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Keep elements of the input until a predicate is true."
|
"Take elements of the input until a predicate is true."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Keep until the element is positive",
|
description: "Take until the element is positive",
|
||||||
example: "echo [-1 -2 9 1] | keep until $it > 0",
|
example: "echo [-1 -2 9 1] | take until $it > 0",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![Value::test_int(-1), Value::test_int(-2)],
|
vals: vec![Value::test_int(-1), Value::test_int(-2)],
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
@ -86,12 +86,12 @@ impl Command for KeepUntil {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::KeepUntil;
|
use crate::TakeUntil;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples() {
|
||||||
use crate::test_examples;
|
use crate::test_examples;
|
||||||
|
|
||||||
test_examples(KeepUntil)
|
test_examples(TakeUntil)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,11 +7,11 @@ use nu_protocol::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct KeepWhile;
|
pub struct TakeWhile;
|
||||||
|
|
||||||
impl Command for KeepWhile {
|
impl Command for TakeWhile {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
"keep while"
|
"take while"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
@ -19,19 +19,19 @@ impl Command for KeepWhile {
|
|||||||
.required(
|
.required(
|
||||||
"predicate",
|
"predicate",
|
||||||
SyntaxShape::RowCondition,
|
SyntaxShape::RowCondition,
|
||||||
"the predicate that kept element must not match",
|
"the predicate that element(s) must match",
|
||||||
)
|
)
|
||||||
.category(Category::Filters)
|
.category(Category::Filters)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Keep elements of the input while a predicate is true."
|
"Take elements of the input while a predicate is true."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![Example {
|
vec![Example {
|
||||||
description: "Keep while the element is negative",
|
description: "Take while the element is negative",
|
||||||
example: "echo [-1 -2 9 1] | keep while $it < 0",
|
example: "echo [-1 -2 9 1] | take while $it < 0",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![Value::test_int(-1), Value::test_int(-2)],
|
vals: vec![Value::test_int(-1), Value::test_int(-2)],
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
@ -86,12 +86,12 @@ impl Command for KeepWhile {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::KeepWhile;
|
use crate::TakeWhile;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_examples() {
|
fn test_examples() {
|
||||||
use crate::test_examples;
|
use crate::test_examples;
|
||||||
|
|
||||||
test_examples(KeepWhile)
|
test_examples(TakeWhile)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -27,7 +27,6 @@ mod histogram;
|
|||||||
mod insert;
|
mod insert;
|
||||||
mod into_filesize;
|
mod into_filesize;
|
||||||
mod into_int;
|
mod into_int;
|
||||||
mod keep;
|
|
||||||
mod last;
|
mod last;
|
||||||
mod length;
|
mod length;
|
||||||
mod lines;
|
mod lines;
|
||||||
@ -60,6 +59,7 @@ mod split_by;
|
|||||||
mod split_column;
|
mod split_column;
|
||||||
mod split_row;
|
mod split_row;
|
||||||
mod str_;
|
mod str_;
|
||||||
|
mod take;
|
||||||
mod touch;
|
mod touch;
|
||||||
mod uniq;
|
mod uniq;
|
||||||
mod update;
|
mod update;
|
||||||
|
@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rows() {
|
fn rows() {
|
||||||
Playground::setup("keep_test_1", |dirs, sandbox| {
|
Playground::setup("take_test_1", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
"caballeros.csv",
|
"caballeros.csv",
|
||||||
r#"
|
r#"
|
||||||
@ -20,7 +20,7 @@ fn rows() {
|
|||||||
cwd: dirs.test(), pipeline(
|
cwd: dirs.test(), pipeline(
|
||||||
r#"
|
r#"
|
||||||
open caballeros.csv
|
open caballeros.csv
|
||||||
| keep 3
|
| take 3
|
||||||
| get lucky_code
|
| get lucky_code
|
||||||
| math sum
|
| math sum
|
||||||
"#
|
"#
|
@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn condition_is_met() {
|
fn condition_is_met() {
|
||||||
Playground::setup("keep_until_test_1", |dirs, sandbox| {
|
Playground::setup("take_until_test_1", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
"caballeros.txt",
|
"caballeros.txt",
|
||||||
r#"
|
r#"
|
||||||
@ -39,7 +39,7 @@ fn condition_is_met() {
|
|||||||
| str collect (char nl)
|
| str collect (char nl)
|
||||||
| from csv
|
| from csv
|
||||||
| skip while "Chicken Collection" != "Blue Chickens"
|
| skip while "Chicken Collection" != "Blue Chickens"
|
||||||
| keep until "Chicken Collection" == "Red Chickens"
|
| take until "Chicken Collection" == "Red Chickens"
|
||||||
| skip 1
|
| skip 1
|
||||||
| into int "31/04/2020"
|
| into int "31/04/2020"
|
||||||
| get "31/04/2020"
|
| get "31/04/2020"
|
@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn condition_is_met() {
|
fn condition_is_met() {
|
||||||
Playground::setup("keep_while_test_1", |dirs, sandbox| {
|
Playground::setup("take_while_test_1", |dirs, sandbox| {
|
||||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||||
"caballeros.txt",
|
"caballeros.txt",
|
||||||
r#"
|
r#"
|
||||||
@ -39,7 +39,7 @@ fn condition_is_met() {
|
|||||||
| str collect (char nl)
|
| str collect (char nl)
|
||||||
| from csv
|
| from csv
|
||||||
| skip 1
|
| skip 1
|
||||||
| keep while "Chicken Collection" != "Blue Chickens"
|
| take while "Chicken Collection" != "Blue Chickens"
|
||||||
| into int "31/04/2020"
|
| into int "31/04/2020"
|
||||||
| get "31/04/2020"
|
| get "31/04/2020"
|
||||||
| math sum
|
| math sum
|
Loading…
Reference in New Issue
Block a user