From 3c1ae3a1ecdd85b8066475537b68836a0d7985c6 Mon Sep 17 00:00:00 2001 From: amtoine Date: Sat, 9 Sep 2023 16:11:01 +0200 Subject: [PATCH] implement the inverse transposition for tables --- src/nu/value.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/nu/value.rs b/src/nu/value.rs index fc2ac67..9c520b0 100644 --- a/src/nu/value.rs +++ b/src/nu/value.rs @@ -178,7 +178,31 @@ pub(crate) fn transpose(value: &Value) -> Value { _ => return value.clone(), } } else { - return value.clone(); + match value { + Value::List { vals, .. } => { + let mut rows = vec![]; + let cols: Vec = vals + .iter() + .map(|v| v.get_data_by_key("1").unwrap().as_string().unwrap()) + .collect(); + + for i in 0..(vals[0].columns().len() - 1) { + rows.push(Value::record( + Record { + cols: cols.clone(), + vals: vals + .iter() + .map(|v| v.get_data_by_key(&format!("{}", i + 2)).unwrap()) + .collect(), + }, + Span::unknown(), + )); + } + + return Value::list(rows, Span::unknown()); + } + _ => return value.clone(), + } } }