mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New array_contains_value command
This commit is contained in:
parent
c0850401ef
commit
51fb735911
|
@ -3,6 +3,7 @@
|
|||
### v0.3.4
|
||||
|
||||
* \[Breaking Change\] Runtime - REPL mode doesn't stop due to crashes from user commands #103
|
||||
* New array_contains_value command.
|
||||
* New map_contains_value command.
|
||||
* New map_contains_key command.
|
||||
* New get_all_var_names command #100
|
||||
|
|
55
docs/sdk.md
55
docs/sdk.md
|
@ -8,6 +8,7 @@
|
|||
* [std::ShowCommandDocumentation (man)](#std__ShowCommandDocumentation)
|
||||
* [std::collections::Array (array)](#std__collections__Array)
|
||||
* [std::collections::ArrayConcat (array_concat)](#std__collections__ArrayConcat)
|
||||
* [std::collections::ArrayContainsValue (array_contains_value)](#std__collections__ArrayContainsValue)
|
||||
* [std::collections::ArrayGet (array_get)](#std__collections__ArrayGet)
|
||||
* [std::collections::ArrayIsEmpty (array_is_empty)](#std__collections__ArrayIsEmpty)
|
||||
* [std::collections::ArrayJoin (array_join)](#std__collections__ArrayJoin)
|
||||
|
@ -480,6 +481,60 @@ set ${scope::array_concat::array}
|
|||
#### Aliases:
|
||||
array_concat
|
||||
|
||||
<a name="std__collections__ArrayContainsValue"></a>
|
||||
## std::collections::ArrayContainsValue
|
||||
|
||||
```sh
|
||||
var = array_contains_value handle value
|
||||
```
|
||||
|
||||
Returns the first index of the array with the same value as provided.<br>
|
||||
If not found, false will be returned.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The array handle.
|
||||
* The value
|
||||
|
||||
#### Return Value
|
||||
|
||||
The value index in the array or false if not found.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = array value1 value2 value3
|
||||
index = array_contains_value ${handle} value2
|
||||
```
|
||||
|
||||
|
||||
#### Source:
|
||||
|
||||
```sh
|
||||
|
||||
scope::array_contains_value::index = set false
|
||||
scope::array_contains_value::value = set ${scope::array_contains_value::argument::2}
|
||||
|
||||
scope::array_contains_value::counter = set 0
|
||||
for scope::array_contains_value::next_value in ${scope::array_contains_value::argument::1}
|
||||
scope::array_contains_value::found = equals ${scope::array_contains_value::next_value} ${scope::array_contains_value::value}
|
||||
|
||||
if ${scope::array_contains_value::found}
|
||||
scope::array_contains_value::index = set ${scope::array_contains_value::counter}
|
||||
scope::array_contains_value::argument::1 = set
|
||||
end
|
||||
|
||||
scope::array_contains_value::counter = calc ${scope:array_contains_value::counter} + 1
|
||||
end
|
||||
|
||||
set ${scope::array_contains_value::index}
|
||||
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
array_contains_value
|
||||
|
||||
<a name="std__collections__ArrayGet"></a>
|
||||
## std::collections::ArrayGet
|
||||
```sh
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
```sh
|
||||
var = array_contains_value handle value
|
||||
```
|
||||
|
||||
Returns the first index of the array with the same value as provided.<br>
|
||||
If not found, false will be returned.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The array handle.
|
||||
* The value
|
||||
|
||||
#### Return Value
|
||||
|
||||
The value index in the array or false if not found.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = array value1 value2 value3
|
||||
index = array_contains_value ${handle} value2
|
||||
```
|
22
duckscript_sdk/src/sdk/std/collections/array_contains_value/mod.rs
Executable file
22
duckscript_sdk/src/sdk/std/collections/array_contains_value/mod.rs
Executable file
|
@ -0,0 +1,22 @@
|
|||
use crate::types::command::create_alias_command;
|
||||
use crate::utils::pckg;
|
||||
use duckscript::types::command::Command;
|
||||
use duckscript::types::error::ScriptError;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
pub(crate) fn create(package: &str) -> Result<Box<dyn Command>, ScriptError> {
|
||||
let name = pckg::concat(package, "ArrayContainsValue");
|
||||
let command = create_alias_command(
|
||||
name,
|
||||
vec!["array_contains_value".to_string()],
|
||||
include_str!("help.md").to_string(),
|
||||
"array_contains_value".to_string(),
|
||||
include_str!("script.ds").to_string(),
|
||||
2,
|
||||
)?;
|
||||
|
||||
Ok(Box::new(command))
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create("").unwrap());
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
scope::array_contains_value::index = set false
|
||||
scope::array_contains_value::value = set ${scope::array_contains_value::argument::2}
|
||||
|
||||
scope::array_contains_value::counter = set 0
|
||||
for scope::array_contains_value::next_value in ${scope::array_contains_value::argument::1}
|
||||
scope::array_contains_value::found = equals ${scope::array_contains_value::next_value} ${scope::array_contains_value::value}
|
||||
|
||||
if ${scope::array_contains_value::found}
|
||||
scope::array_contains_value::index = set ${scope::array_contains_value::counter}
|
||||
scope::array_contains_value::argument::1 = set
|
||||
end
|
||||
|
||||
scope::array_contains_value::counter = calc ${scope:array_contains_value::counter} + 1
|
||||
end
|
||||
|
||||
set ${scope::array_contains_value::index}
|
|
@ -1,5 +1,6 @@
|
|||
pub(crate) mod array;
|
||||
mod array_concat;
|
||||
mod array_contains_value;
|
||||
mod array_get;
|
||||
mod array_is_empty;
|
||||
mod array_join;
|
||||
|
@ -36,6 +37,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
|
||||
commands.set(array::create(&package))?;
|
||||
commands.set(array_concat::create(&package)?)?;
|
||||
commands.set(array_contains_value::create(&package)?)?;
|
||||
commands.set(array_get::create(&package))?;
|
||||
commands.set(array_push::create(&package))?;
|
||||
commands.set(array_set::create(&package))?;
|
||||
|
|
48
test/std/collections/array_contains_value_test.ds
Normal file
48
test/std/collections/array_contains_value_test.ds
Normal file
|
@ -0,0 +1,48 @@
|
|||
|
||||
fn test_empty
|
||||
handle = array
|
||||
|
||||
found = array_contains_value ${handle} value
|
||||
release ${handle}
|
||||
|
||||
assert_eq ${found} false
|
||||
end
|
||||
|
||||
fn test_not_found
|
||||
handle = array value1
|
||||
|
||||
found = array_contains_value ${handle} value2
|
||||
release ${handle}
|
||||
|
||||
assert_eq ${found} false
|
||||
end
|
||||
|
||||
fn test_found
|
||||
handle = array value
|
||||
|
||||
found = array_contains_value ${handle} value
|
||||
release ${handle}
|
||||
|
||||
assert_eq ${found} 0
|
||||
end
|
||||
|
||||
fn test_both
|
||||
handle = array value1 value2 value1 value1 value2 value2 value3
|
||||
size = array_length ${handle}
|
||||
last_index = calc ${size} - 1
|
||||
|
||||
found = array_contains_value ${handle} value1
|
||||
assert_eq ${found} 0
|
||||
|
||||
found = array_contains_value ${handle} value2
|
||||
assert_eq ${found} 1
|
||||
|
||||
found = array_contains_value ${handle} value3
|
||||
assert ${found} ${last_index}
|
||||
|
||||
found = array_contains_value ${handle} value4
|
||||
assert_eq ${found} false
|
||||
|
||||
release ${handle}
|
||||
end
|
||||
|
|
@ -18,16 +18,6 @@ fn test_not_found
|
|||
assert_false ${found}
|
||||
end
|
||||
|
||||
fn test_not_found
|
||||
handle = map
|
||||
map_put ${handle} key1 value
|
||||
|
||||
found = map_contains_key ${handle} key2
|
||||
release ${handle}
|
||||
|
||||
assert_false ${found}
|
||||
end
|
||||
|
||||
fn test_found
|
||||
handle = map
|
||||
map_put ${handle} key value
|
||||
|
|
|
@ -8,16 +8,6 @@ fn test_empty
|
|||
assert_false ${found}
|
||||
end
|
||||
|
||||
fn test_not_found
|
||||
handle = map
|
||||
map_put ${handle} key1 value
|
||||
|
||||
found = map_contains_value ${handle} key2
|
||||
release ${handle}
|
||||
|
||||
assert_false ${found}
|
||||
end
|
||||
|
||||
fn test_not_found
|
||||
handle = map
|
||||
map_put ${handle} key1 value1
|
||||
|
|
Loading…
Reference in a new issue