Skip to content

Commit

Permalink
add empty tranpose and tests that should pass
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Sep 9, 2023
1 parent 235a33e commit db3e9aa
Showing 1 changed file with 83 additions and 1 deletion.
84 changes: 83 additions & 1 deletion src/nu/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,45 @@ pub(crate) fn is_table(value: &Value) -> bool {
}
}

/// this effectively implements the following idempotent `transpose` command written in Nushell
/// ```nushell
/// alias "core transpose" = transpose
///
/// def transpose []: [table -> any, record -> table] {
/// let data = $in
///
/// if ($data | columns) == (seq 1 ($data | columns | length) | into string) {
/// if ($data | columns | length) == 2 {
/// return ($data | core transpose --header-row | into record)
/// } else {
/// return ($data | core transpose --header-row)
/// }
/// }
///
/// $data | core transpose | rename --block {
/// ($in | str replace "column" "" | into int) + 1 | into string
/// }
/// }
///
/// #[test]
/// def transposition [] {
/// use std assert
///
/// assert equal (ls | transpose explore | transpose) (ls)
/// assert equal (open Cargo.toml | transpose | transpose) (open Cargo.toml)
/// }
/// ```
pub(crate) fn transpose(value: &Value) -> Value {

Check warning on line 151 in src/nu/value.rs

View workflow job for this annotation

GitHub Actions / fmt-check-clippy

function `transpose` is never used

Check warning on line 151 in src/nu/value.rs

View workflow job for this annotation

GitHub Actions / fmt-check-clippy

function `transpose` is never used

Check failure on line 151 in src/nu/value.rs

View workflow job for this annotation

GitHub Actions / fmt-check-clippy

function `transpose` is never used

Check warning on line 151 in src/nu/value.rs

View workflow job for this annotation

GitHub Actions / tests (ubuntu-20.04)

function `transpose` is never used
value.clone()
}

#[cfg(test)]
mod tests {
use super::{is_table, mutate_value_cell};
use crate::nu::cell_path::{to_path_member_vec, PM};
use crate::nu::{
cell_path::{to_path_member_vec, PM},
value::transpose,
};
use nu_protocol::{ast::CellPath, record, Config, Value};

fn default_value_repr(value: &Value) -> String {
Expand Down Expand Up @@ -368,4 +403,51 @@ mod tests {

assert_eq!(is_table(&Value::test_int(0)), false);
}

#[test]
fn transposition() {
let record = Value::test_record(record! {
"a" => Value::test_int(1),
"b" => Value::test_int(2),
});
let expected = Value::test_list(vec![
Value::test_record(record! {
"1" => Value::test_string("a"),
"2" => Value::test_int(1),
}),
Value::test_record(record! {
"1" => Value::test_string("b"),
"2" => Value::test_int(2),
}),
]);
assert_eq!(transpose(&record), expected);
// make sure `transpose` is an *involution*
assert_eq!(transpose(&expected), record);

let table = Value::test_list(vec![
Value::test_record(record! {
"a" => Value::test_int(1),
"b" => Value::test_int(2),
}),
Value::test_record(record! {
"a" => Value::test_int(3),
"b" => Value::test_int(4),
}),
]);
let expected = Value::test_list(vec![
Value::test_record(record! {
"1" => Value::test_string("a"),
"2" => Value::test_int(1),
"3" => Value::test_int(3),
}),
Value::test_record(record! {
"1" => Value::test_string("b"),
"2" => Value::test_int(2),
"3" => Value::test_int(4),
}),
]);
assert_eq!(transpose(&table), expected);
// make sure `transpose` is an *involution*
assert_eq!(transpose(&expected), table);
}
}

0 comments on commit db3e9aa

Please sign in to comment.