forked from extern/nushell
Make str collect
take an optional separator value (#2289)
* Make `str collect` take an optional separator value * Make `str collect` take an optional separator value * Add some tests * Fix my tests
This commit is contained in:
parent
cda53b6cda
commit
a88f5c7ae7
@ -1,10 +1,16 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value};
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
||||||
|
use nu_source::Tagged;
|
||||||
|
|
||||||
pub struct SubCommand;
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct SubCommandArgs {
|
||||||
|
separator: Option<Tagged<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl WholeStreamCommand for SubCommand {
|
impl WholeStreamCommand for SubCommand {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
@ -12,7 +18,11 @@ impl WholeStreamCommand for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("str collect")
|
Signature::build("str collect").desc(self.usage()).optional(
|
||||||
|
"separator",
|
||||||
|
SyntaxShape::String,
|
||||||
|
"the separator to put between the different values",
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
@ -22,16 +32,9 @@ impl WholeStreamCommand for SubCommand {
|
|||||||
async fn run(
|
async fn run(
|
||||||
&self,
|
&self,
|
||||||
args: CommandArgs,
|
args: CommandArgs,
|
||||||
_registry: &CommandRegistry,
|
registry: &CommandRegistry,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
let output = args
|
collect(args, registry).await
|
||||||
.input
|
|
||||||
.collect_string(args.call_info.name_tag.clone())
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(OutputStream::one(ReturnSuccess::value(
|
|
||||||
UntaggedValue::string(output.item).into_value(output.tag),
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
@ -43,6 +46,24 @@ impl WholeStreamCommand for SubCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn collect(
|
||||||
|
args: CommandArgs,
|
||||||
|
registry: &CommandRegistry,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let tag = args.call_info.name_tag.clone();
|
||||||
|
let (SubCommandArgs { separator }, input) = args.process(registry).await?;
|
||||||
|
let separator = separator.map(|tagged| tagged.item).unwrap_or_default();
|
||||||
|
|
||||||
|
let strings: Vec<Result<String, ShellError>> =
|
||||||
|
input.map(|value| value.as_string()).collect().await;
|
||||||
|
let strings: Vec<String> = strings.into_iter().collect::<Result<_, _>>()?;
|
||||||
|
let output = strings.join(&separator);
|
||||||
|
|
||||||
|
Ok(OutputStream::one(ReturnSuccess::value(
|
||||||
|
UntaggedValue::string(output).into_value(tag),
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::SubCommand;
|
use super::SubCommand;
|
||||||
|
53
crates/nu-cli/tests/commands/str_/collect.rs
Normal file
53
crates/nu-cli/tests/commands/str_/collect.rs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
use nu_test_support::{nu, pipeline};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_1() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
echo 1..5 | str from | str collect
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "12345");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_2() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
echo [a b c d] | str collect "<sep>"
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "a<sep>b<sep>c<sep>d");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn construct_a_path() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
echo [sample txt] | str collect "."
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.out, "sample.txt");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sum_one_to_four() {
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: ".", pipeline(
|
||||||
|
r#"
|
||||||
|
echo 1..4 | str from | str collect "+" | math eval
|
||||||
|
"#
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
assert!(actual.out.contains("10.0"));
|
||||||
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
mod collect;
|
||||||
|
|
||||||
use nu_test_support::fs::Stub::FileWithContent;
|
use nu_test_support::fs::Stub::FileWithContent;
|
||||||
use nu_test_support::playground::Playground;
|
use nu_test_support::playground::Playground;
|
||||||
use nu_test_support::{nu, pipeline};
|
use nu_test_support::{nu, pipeline};
|
Loading…
Reference in New Issue
Block a user