feat(data-structures): add is_empty getter to map, string and blob.

This commit is contained in:
ltabis 2022-08-24 09:54:41 +02:00
parent 9c3443538a
commit 4b3608cedc
6 changed files with 41 additions and 3 deletions

View File

@ -3047,6 +3047,9 @@ fn keys(map: Map) -> Array;
/// Number of elements in the array. /// Number of elements in the array.
fn len(array: Array) -> int; fn len(array: Array) -> int;
/// Return true if the array is empty.
fn is_empty(array: Array) -> bool;
/// Return the length of the BLOB. /// Return the length of the BLOB.
/// ///
/// # Example /// # Example
@ -3060,9 +3063,15 @@ fn len(array: Array) -> int;
/// ``` /// ```
fn len(blob: Blob) -> int; fn len(blob: Blob) -> int;
/// Return true if the blob is empty.
fn is_empty(blob: Blob) -> bool;
/// Return the number of properties in the object map. /// Return the number of properties in the object map.
fn len(map: Map) -> int; fn len(map: Map) -> int;
/// Return true if the map is empty.
fn is_empty(map: Map) -> bool;
/// Return the length of the string, in number of characters. /// Return the length of the string, in number of characters.
/// ///
/// # Example /// # Example
@ -3074,6 +3083,9 @@ fn len(map: Map) -> int;
/// ``` /// ```
fn len(string: String) -> int; fn len(string: String) -> int;
/// Return true if the string is empty.
fn is_empty(string: String) -> bool;
/// Return the natural log of the decimal number. /// Return the natural log of the decimal number.
fn ln(x: Decimal) -> Decimal; fn ln(x: Decimal) -> Decimal;

View File

@ -2532,6 +2532,9 @@ fn keys(map: Map) -> Array;
/// Number of elements in the array. /// Number of elements in the array.
fn len(array: Array) -> int; fn len(array: Array) -> int;
/// Return true if the array is empty.
fn is_empty(array: Array) -> bool;
/// Return the length of the BLOB. /// Return the length of the BLOB.
/// ///
/// # Example /// # Example
@ -2545,9 +2548,15 @@ fn len(array: Array) -> int;
/// ``` /// ```
fn len(blob: Blob) -> int; fn len(blob: Blob) -> int;
/// Return true if the blob is empty.
fn is_empty(blob: Blob) -> bool;
/// Return the number of properties in the object map. /// Return the number of properties in the object map.
fn len(map: Map) -> int; fn len(map: Map) -> int;
/// Return true if the map is empty.
fn is_empty(map: Map) -> bool;
/// Return the length of the string, in number of characters. /// Return the length of the string, in number of characters.
/// ///
/// # Example /// # Example
@ -2559,6 +2568,9 @@ fn len(map: Map) -> int;
/// ``` /// ```
fn len(string: String) -> int; fn len(string: String) -> int;
/// Return true if the string is empty.
fn is_empty(string: String) -> bool;
/// Return the natural log of the decimal number. /// Return the natural log of the decimal number.
fn ln(x: Decimal) -> Decimal; fn ln(x: Decimal) -> Decimal;

View File

@ -31,8 +31,8 @@ pub mod array_functions {
array.len() as INT array.len() as INT
} }
/// Return true if the array is empty. /// Return true if the array is empty.
#[rhai_fn(name = "empty", get = "empty", pure)] #[rhai_fn(name = "is_empty", get = "is_empty", pure)]
pub fn empty(array: &mut Array) -> bool { pub fn is_empty(array: &mut Array) -> bool {
array.len() == 0 array.len() == 0
} }
/// Get a copy of the element at the `index` position in the array. /// Get a copy of the element at the `index` position in the array.

View File

@ -140,6 +140,11 @@ pub mod blob_functions {
pub fn len(blob: &mut Blob) -> INT { pub fn len(blob: &mut Blob) -> INT {
blob.len() as INT blob.len() as INT
} }
/// Return true if the blob is empty.
#[rhai_fn(name = "is_empty", get = "is_empty", pure)]
pub fn is_empty(blob: &mut Blob) -> bool {
blob.len() == 0
}
/// Get the byte value at the `index` position in the BLOB. /// Get the byte value at the `index` position in the BLOB.
/// ///
/// * If `index` < 0, position counts from the end of the BLOB (`-1` is the last element). /// * If `index` < 0, position counts from the end of the BLOB (`-1` is the last element).

View File

@ -25,7 +25,11 @@ mod map_functions {
pub fn len(map: &mut Map) -> INT { pub fn len(map: &mut Map) -> INT {
map.len() as INT map.len() as INT
} }
/// Return true if the map is empty.
#[rhai_fn(name = "is_empty", get = "is_empty", pure)]
pub fn is_empty(map: &mut Map) -> bool {
map.len() == 0
}
/// Get the value of the `property` in the object map and return a copy. /// Get the value of the `property` in the object map and return a copy.
/// ///
/// If `property` does not exist in the object map, `()` is returned. /// If `property` does not exist in the object map, `()` is returned.

View File

@ -163,6 +163,11 @@ mod string_functions {
string.chars().count() as INT string.chars().count() as INT
} }
} }
/// Return true if the string is empty.
#[rhai_fn(name = "is_empty", get = "is_empty")]
pub fn is_empty(string: &str) -> bool {
string.len() == 0
}
/// Return the length of the string, in number of bytes used to store it in UTF-8 encoding. /// Return the length of the string, in number of bytes used to store it in UTF-8 encoding.
/// ///
/// # Example /// # Example