mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 08:23:24 +01: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,
|
||||
Insert,
|
||||
SplitBy,
|
||||
Keep,
|
||||
Take,
|
||||
Merge,
|
||||
Move,
|
||||
KeepUntil,
|
||||
KeepWhile,
|
||||
TakeWhile,
|
||||
TakeUntil,
|
||||
Last,
|
||||
Length,
|
||||
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 headers;
|
||||
mod insert;
|
||||
mod keep;
|
||||
mod last;
|
||||
mod length;
|
||||
mod lines;
|
||||
@ -38,6 +37,7 @@ mod skip;
|
||||
mod sort;
|
||||
mod sort_by;
|
||||
mod split_by;
|
||||
mod take;
|
||||
mod transpose;
|
||||
mod uniq;
|
||||
mod update;
|
||||
@ -67,7 +67,6 @@ pub use group::Group;
|
||||
pub use group_by::GroupBy;
|
||||
pub use headers::Headers;
|
||||
pub use insert::Insert;
|
||||
pub use keep::*;
|
||||
pub use last::Last;
|
||||
pub use length::Length;
|
||||
pub use lines::Lines;
|
||||
@ -88,6 +87,7 @@ pub use skip::*;
|
||||
pub use sort::Sort;
|
||||
pub use sort_by::SortBy;
|
||||
pub use split_by::SplitBy;
|
||||
pub use take::*;
|
||||
pub use transpose::Transpose;
|
||||
pub use uniq::*;
|
||||
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)]
|
||||
pub struct Keep;
|
||||
pub struct Take;
|
||||
|
||||
impl Command for Keep {
|
||||
impl Command for Take {
|
||||
fn name(&self) -> &str {
|
||||
"keep"
|
||||
"take"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
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)
|
||||
}
|
||||
|
||||
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> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Keep two elements",
|
||||
example: "echo [[editions]; [2015] [2018] [2021]] | keep 2",
|
||||
description: "Take two elements",
|
||||
example: "echo [[editions]; [2015] [2018] [2021]] | take 2",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
@ -48,8 +48,8 @@ impl Command for Keep {
|
||||
}),
|
||||
},
|
||||
Example {
|
||||
description: "Keep the first value",
|
||||
example: "echo [2 4 6 8] | keep",
|
||||
description: "Take the first value",
|
||||
example: "echo [2 4 6 8] | take",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_int(2)],
|
||||
span: Span::test_data(),
|
||||
@ -94,12 +94,12 @@ impl Command for Keep {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Keep;
|
||||
use crate::Take;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(Keep {})
|
||||
test_examples(Take {})
|
||||
}
|
||||
}
|
@ -7,11 +7,11 @@ use nu_protocol::{
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct KeepUntil;
|
||||
pub struct TakeUntil;
|
||||
|
||||
impl Command for KeepUntil {
|
||||
impl Command for TakeUntil {
|
||||
fn name(&self) -> &str {
|
||||
"keep until"
|
||||
"take until"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
@ -19,19 +19,19 @@ impl Command for KeepUntil {
|
||||
.required(
|
||||
"predicate",
|
||||
SyntaxShape::RowCondition,
|
||||
"the predicate that kept element must not match",
|
||||
"the predicate that element(s) must not match",
|
||||
)
|
||||
.category(Category::Filters)
|
||||
}
|
||||
|
||||
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> {
|
||||
vec![Example {
|
||||
description: "Keep until the element is positive",
|
||||
example: "echo [-1 -2 9 1] | keep until $it > 0",
|
||||
description: "Take until the element is positive",
|
||||
example: "echo [-1 -2 9 1] | take until $it > 0",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_int(-1), Value::test_int(-2)],
|
||||
span: Span::test_data(),
|
||||
@ -86,12 +86,12 @@ impl Command for KeepUntil {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::KeepUntil;
|
||||
use crate::TakeUntil;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(KeepUntil)
|
||||
test_examples(TakeUntil)
|
||||
}
|
||||
}
|
@ -7,11 +7,11 @@ use nu_protocol::{
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct KeepWhile;
|
||||
pub struct TakeWhile;
|
||||
|
||||
impl Command for KeepWhile {
|
||||
impl Command for TakeWhile {
|
||||
fn name(&self) -> &str {
|
||||
"keep while"
|
||||
"take while"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
@ -19,19 +19,19 @@ impl Command for KeepWhile {
|
||||
.required(
|
||||
"predicate",
|
||||
SyntaxShape::RowCondition,
|
||||
"the predicate that kept element must not match",
|
||||
"the predicate that element(s) must match",
|
||||
)
|
||||
.category(Category::Filters)
|
||||
}
|
||||
|
||||
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> {
|
||||
vec![Example {
|
||||
description: "Keep while the element is negative",
|
||||
example: "echo [-1 -2 9 1] | keep while $it < 0",
|
||||
description: "Take while the element is negative",
|
||||
example: "echo [-1 -2 9 1] | take while $it < 0",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_int(-1), Value::test_int(-2)],
|
||||
span: Span::test_data(),
|
||||
@ -86,12 +86,12 @@ impl Command for KeepWhile {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::KeepWhile;
|
||||
use crate::TakeWhile;
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
use crate::test_examples;
|
||||
|
||||
test_examples(KeepWhile)
|
||||
test_examples(TakeWhile)
|
||||
}
|
||||
}
|
@ -27,7 +27,6 @@ mod histogram;
|
||||
mod insert;
|
||||
mod into_filesize;
|
||||
mod into_int;
|
||||
mod keep;
|
||||
mod last;
|
||||
mod length;
|
||||
mod lines;
|
||||
@ -60,6 +59,7 @@ mod split_by;
|
||||
mod split_column;
|
||||
mod split_row;
|
||||
mod str_;
|
||||
mod take;
|
||||
mod touch;
|
||||
mod uniq;
|
||||
mod update;
|
||||
|
@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
fn rows() {
|
||||
Playground::setup("keep_test_1", |dirs, sandbox| {
|
||||
Playground::setup("take_test_1", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![FileWithContentToBeTrimmed(
|
||||
"caballeros.csv",
|
||||
r#"
|
||||
@ -20,7 +20,7 @@ fn rows() {
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
open caballeros.csv
|
||||
| keep 3
|
||||
| take 3
|
||||
| get lucky_code
|
||||
| math sum
|
||||
"#
|
@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
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(
|
||||
"caballeros.txt",
|
||||
r#"
|
||||
@ -39,7 +39,7 @@ fn condition_is_met() {
|
||||
| str collect (char nl)
|
||||
| from csv
|
||||
| skip while "Chicken Collection" != "Blue Chickens"
|
||||
| keep until "Chicken Collection" == "Red Chickens"
|
||||
| take until "Chicken Collection" == "Red Chickens"
|
||||
| skip 1
|
||||
| into int "31/04/2020"
|
||||
| get "31/04/2020"
|
@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
|
||||
|
||||
#[test]
|
||||
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(
|
||||
"caballeros.txt",
|
||||
r#"
|
||||
@ -39,7 +39,7 @@ fn condition_is_met() {
|
||||
| str collect (char nl)
|
||||
| from csv
|
||||
| skip 1
|
||||
| keep while "Chicken Collection" != "Blue Chickens"
|
||||
| take while "Chicken Collection" != "Blue Chickens"
|
||||
| into int "31/04/2020"
|
||||
| get "31/04/2020"
|
||||
| math sum
|
Loading…
Reference in New Issue
Block a user