Standardize case conversion command name and implement kebabcase

This commit is contained in:
Philip Koperski 2021-07-17 16:05:05 -04:00
parent b5a022a454
commit ed7bc9ac7e
18 changed files with 170 additions and 34 deletions

View file

@ -145,17 +145,18 @@
* [std::string::Base64Decode (base64_decode)](#std__string__Base64Decode)
* [std::string::Base64Encode (base64_encode)](#std__string__Base64Encode)
* [std::string::BytesToString (bytes_to_string)](#std__string__BytesToString)
* [std::string::CamelCase (camel_case)](#std__string__CamelCase)
* [std::string::CamelCase (camelcase)](#std__string__CamelCase)
* [std::string::Concat (concat)](#std__string__Concat)
* [std::string::Contains (contains)](#std__string__Contains)
* [std::string::EndsWith (ends_with)](#std__string__EndsWith)
* [std::string::Equals (equals, eq)](#std__string__Equals)
* [std::string::IndexOf (indexof)](#std__string__IndexOf)
* [std::string::IsEmpty (is_empty)](#std__string__IsEmpty)
* [std::string::KebabCase (kebabcase)](#std__string__KebabCase)
* [std::string::LastIndexOf (last_indexof)](#std__string__LastIndexOf)
* [std::string::Length (length, strlen)](#std__string__Length)
* [std::string::Replace (replace)](#std__string__Replace)
* [std::string::SnakeCase (snake_case)](#std__string__SnakeCase)
* [std::string::SnakeCase (snakecase)](#std__string__SnakeCase)
* [std::string::Split (split)](#std__string__Split)
* [std::string::StartsWith (starts_with)](#std__string__StartsWith)
* [std::string::StringToBytes (string_to_bytes)](#std__string__StringToBytes)
@ -5461,7 +5462,7 @@ bytes_to_string
<a name="std__string__CamelCase"></a>
## std::string::CamelCase
```sh
var = camel_case text
var = camelcase text
```
Converts the provided string into camel case.
@ -5478,13 +5479,13 @@ The converted string.
#### Examples
```sh
string = camel_case "hello, world!"
string = camelcase "hello, world!"
assert_eq ${string} "HelloWorld"
```
#### Aliases:
camel_case
camelcase
<a name="std__string__Concat"></a>
@ -5718,6 +5719,35 @@ index = last_indexof " some text " some
#### Aliases:
last_indexof
<a name="std__string__KebabCase"></a>
## std::string::KebabCase
```sh
var = kebabcase text
```
Converts the provided string into kebab case.
All non-alphanumeric characters are ignored.
#### Parameters
The string to convert.
#### Return Value
The converted string.
#### Examples
```sh
string = kebabcase "hello, world!"
assert_eq ${string} "hello-world"
```
#### Aliases:
kebabcase
<a name="std__string__Length"></a>
## std::string::Length
```sh
@ -5807,7 +5837,7 @@ replace
<a name="std__string__SnakeCase"></a>
## std::string::SnakeCase
```sh
var = snake_case text
var = snakecase text
```
Converts the provided string into snake case.
@ -5824,13 +5854,13 @@ The converted string.
#### Examples
```sh
string = snake_case "Hello, World!"
string = snakecase "Hello, World!"
assert_eq ${string} "hello_world"
```
#### Aliases:
snake_case
snakecase
<a name="std__string__Split"></a>

View file

@ -1,5 +1,5 @@
```sh
var = camel_case text
var = camelcase text
```
Converts the provided string into camel case.
@ -16,6 +16,7 @@ The converted string.
#### Examples
```sh
string = camel_case "hello, world!"
string = camelcase "hello, world!"
assert_eq ${string} "HelloWorld"
```

View file

@ -16,7 +16,7 @@ impl Command for CommandImpl {
}
fn aliases(&self) -> Vec<String> {
vec!["camel_case".to_string()]
vec!["camelcase".to_string()]
}
fn help(&self) -> String {
@ -31,8 +31,8 @@ impl Command for CommandImpl {
if arguments.is_empty() {
CommandResult::Error("No arguments provided.".to_string())
} else {
let snake_case = heck::CamelCase::to_camel_case(arguments[0].as_str());
CommandResult::Continue(Some(snake_case))
let value = heck::CamelCase::to_camel_case(arguments[0].as_str());
CommandResult::Continue(Some(value))
}
}
}

View file

@ -9,14 +9,14 @@ fn common_functions() {
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = camel_case", "out");
test::run_script_and_error(vec![create("")], "out = camelcase", "out");
}
#[test]
fn run_single_argument() {
test::run_script_and_validate(
vec![create("")],
r#"out = camel_case "hello, world!""#,
r#"out = camelcase "hello, world!""#,
CommandValidation::Match("out".to_string(), "HelloWorld".to_string()),
);
}

View file

@ -0,0 +1,22 @@
```sh
var = kebobcase text
```
Converts the provided string into kebob case.
All non-alphanumeric characters are ignored.
#### Parameters
The string to convert.
#### Return Value
The converted string.
#### Examples
```sh
string = kebobcase "Hello, World!"
assert_eq ${string} "hello-world"
```

View file

@ -0,0 +1,44 @@
use crate::utils::pckg;
use duckscript::types::command::{Command, CommandResult};
#[cfg(test)]
#[path = "./mod_test.rs"]
mod mod_test;
#[derive(Clone)]
pub(crate) struct CommandImpl {
package: String,
}
impl Command for CommandImpl {
fn name(&self) -> String {
pckg::concat(&self.package, "KebabCase")
}
fn aliases(&self) -> Vec<String> {
vec!["kebabcase".to_string()]
}
fn help(&self) -> String {
include_str!("help.md").to_string()
}
fn clone_and_box(&self) -> Box<dyn Command> {
Box::new((*self).clone())
}
fn run(&self, arguments: Vec<String>) -> CommandResult {
if arguments.is_empty() {
CommandResult::Error("No arguments provided.".to_string())
} else {
let value = heck::KebabCase::to_kebab_case(arguments[0].as_str());
CommandResult::Continue(Some(value))
}
}
}
pub(crate) fn create(package: &str) -> Box<dyn Command> {
Box::new(CommandImpl {
package: package.to_string(),
})
}

View file

@ -0,0 +1,22 @@
use super::*;
use crate::test;
use crate::test::CommandValidation;
#[test]
fn common_functions() {
test::test_common_command_functions(create(""));
}
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = kebabcase", "out");
}
#[test]
fn run_single_argument() {
test::run_script_and_validate(
vec![create("")],
r#"out = kebabcase "Hello, World!""#,
CommandValidation::Match("out".to_string(), "hello-world".to_string()),
);
}

View file

@ -18,3 +18,4 @@ The converted string.
string = lowercase "Hello World"
assert_eq ${string} "hello world"
```

View file

@ -31,8 +31,8 @@ impl Command for CommandImpl {
if arguments.is_empty() {
CommandResult::Error("No arguments provided.".to_string())
} else {
let lowercase = arguments[0].to_lowercase();
CommandResult::Continue(Some(lowercase))
let value = arguments[0].to_lowercase();
CommandResult::Continue(Some(value))
}
}
}

View file

@ -2,18 +2,19 @@ mod base64;
mod base64_decode;
mod base64_encode;
pub(crate) mod bytes_to_string;
mod camel_case;
mod camelcase;
mod concat;
mod contains;
mod ends_with;
pub(crate) mod equals;
mod indexof;
mod is_empty;
mod kebabcase;
mod last_indexof;
mod length;
mod lowercase;
mod replace;
mod snake_case;
mod snakecase;
mod split;
mod starts_with;
pub(crate) mod string_to_bytes;
@ -36,18 +37,19 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
commands.set(base64_decode::create(&package))?;
commands.set(base64_encode::create(&package))?;
commands.set(bytes_to_string::create(&package))?;
commands.set(camel_case::create(&package))?;
commands.set(camelcase::create(&package))?;
commands.set(concat::create(&package)?)?;
commands.set(contains::create(&package))?;
commands.set(ends_with::create(&package))?;
commands.set(equals::create(&package))?;
commands.set(indexof::create(&package))?;
commands.set(is_empty::create(&package))?;
commands.set(kebabcase::create(&package))?;
commands.set(last_indexof::create(&package))?;
commands.set(length::create(&package))?;
commands.set(lowercase::create(&package))?;
commands.set(replace::create(&package))?;
commands.set(snake_case::create(&package))?;
commands.set(snakecase::create(&package))?;
commands.set(split::create(&package))?;
commands.set(starts_with::create(&package))?;
commands.set(string_to_bytes::create(&package))?;

View file

@ -1,5 +1,5 @@
```sh
var = snake_case text
var = snakecase text
```
Converts the provided string into snake case.
@ -16,6 +16,7 @@ The converted string.
#### Examples
```sh
string = snake_case "Hello, World!"
string = snakecase "Hello, World!"
assert_eq ${string} "hello_world"
```

View file

@ -16,7 +16,7 @@ impl Command for CommandImpl {
}
fn aliases(&self) -> Vec<String> {
vec!["snake_case".to_string()]
vec!["snakecase".to_string()]
}
fn help(&self) -> String {
@ -31,8 +31,8 @@ impl Command for CommandImpl {
if arguments.is_empty() {
CommandResult::Error("No arguments provided.".to_string())
} else {
let snake_case = heck::SnakeCase::to_snake_case(arguments[0].as_str());
CommandResult::Continue(Some(snake_case))
let value = heck::SnakeCase::to_snake_case(arguments[0].as_str());
CommandResult::Continue(Some(value))
}
}
}

View file

@ -9,14 +9,14 @@ fn common_functions() {
#[test]
fn run_no_args() {
test::run_script_and_error(vec![create("")], "out = snake_case", "out");
test::run_script_and_error(vec![create("")], "out = snakecase", "out");
}
#[test]
fn run_single_argument() {
test::run_script_and_validate(
vec![create("")],
r#"out = snake_case "Hello, World!""#,
r#"out = snakecase "Hello, World!""#,
CommandValidation::Match("out".to_string(), "hello_world".to_string()),
);
}

View file

@ -18,3 +18,4 @@ The converted string.
string = uppercase "Hello World"
assert_eq ${string} "HELLO WORLD"
```

View file

@ -31,8 +31,8 @@ impl Command for CommandImpl {
if arguments.is_empty() {
CommandResult::Error("No arguments provided.".to_string())
} else {
let uppercase = arguments[0].to_uppercase();
CommandResult::Continue(Some(uppercase))
let value = arguments[0].to_uppercase();
CommandResult::Continue(Some(value))
}
}
}

View file

@ -1,12 +1,12 @@
fn test_without_symbols
output = camel_case "hello world 22"
output = camelcase "Hello world 22"
assert_eq ${output} "HelloWorld22"
end
fn test_with_symbols
output = camel_case "hello!@#$% world^&*()[]{}22"
output = camelcase "Hello!@#$% world^&*()[]{}22"
assert_eq ${output} "HelloWorld22"
end

View file

@ -0,0 +1,12 @@
fn test_without_symbols
output = kebabcase "Hello World 22"
assert_eq ${output} "hello-world-22"
end
fn test_with_symbols
output = kebabcase "hello!@#$% world^&*()[]{}22"
assert_eq ${output} "hello-world-22"
end

View file

@ -1,12 +1,12 @@
fn test_without_symbols
output = snake_case "hello world 22"
output = snakecase "Hello world 22"
assert_eq ${output} "hello_world_22"
end
fn test_with_symbols
output = snake_case "hello!@#$% world^&*()[]{}22"
output = snakecase "Hello!@#$% world^&*()[]{}22"
assert_eq ${output} "hello_world_22"
end