group-by can generate custom grouping key by block evaluation. (#2172)

This commit is contained in:
Andrés N. Robalino
2020-07-14 08:45:19 -05:00
committed by GitHub
parent 8551e06d9e
commit f2c4d22739
7 changed files with 163 additions and 28 deletions

View File

@ -7,16 +7,16 @@ use nu_value_ext::as_string;
#[allow(clippy::type_complexity)]
pub fn group(
values: &Value,
grouper: &Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
grouper: &Option<Box<dyn Fn(usize, &Value) -> Result<String, ShellError> + Send>>,
tag: impl Into<Tag>,
) -> Result<Value, ShellError> {
let tag = tag.into();
let mut groups: IndexMap<String, Vec<Value>> = IndexMap::new();
for value in values.table_entries() {
for (idx, value) in values.table_entries().enumerate() {
let group_key = if let Some(ref grouper) = grouper {
grouper(&value)
grouper(idx, &value)
} else {
as_string(&value)
};

View File

@ -7,7 +7,7 @@ use crate::utils::data::group;
#[allow(clippy::type_complexity)]
pub fn split(
value: &Value,
splitter: &Option<Box<dyn Fn(&Value) -> Result<String, ShellError> + Send>>,
splitter: &Option<Box<dyn Fn(usize, &Value) -> Result<String, ShellError> + Send>>,
tag: impl Into<Tag>,
) -> Result<Value, ShellError> {
let tag = tag.into();