Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix split list when separater is the first element of list #7355

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions crates/nu-command/src/strings/split/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ impl Command for SubCommand {
span: Span::test_data(),
}),
},
Example {
description: "Split a list of chars into two lists",
example: "[a, b, c, d, a, e, f, g] | split list a",
result: Some(Value::List {
vals: vec![
Value::List {
vals: vec![
Value::test_string("b"),
Value::test_string("c"),
Value::test_string("d"),
],
span: Span::test_data(),
},
Value::List {
vals: vec![
Value::test_string("e"),
Value::test_string("f"),
Value::test_string("g"),
],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
]
}
}
Expand All @@ -110,12 +135,14 @@ fn split_list(
let mut returned_list = Vec::new();
let iter = input.into_interruptible_iter(engine_state.ctrlc.clone());
for val in iter {
if val == separator && !temp_list.is_empty() {
returned_list.push(Value::List {
vals: temp_list.clone(),
span: call.head,
});
temp_list = Vec::new();
if val == separator {
if !temp_list.is_empty() {
returned_list.push(Value::List {
vals: temp_list.clone(),
span: call.head,
});
temp_list = Vec::new();
}
} else {
temp_list.push(val);
}
Expand Down