remove str from (#3500)

This commit is contained in:
Darren Schroeder 2021-05-27 12:18:02 -05:00 committed by GitHub
parent bcdf74562b
commit 48171f8e24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 18 additions and 295 deletions

View File

@ -282,7 +282,7 @@ pub(crate) use split::{Split, SplitChars, SplitColumn, SplitRow};
pub(crate) use split_by::SplitBy;
pub(crate) use str_::{
Str, StrCamelCase, StrCapitalize, StrCollect, StrContains, StrDowncase, StrEndsWith,
StrFindReplace, StrFrom, StrIndexOf, StrKebabCase, StrLPad, StrLength, StrPascalCase, StrRPad,
StrFindReplace, StrIndexOf, StrKebabCase, StrLPad, StrLength, StrPascalCase, StrRPad,
StrReverse, StrScreamingSnakeCase, StrSnakeCase, StrStartsWith, StrSubstring, StrToDatetime,
StrToDecimal, StrToInteger, StrTrim, StrTrimLeft, StrTrimRight, StrUpcase,
};
@ -333,7 +333,6 @@ mod tests {
whole_stream_command(StrUpcase),
whole_stream_command(StrCapitalize),
whole_stream_command(StrFindReplace),
whole_stream_command(StrFrom),
whole_stream_command(StrSubstring),
whole_stream_command(StrToDatetime),
whole_stream_command(StrContains),

View File

@ -89,7 +89,6 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
whole_stream_command(StrUpcase),
whole_stream_command(StrCapitalize),
whole_stream_command(StrFindReplace),
whole_stream_command(StrFrom),
whole_stream_command(StrSubstring),
whole_stream_command(StrToDatetime),
whole_stream_command(StrContains),

View File

@ -160,7 +160,7 @@ pub fn histogram(args: CommandArgs) -> Result<ActionStream, ShellError> {
"{}%",
// Some(2) < the number of digits
// true < group the digits
crate::commands::str_::from::action(&percentage, &name, Some(2), true)?
crate::commands::into::string::action(&percentage, &name, Some(2), true)?
.as_string()?
);
fact.insert_untagged("percentage", UntaggedValue::string(fmt_percentage));

View File

@ -1,7 +1,7 @@
mod binary;
mod command;
mod int;
mod string;
pub mod string;
pub use binary::SubCommand as IntoBinary;
pub use command::Command as Into;

View File

@ -1,273 +0,0 @@
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tagged;
use num_bigint::{BigInt, BigUint, ToBigInt};
// TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml)
use nu_data::base::shape::InlineShape;
use num_format::Locale;
use num_traits::{Pow, Signed};
use std::iter;
pub struct SubCommand;
struct Arguments {
decimals: Option<Tagged<u64>>,
group_digits: bool,
column_paths: Vec<ColumnPath>,
}
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"str from"
}
fn signature(&self) -> Signature {
Signature::build("str from")
.rest(
SyntaxShape::ColumnPath,
"optionally convert to string by column paths",
)
.named(
"decimals",
SyntaxShape::Int,
"decimal digits to which to round",
Some('d'),
)
}
fn usage(&self) -> &str {
"Converts numeric types to strings. Trims trailing zeros unless decimals parameter is specified."
}
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
operate(args)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "round to nearest integer",
example: "echo 1.7 | str from -d 0",
result: Some(vec![UntaggedValue::string("2").into_untagged_value()]),
},
/*
FIXME: this isn't currently supported because of num_format being out of date. Once it's updated, re-enable this
Example {
description: "format large number with localized digit grouping",
example: "= 1000000.2 | str from -g",
result: Some(vec![
UntaggedValue::string("1,000,000.2").into_untagged_value()
]),
},
*/
]
}
}
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
let (options, input) = args.extract(|params| {
Ok(Arguments {
decimals: params.get_flag("decimals")?,
group_digits: false,
column_paths: params.rest(0)?,
})
})?;
let digits = options.decimals.as_ref().map(|tagged| tagged.item);
let group_digits = options.group_digits;
Ok(input
.map(move |v| {
if options.column_paths.is_empty() {
ReturnSuccess::value(action(&v, v.tag(), digits, group_digits)?)
} else {
let mut ret = v;
for path in &options.column_paths {
ret = ret.swap_data_by_column_path(
path,
Box::new(move |old| action(old, old.tag(), digits, group_digits)),
)?;
}
ReturnSuccess::value(ret)
}
})
.to_action_stream())
}
// TODO If you're using the with-system-locale feature and you're on Windows, Clang 3.9 or higher is also required.
pub fn action(
input: &Value,
tag: impl Into<Tag>,
digits: Option<u64>,
group_digits: bool,
) -> Result<Value, ShellError> {
match &input.value {
UntaggedValue::Primitive(prim) => Ok(UntaggedValue::string(match prim {
Primitive::Int(int) => {
if group_digits {
format_int(*int) // int.to_formatted_string(*locale)
} else {
int.to_string()
}
}
Primitive::BigInt(int) => {
if group_digits {
format_bigint(int) // int.to_formatted_string(*locale)
} else {
int.to_string()
}
}
Primitive::Decimal(dec) => format_decimal(dec.clone(), digits, group_digits),
Primitive::String(a_string) => a_string.to_string(),
Primitive::Boolean(a_bool) => a_bool.to_string(),
Primitive::Date(a_date) => a_date.format("%c").to_string(),
Primitive::FilePath(a_filepath) => a_filepath.as_path().display().to_string(),
Primitive::Filesize(a_filesize) => {
let byte_string = InlineShape::format_bytes(*a_filesize, None);
byte_string.1
}
Primitive::Nothing => "nothing".to_string(),
_ => {
return Err(ShellError::unimplemented(&format!(
"str from for non-numeric primitives {:?}",
prim
)))
}
})
.into_value(tag)),
UntaggedValue::Row(_) => Err(ShellError::labeled_error(
"specify column to use 'str from'",
"found table",
input.tag.clone(),
)),
_ => Err(ShellError::unimplemented(
"str from for non-primitive, non-table types",
)),
}
}
fn format_int(int: i64) -> String {
format!("{}", int)
// TODO once platform-specific dependencies are stable (see Cargo.toml)
// #[cfg(windows)]
// {
// int.to_formatted_string(&Locale::en)
// }
// #[cfg(not(windows))]
// {
// match SystemLocale::default() {
// Ok(locale) => int.to_formatted_string(&locale),
// Err(_) => int.to_formatted_string(&Locale::en),
// }
// }
}
fn format_bigint(int: &BigInt) -> String {
format!("{}", int)
// TODO once platform-specific dependencies are stable (see Cargo.toml)
// #[cfg(windows)]
// {
// int.to_formatted_string(&Locale::en)
// }
// #[cfg(not(windows))]
// {
// match SystemLocale::default() {
// Ok(locale) => int.to_formatted_string(&locale),
// Err(_) => int.to_formatted_string(&Locale::en),
// }
// }
}
fn format_decimal(mut decimal: BigDecimal, digits: Option<u64>, group_digits: bool) -> String {
if let Some(n) = digits {
decimal = round_decimal(&decimal, n)
}
if decimal.is_integer() && (digits.is_none() || digits == Some(0)) {
let int = decimal
.to_bigint()
.expect("integer BigDecimal should convert to BigInt");
return if group_digits {
int.to_string()
} else {
format_bigint(&int)
};
}
let (int, exp) = decimal.as_bigint_and_exponent();
let factor = BigInt::from(10).pow(BigUint::from(exp as u64)); // exp > 0 for non-int decimal
let int_part = &int / &factor;
let dec_part = (&int % &factor)
.abs()
.to_biguint()
.expect("BigInt::abs should always produce positive signed BigInt and thus BigUInt")
.to_str_radix(10);
let dec_str = if let Some(n) = digits {
dec_part
.chars()
.chain(iter::repeat('0'))
.take(n as usize)
.collect()
} else {
String::from(dec_part.trim_end_matches('0'))
};
let format_default_loc = |int_part: BigInt| {
let loc = Locale::en;
//TODO: when num_format is available for recent bigint, replace this with the locale-based format
let (int_str, sep) = (format!("{}", int_part), String::from(loc.decimal()));
format!("{}{}{}", int_str, sep, dec_str)
};
format_default_loc(int_part)
// TODO once platform-specific dependencies are stable (see Cargo.toml)
// #[cfg(windows)]
// {
// format_default_loc(int_part)
// }
// #[cfg(not(windows))]
// {
// match SystemLocale::default() {
// Ok(sys_loc) => {
// let int_str = int_part.to_formatted_string(&sys_loc);
// let sep = String::from(sys_loc.decimal());
// format!("{}{}{}", int_str, sep, dec_str)
// }
// Err(_) => format_default_loc(int_part),
// }
// }
}
fn round_decimal(decimal: &BigDecimal, mut digits: u64) -> BigDecimal {
let mut mag = decimal.clone();
while mag >= BigDecimal::from(1) {
mag = mag / 10;
digits += 1;
}
decimal.with_prec(digits)
}
#[cfg(test)]
mod tests {
use super::ShellError;
use super::SubCommand;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
test_examples(SubCommand {})
}
}

View File

@ -6,7 +6,6 @@ mod contains;
mod downcase;
mod ends_with;
mod find_replace;
pub mod from;
mod index_of;
mod length;
mod lpad;
@ -32,7 +31,6 @@ pub use contains::SubCommand as StrContains;
pub use downcase::SubCommand as StrDowncase;
pub use ends_with::SubCommand as StrEndsWith;
pub use find_replace::SubCommand as StrFindReplace;
pub use from::SubCommand as StrFrom;
pub use index_of::SubCommand as StrIndexOf;
pub use length::SubCommand as StrLength;
pub use lpad::SubCommand as StrLPad;

View File

@ -9,7 +9,7 @@ fn reduce_table_column() {
| from json
| get total
| reduce -f 20 { $it + (math eval $"($acc)^1.05")}
| str from -d 1
| into string -d 1
"#
)
);
@ -25,7 +25,7 @@ fn reduce_table_column_with_path() {
echo "[{month:2,total:30}, {month:3,total:10}, {month:4,total:3}, {month:5,total:60}]"
| from json
| reduce -f 20 { $it.total + (math eval $"($acc)^1.05")}
| str from -d 1
| into string -d 1
"#
)
);

View File

@ -5,7 +5,7 @@ fn test_1() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1..5 | str from | str collect
echo 1..5 | into string | str collect
"#
)
);
@ -44,7 +44,7 @@ fn sum_one_to_four() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1..4 | str from | str collect "+" | math eval
echo 1..4 | into string | str collect "+" | math eval
"#
)
);

View File

@ -6,7 +6,7 @@ fn from_range() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1..5 | str from | to json
echo 1..5 | into string | to json
"#
)
);
@ -19,7 +19,7 @@ fn from_number() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 5 | str from
echo 5 | into string
"#
)
);
@ -32,7 +32,7 @@ fn from_decimal() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo 1.5 | str from
echo 1.5 | into string
"#
)
);
@ -45,7 +45,7 @@ fn from_boolean() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo $true | str from
echo $true | into string
"#
)
);
@ -58,7 +58,7 @@ fn from_string() {
let actual = nu!(
cwd: ".", pipeline(
r#"
echo "one" | str from
echo "one" | into string
"#
)
);
@ -79,7 +79,7 @@ fn from_filename() {
let actual = nu!(
cwd: dirs.test(),
"ls sample.toml | get name | str from"
"ls sample.toml | get name | into string"
);
assert_eq!(actual.out, "sample.toml");
@ -99,7 +99,7 @@ fn from_filesize() {
let actual = nu!(
cwd: dirs.test(),
"ls sample.toml | get size | str from"
"ls sample.toml | get size | into string"
);
assert_eq!(actual.out, "25 B");
@ -111,7 +111,7 @@ fn from_decimal_correct_trailing_zeros() {
let actual = nu!(
cwd: ".", pipeline(
r#"
= 1.23000 | str from -d 3
= 1.23000 | into string -d 3
"#
));
@ -123,7 +123,7 @@ fn from_int_decimal_correct_trailing_zeros() {
let actual = nu!(
cwd: ".", pipeline(
r#"
= 1.00000 | str from -d 3
= 1.00000 | into string -d 3
"#
));
@ -135,7 +135,7 @@ fn from_int_decimal_trim_trailing_zeros() {
let actual = nu!(
cwd: ".", pipeline(
r#"
= 1.00000 | str from | format "{$it} flat"
= 1.00000 | into string | format "{$it} flat"
"#
));
@ -149,7 +149,7 @@ fn from_table() {
r#"
echo '[{"name": "foo", "weight": 32.377}, {"name": "bar", "weight": 15.2}]'
| from json
| str from weight -d 2
| into string weight -d 2
"#
));