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:
Shaurya Shubham 2020-08-02 12:59:29 +05:30 committed by GitHub
parent cda53b6cda
commit a88f5c7ae7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 11 deletions

View file

@ -1,10 +1,16 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
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;
#[derive(Deserialize)]
pub struct SubCommandArgs {
separator: Option<Tagged<String>>,
}
#[async_trait]
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
@ -12,7 +18,11 @@ impl WholeStreamCommand for SubCommand {
}
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 {
@ -22,16 +32,9 @@ impl WholeStreamCommand for SubCommand {
async fn run(
&self,
args: CommandArgs,
_registry: &CommandRegistry,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let output = args
.input
.collect_string(args.call_info.name_tag.clone())
.await?;
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(output.item).into_value(output.tag),
)))
collect(args, registry).await
}
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)]
mod tests {
use super::SubCommand;

View 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"));
}

View file

@ -1,3 +1,5 @@
mod collect;
use nu_test_support::fs::Stub::FileWithContent;
use nu_test_support::playground::Playground;
use nu_test_support::{nu, pipeline};