mirror of
https://github.com/sagiegurari/duckscript
synced 2024-07-20 17:55:45 +00:00
New set_from_array command
This commit is contained in:
parent
f519a20074
commit
9e6b35ce07
|
@ -2,6 +2,7 @@
|
|||
|
||||
### v0.3.4
|
||||
|
||||
* New set_from_array command.
|
||||
* New set_to_array command.
|
||||
* New set_contains command.
|
||||
* New set_remove command.
|
||||
|
|
47
docs/sdk.md
47
docs/sdk.md
|
@ -38,6 +38,7 @@
|
|||
* [std::collections::Set (set_new)](#std__collections__Set)
|
||||
* [std::collections::SetClear (set_clear)](#std__collections__SetClear)
|
||||
* [std::collections::SetContains (set_contains)](#std__collections__SetContains)
|
||||
* [std::collections::SetFromArray (set_from_array)](#std__collections__SetFromArray)
|
||||
* [std::collections::SetIsEmpty (set_is_empty)](#std__collections__SetIsEmpty)
|
||||
* [std::collections::SetPut (set_put, set_add)](#std__collections__SetPut)
|
||||
* [std::collections::SetRemove (set_remove)](#std__collections__SetRemove)
|
||||
|
@ -1622,6 +1623,52 @@ found = set_contains ${handle} value2
|
|||
#### Aliases:
|
||||
set_contains
|
||||
|
||||
<a name="std__collections__SetFromArray"></a>
|
||||
## std::collections::SetFromArray
|
||||
|
||||
```sh
|
||||
set_handle = set_from_array array_handle
|
||||
```
|
||||
|
||||
Returns a set handle created from the provided array values.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The array handle.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The new set handle.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
array_handle = array value1 value2 value3
|
||||
set_handle = set_from_array ${handle}
|
||||
```
|
||||
|
||||
|
||||
#### Source:
|
||||
|
||||
```sh
|
||||
|
||||
if not is_array ${scope::set_from_array::argument::1}
|
||||
trigger_error "Invalid input, non array handle or array not found."
|
||||
end
|
||||
|
||||
scope::set_from_array::set = set_new
|
||||
for scope::set_from_array::next_value in ${scope::set_from_array::argument::1}
|
||||
set_put ${scope::set_from_array::set} ${scope::set_from_array::next_value}
|
||||
end
|
||||
|
||||
set ${scope::set_from_array::set}
|
||||
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
set_from_array
|
||||
|
||||
<a name="std__collections__SetIsEmpty"></a>
|
||||
## std::collections::SetIsEmpty
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ mod read_properties;
|
|||
mod set;
|
||||
mod set_clear;
|
||||
mod set_contains;
|
||||
mod set_from_array;
|
||||
mod set_is_empty;
|
||||
mod set_put;
|
||||
mod set_remove;
|
||||
|
@ -78,6 +79,7 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(set::create(&package))?;
|
||||
commands.set(set_clear::create(&package))?;
|
||||
commands.set(set_contains::create(&package))?;
|
||||
commands.set(set_from_array::create(&package)?)?;
|
||||
commands.set(set_is_empty::create(&package)?)?;
|
||||
commands.set(set_put::create(&package))?;
|
||||
commands.set(set_remove::create(&package))?;
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
```sh
|
||||
set_handle = set_from_array array_handle
|
||||
```
|
||||
|
||||
Returns a set handle created from the provided array values.
|
||||
|
||||
#### Parameters
|
||||
|
||||
The array handle.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The new set handle.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
array_handle = array value1 value2 value3
|
||||
set_handle = set_from_array ${handle}
|
||||
```
|
22
duckscript_sdk/src/sdk/std/collections/set_from_array/mod.rs
Executable file
22
duckscript_sdk/src/sdk/std/collections/set_from_array/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, "SetFromArray");
|
||||
let command = create_alias_command(
|
||||
name,
|
||||
vec!["set_from_array".to_string()],
|
||||
include_str!("help.md").to_string(),
|
||||
"set_from_array".to_string(),
|
||||
include_str!("script.ds").to_string(),
|
||||
1,
|
||||
)?;
|
||||
|
||||
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,11 @@
|
|||
|
||||
if not is_array ${scope::set_from_array::argument::1}
|
||||
trigger_error "Invalid input, non array handle or array not found."
|
||||
end
|
||||
|
||||
scope::set_from_array::set = set_new
|
||||
for scope::set_from_array::next_value in ${scope::set_from_array::argument::1}
|
||||
set_put ${scope::set_from_array::set} ${scope::set_from_array::next_value}
|
||||
end
|
||||
|
||||
set ${scope::set_from_array::set}
|
21
test/std/collections/set_from_array_test.ds
Normal file
21
test/std/collections/set_from_array_test.ds
Normal file
|
@ -0,0 +1,21 @@
|
|||
|
||||
fn test_valid
|
||||
arr = array 1 2 3 1
|
||||
|
||||
handle = set_from_array ${arr}
|
||||
size = set_size ${handle}
|
||||
assert_eq ${size} 3
|
||||
|
||||
result = set_contains ${handle} 1
|
||||
contained = not equals ${result} false
|
||||
assert ${contained}
|
||||
result = set_contains ${handle} 2
|
||||
contained = not equals ${result} false
|
||||
assert ${contained}
|
||||
result = set_contains ${handle} 3
|
||||
contained = not equals ${result} false
|
||||
assert ${contained}
|
||||
|
||||
release ${handle}
|
||||
release ${arr}
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
|
||||
fn test_map_keys
|
||||
fn test_valid
|
||||
handle = set_new 1 2 3 4
|
||||
|
||||
arr = set_to_array ${handle}
|
||||
|
|
Loading…
Reference in a new issue