to csv and to tsv (#412)

* MathEval Variance and Stddev

* Fix tests and linting

* Typo

* Deal with streams when they are not tables

* ToTsv and ToCsv
This commit is contained in:
Luccas Mateus
2021-12-02 23:02:22 -03:00
committed by GitHub
parent 349e83abd0
commit 3d8394a909
6 changed files with 363 additions and 0 deletions

View File

@ -287,6 +287,38 @@ impl Value {
}
}
pub fn get_data_by_key(&self, name: &str) -> Option<Value> {
match self {
Value::Record { cols, vals, .. } => cols
.iter()
.zip(vals.iter())
.find(|(col, _)| col == &name)
.map(|(_, val)| val.clone()),
Value::List { vals, span } => {
let mut out = vec![];
for item in vals {
match item {
Value::Record { .. } => match item.get_data_by_key(name) {
Some(v) => out.push(v),
None => out.push(Value::nothing(*span)),
},
_ => out.push(Value::nothing(*span)),
}
}
if !out.is_empty() {
Some(Value::List {
vals: out,
span: *span,
})
} else {
None
}
}
_ => None,
}
}
/// Convert Value into string. Note that Streams will be consumed.
pub fn into_string(self, separator: &str, config: &Config) -> String {
match self {