Cratification: move some str case commands to nu-cmd-extra (#9926)

I am moving the following str case commands to nu-cmd-extra (as
discussed in the core team meeting the other day)

* camel-case
* kebab-case
* pascal-case
* screaming-snake-case
* snake-case
* title-case
This commit is contained in:
Michael Angerman
2023-08-06 06:40:44 -07:00
committed by GitHub
parent 066790552a
commit 58f98a4260
17 changed files with 145 additions and 27 deletions

View File

@ -1,99 +0,0 @@
use inflector::cases::camelcase::to_camel_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str camel-case"
}
fn signature(&self) -> Signature {
Signature::build("str camel-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to camelCase."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "caps", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_camel_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to camelCase",
example: " 'NuShell' | str camel-case",
result: Some(Value::test_string("nuShell")),
},
Example {
description: "convert a string to camelCase",
example: "'this-is-the-first-case' | str camel-case",
result: Some(Value::test_string("thisIsTheFirstCase")),
},
Example {
description: "convert a string to camelCase",
example: " 'this_is_the_second_case' | str camel-case",
result: Some(Value::test_string("thisIsTheSecondCase")),
},
Example {
description: "convert a column from a table to camelCase",
example: r#"[[lang, gems]; [nu_test, 100]] | str camel-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("nuTest"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -1,98 +0,0 @@
use inflector::cases::kebabcase::to_kebab_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str kebab-case"
}
fn signature(&self) -> Signature {
Signature::build("str kebab-case")
.input_output_types(vec![
(Type::String, Type::String),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to kebab-case."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "hyphens", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_kebab_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to kebab-case",
example: "'NuShell' | str kebab-case",
result: Some(Value::test_string("nu-shell")),
},
Example {
description: "convert a string to kebab-case",
example: "'thisIsTheFirstCase' | str kebab-case",
result: Some(Value::test_string("this-is-the-first-case")),
},
Example {
description: "convert a string to kebab-case",
example: "'THIS_IS_THE_SECOND_CASE' | str kebab-case",
result: Some(Value::test_string("this-is-the-second-case")),
},
Example {
description: "convert a column from a table to kebab-case",
example: r#"[[lang, gems]; [nuTest, 100]] | str kebab-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("nu-test"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -1,23 +1,11 @@
mod camel_case;
mod capitalize;
mod downcase;
mod kebab_case;
mod pascal_case;
mod screaming_snake_case;
mod snake_case;
mod str_;
mod title_case;
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 title_case::SubCommand as StrTitleCase;
pub use upcase::SubCommand as StrUpcase;
use nu_engine::CallExt;

View File

@ -1,99 +0,0 @@
use inflector::cases::pascalcase::to_pascal_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str pascal-case"
}
fn signature(&self) -> Signature {
Signature::build("str pascal-case")
.input_output_types(vec![
(Type::String, Type::String),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to PascalCase."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "caps", "upper", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_pascal_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to PascalCase",
example: "'nu-shell' | str pascal-case",
result: Some(Value::test_string("NuShell")),
},
Example {
description: "convert a string to PascalCase",
example: "'this-is-the-first-case' | str pascal-case",
result: Some(Value::test_string("ThisIsTheFirstCase")),
},
Example {
description: "convert a string to PascalCase",
example: "'this_is_the_second_case' | str pascal-case",
result: Some(Value::test_string("ThisIsTheSecondCase")),
},
Example {
description: "convert a column from a table to PascalCase",
example: r#"[[lang, gems]; [nu_test, 100]] | str pascal-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("NuTest"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -1,98 +0,0 @@
use inflector::cases::screamingsnakecase::to_screaming_snake_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str screaming-snake-case"
}
fn signature(&self) -> Signature {
Signature::build("str screaming-snake-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to SCREAMING_SNAKE_CASE."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "underscore", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_screaming_snake_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "NuShell" | str screaming-snake-case"#,
result: Some(Value::test_string("NU_SHELL")),
},
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#" "this_is_the_second_case" | str screaming-snake-case"#,
result: Some(Value::test_string("THIS_IS_THE_SECOND_CASE")),
},
Example {
description: "convert a string to SCREAMING_SNAKE_CASE",
example: r#""this-is-the-first-case" | str screaming-snake-case"#,
result: Some(Value::test_string("THIS_IS_THE_FIRST_CASE")),
},
Example {
description: "convert a column from a table to SCREAMING_SNAKE_CASE",
example: r#"[[lang, gems]; [nu_test, 100]] | str screaming-snake-case lang"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("NU_TEST"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -1,98 +0,0 @@
use inflector::cases::snakecase::to_snake_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str snake-case"
}
fn signature(&self) -> Signature {
Signature::build("str snake-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to snake_case."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "underscore", "lower", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_snake_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to snake_case",
example: r#" "NuShell" | str snake-case"#,
result: Some(Value::test_string("nu_shell")),
},
Example {
description: "convert a string to snake_case",
example: r#" "this_is_the_second_case" | str snake-case"#,
result: Some(Value::test_string("this_is_the_second_case")),
},
Example {
description: "convert a string to snake_case",
example: r#""this-is-the-first-case" | str snake-case"#,
result: Some(Value::test_string("this_is_the_first_case")),
},
Example {
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 {
span: Span::test_data(),
cols: vec!["lang".to_string(), "gems".to_string()],
vals: vec![Value::test_string("nu_test"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View File

@ -1,94 +0,0 @@
use inflector::cases::titlecase::to_title_case;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
use crate::operate;
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str title-case"
}
fn signature(&self) -> Signature {
Signature::build("str title-case")
.input_output_types(vec![
(Type::String, Type::String),
(
Type::List(Box::new(Type::String)),
Type::List(Box::new(Type::String)),
),
(Type::Table(vec![]), Type::Table(vec![])),
(Type::Record(vec![]), Type::Record(vec![])),
])
.allow_variants_without_examples(true)
.rest(
"rest",
SyntaxShape::CellPath,
"For a data structure input, convert strings at the given cell paths",
)
.category(Category::Strings)
}
fn usage(&self) -> &str {
"Convert a string to Title Case."
}
fn search_terms(&self) -> Vec<&str> {
vec!["convert", "style", "convention"]
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
operate(engine_state, stack, call, input, &to_title_case)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert a string to Title Case",
example: "'nu-shell' | str title-case",
result: Some(Value::test_string("Nu Shell")),
},
Example {
description: "convert a string to Title Case",
example: "'this is a test case' | str title-case",
result: Some(Value::test_string("This Is A Test Case")),
},
Example {
description: "convert a column from a table to Title Case",
example: r#"[[title, count]; ['nu test', 100]] | str title-case title"#,
result: Some(Value::List {
vals: vec![Value::Record {
span: Span::test_data(),
cols: vec!["title".to_string(), "count".to_string()],
vals: vec![Value::test_string("Nu Test"), Value::test_int(100)],
}],
span: Span::test_data(),
}),
},
]
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}