mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-06 08:02:06 +00:00
New replace command.
This commit is contained in:
parent
9528e59696
commit
274493319c
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.1.8
|
||||
|
||||
* New replace command.
|
||||
* New current_time command.
|
||||
* New greater_than and less_than commands.
|
||||
* New wget (http_client) command #20
|
||||
|
|
32
docs/sdk.md
32
docs/sdk.md
|
@ -59,6 +59,7 @@
|
|||
* [std::string::IsEmpty (is_empty)](#std__string__IsEmpty)
|
||||
* [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::StartsWith (starts_with)](#std__string__StartsWith)
|
||||
* [std::string::SubString (substring)](#std__string__SubString)
|
||||
* [std::string::Trim (trim)](#std__string__Trim)
|
||||
|
@ -2093,6 +2094,37 @@ len = length "Hello World"
|
|||
#### Aliases:
|
||||
length, strlen
|
||||
|
||||
<a name="std__string__Replace"></a>
|
||||
## std::string::Replace
|
||||
```sh
|
||||
var = replace text from to
|
||||
```
|
||||
|
||||
Returns new value of text after replacing all from values to the provided to values.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The full text
|
||||
* The from text
|
||||
* The to text
|
||||
|
||||
#### Return Value
|
||||
|
||||
The updated text.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
text = set "my large text value with lots of text"
|
||||
updated = replace ${text} text stuff
|
||||
|
||||
assert_eq ${updated} "my large stuff value with lots of stuff"
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
replace
|
||||
|
||||
<a name="std__string__StartsWith"></a>
|
||||
## std::string::StartsWith
|
||||
```sh
|
||||
|
|
|
@ -5,6 +5,7 @@ mod indexof;
|
|||
mod is_empty;
|
||||
mod last_indexof;
|
||||
mod length;
|
||||
mod replace;
|
||||
mod starts_with;
|
||||
mod substring;
|
||||
mod trim;
|
||||
|
@ -27,6 +28,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(is_empty::create(&package))?;
|
||||
commands.set(last_indexof::create(&package))?;
|
||||
commands.set(length::create(&package))?;
|
||||
commands.set(replace::create(&package))?;
|
||||
commands.set(starts_with::create(&package))?;
|
||||
commands.set(substring::create(&package))?;
|
||||
commands.set(trim::create(&package))?;
|
||||
|
|
24
duckscript_sdk/src/sdk/std/string/replace/help.md
Normal file
24
duckscript_sdk/src/sdk/std/string/replace/help.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
```sh
|
||||
var = replace text from to
|
||||
```
|
||||
|
||||
Returns new value of text after replacing all from values to the provided to values.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The full text
|
||||
* The from text
|
||||
* The to text
|
||||
|
||||
#### Return Value
|
||||
|
||||
The updated text.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
text = set "my large text value with lots of text"
|
||||
updated = replace ${text} text stuff
|
||||
|
||||
assert_eq ${updated} "my large stuff value with lots of stuff"
|
||||
```
|
40
duckscript_sdk/src/sdk/std/string/replace/mod.rs
Executable file
40
duckscript_sdk/src/sdk/std/string/replace/mod.rs
Executable file
|
@ -0,0 +1,40 @@
|
|||
use crate::utils::pckg;
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "Replace")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["replace".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
if arguments.len() < 3 {
|
||||
CommandResult::Error("Three arguments are required.".to_string())
|
||||
} else {
|
||||
let result = arguments[0].replace(&arguments[1], &arguments[2]);
|
||||
|
||||
CommandResult::Continue(Some(result.to_string()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
41
duckscript_sdk/src/sdk/std/string/replace/mod_test.rs
Normal file
41
duckscript_sdk/src/sdk/std/string/replace/mod_test.rs
Normal file
|
@ -0,0 +1,41 @@
|
|||
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 = replace", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_single_argument() {
|
||||
test::run_script_and_error(vec![create("")], "out = replace true", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_two_arguments() {
|
||||
test::run_script_and_error(vec![create("")], "out = replace true true", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_found() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = replace bigtextbigtext ig aa",
|
||||
CommandValidation::Match("out".to_string(), "baatextbaatext".to_string()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_not_found() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = replace text ig aa",
|
||||
CommandValidation::Match("out".to_string(), "text".to_string()),
|
||||
);
|
||||
}
|
14
test/std/string/replace_test.ds
Normal file
14
test/std/string/replace_test.ds
Normal file
|
@ -0,0 +1,14 @@
|
|||
|
||||
function test_found
|
||||
text = set "my large text value with lots of text"
|
||||
updated = replace ${text} text stuff
|
||||
|
||||
assert_eq ${updated} "my large stuff value with lots of stuff"
|
||||
end
|
||||
|
||||
function test_not_found
|
||||
text = set "my large text value with lots of text"
|
||||
updated = replace ${text} stuff other
|
||||
|
||||
assert_eq ${updated} "my large text value with lots of text"
|
||||
end
|
Loading…
Reference in a new issue