Move capitalize, downcase, upcase to /cases; fix some example descriptions; clarify usage text (#5572)

Co-authored-by: kyle <kyle@archtop.local>
This commit is contained in:
krober
2022-05-17 23:55:43 -05:00
committed by GitHub
parent 7a78171b34
commit 3e09158afc
9 changed files with 16 additions and 16 deletions

View File

@@ -0,0 +1,145 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Category;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str capitalize"
}
fn signature(&self) -> Signature {
Signature::build("str capitalize")
.rest(
"rest",
SyntaxShape::CellPath,
"optionally capitalize text by column paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Capitalize first letter of text"
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Capitalize contents",
example: "'good day' | str capitalize",
result: Some(Value::String {
val: "Good day".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Capitalize contents",
example: "'anton' | str capitalize",
result: Some(Value::String {
val: "Anton".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Capitalize a column in a table",
example: "[[lang, gems]; [nu_test, 100]] | str capitalize lang",
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![
Value::String {
val: "Nu_test".to_string(),
span: Span::test_data(),
},
Value::test_int(100),
],
}],
span: Span::test_data(),
}),
},
]
}
}
fn operate(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
input.map(
move |v| {
if column_paths.is_empty() {
action(&v, head)
} else {
let mut ret = v;
for path in &column_paths {
let r =
ret.update_cell_path(&path.members, Box::new(move |old| action(old, head)));
if let Err(error) = r {
return Value::Error { error };
}
}
ret
}
},
engine_state.ctrlc.clone(),
)
}
fn action(input: &Value, head: Span) -> Value {
match input {
Value::String { val, .. } => Value::String {
val: uppercase_helper(val),
span: head,
},
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(
"Input's type is {}. This command only works with strings.",
other.get_type()
),
head,
),
},
}
}
fn uppercase_helper(s: &str) -> String {
// apparently more performant https://stackoverflow.com/questions/38406793/why-is-capitalizing-the-first-letter-of-a-string-so-convoluted-in-rust
let mut chars = s.chars();
match chars.next() {
None => String::new(),
Some(f) => f.to_uppercase().collect::<String>() + chars.as_str(),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@@ -0,0 +1,163 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Category;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str downcase"
}
fn signature(&self) -> Signature {
Signature::build("str downcase")
.rest(
"rest",
SyntaxShape::CellPath,
"optionally downcase text by column paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Make text lowercase"
}
fn search_terms(&self) -> Vec<&str> {
vec!["lower case", "lowercase"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Downcase contents",
example: "'NU' | str downcase",
result: Some(Value::String {
val: "nu".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Downcase contents",
example: "'TESTa' | str downcase",
result: Some(Value::String {
val: "testa".to_string(),
span: Span::test_data(),
}),
},
Example {
description: "Downcase contents",
example: "[[ColA ColB]; [Test ABC]] | str downcase ColA",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::String {
val: "test".to_string(),
span: Span::test_data(),
},
Value::String {
val: "ABC".to_string(),
span: Span::test_data(),
},
],
span: Span::test_data(),
}],
span: Span::test_data(),
}),
},
Example {
description: "Downcase contents",
example: "[[ColA ColB]; [Test ABC]] | str downcase ColA ColB",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string()],
vals: vec![
Value::String {
val: "test".to_string(),
span: Span::test_data(),
},
Value::String {
val: "abc".to_string(),
span: Span::test_data(),
},
],
span: Span::test_data(),
}],
span: Span::test_data(),
}),
},
]
}
}
fn operate(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
input.map(
move |v| {
if column_paths.is_empty() {
action(&v, head)
} else {
let mut ret = v;
for path in &column_paths {
let r =
ret.update_cell_path(&path.members, Box::new(move |old| action(old, head)));
if let Err(error) = r {
return Value::Error { error };
}
}
ret
}
},
engine_state.ctrlc.clone(),
)
}
fn action(input: &Value, head: Span) -> Value {
match input {
Value::String { val, .. } => Value::String {
val: val.to_ascii_lowercase(),
span: head,
},
other => Value::Error {
error: ShellError::UnsupportedInput(
format!(
"Input's type is {}. This command only works with strings.",
other.get_type()
),
head,
),
},
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@@ -1,16 +1,22 @@
pub mod camel_case;
pub mod capitalize;
pub mod downcase;
pub mod kebab_case;
pub mod pascal_case;
pub mod screaming_snake_case;
pub mod snake_case;
pub mod str_;
pub mod upcase;
pub use camel_case::SubCommand as StrCamelCase;
pub use capitalize::SubCommand as StrCapitalize;
pub use downcase::SubCommand as StrDowncase;
pub use kebab_case::SubCommand as StrKebabCase;
pub use pascal_case::SubCommand as StrPascalCase;
pub use screaming_snake_case::SubCommand as StrScreamingSnakeCase;
pub use snake_case::SubCommand as StrSnakeCase;
pub use str_::Str;
pub use upcase::SubCommand as StrUpcase;
use nu_engine::CallExt;

View File

@@ -41,7 +41,7 @@ impl Command for SubCommand {
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to camelCase",
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "NuShell" | str screaming-snake-case"#,
result: Some(Value::String {
val: "NU_SHELL".to_string(),
@@ -49,7 +49,7 @@ impl Command for SubCommand {
}),
},
Example {
description: "convert a string to camelCase",
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "this_is_the_second_case" | str screaming-snake-case"#,
result: Some(Value::String {
val: "THIS_IS_THE_SECOND_CASE".to_string(),
@@ -57,7 +57,7 @@ impl Command for SubCommand {
}),
},
Example {
description: "convert a string to camelCase",
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#""this-is-the-first-case" | str screaming-snake-case"#,
result: Some(Value::String {
val: "THIS_IS_THE_FIRST_CASE".to_string(),

View File

@@ -40,7 +40,7 @@ impl Command for SubCommand {
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to camelCase",
description: "convert a string to snake_case",
example: r#" "NuShell" | str snake-case"#,
result: Some(Value::String {
val: "nu_shell".to_string(),
@@ -48,7 +48,7 @@ impl Command for SubCommand {
}),
},
Example {
description: "convert a string to camelCase",
description: "convert a string to snake_case",
example: r#" "this_is_the_second_case" | str snake-case"#,
result: Some(Value::String {
val: "this_is_the_second_case".to_string(),
@@ -56,7 +56,7 @@ impl Command for SubCommand {
}),
},
Example {
description: "convert a string to camelCase",
description: "convert a string to snake_case",
example: r#""this-is-the-first-case" | str snake-case"#,
result: Some(Value::String {
val: "this_is_the_first_case".to_string(),
@@ -64,7 +64,7 @@ impl Command for SubCommand {
}),
},
Example {
description: "convert a column from a table to snake-case",
description: "convert a column from a table to snake_case",
example: r#"[[lang, gems]; [nuTest, 100]] | str snake-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {

View File

@@ -18,7 +18,7 @@ impl Command for Str {
}
fn usage(&self) -> &str {
"Various commands for working with string data."
"Various commands for working with string data"
}
fn run(

View File

@@ -0,0 +1,113 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str upcase"
}
fn signature(&self) -> Signature {
Signature::build("str upcase").rest(
"rest",
SyntaxShape::CellPath,
"optionally upcase text by column paths",
)
}
fn usage(&self) -> &str {
"Make text uppercase"
}
fn search_terms(&self) -> Vec<&str> {
vec!["uppercase", "upper case"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Upcase contents",
example: "'nu' | str upcase",
result: Some(Value::test_string("NU")),
}]
}
}
fn operate(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let head = call.head;
let column_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
input.map(
move |v| {
if column_paths.is_empty() {
action(&v, head)
} else {
let mut ret = v;
for path in &column_paths {
let r =
ret.update_cell_path(&path.members, Box::new(move |old| action(old, head)));
if let Err(error) = r {
return Value::Error { error };
}
}
ret
}
},
engine_state.ctrlc.clone(),
)
}
fn action(input: &Value, head: Span) -> Value {
match input {
Value::String { val: s, .. } => Value::String {
val: s.to_uppercase(),
span: head,
},
other => {
let got = format!("Expected string but got {}", other.get_type());
Value::Error {
error: ShellError::UnsupportedInput(got, head),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::{action, SubCommand};
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
#[test]
fn upcases() {
let word = Value::test_string("andres");
let actual = action(&word, Span::test_data());
let expected = Value::test_string("ANDRES");
assert_eq!(actual, expected);
}
}