From 2bb5d1e03316cb29151f0e478cad1606c1005de5 Mon Sep 17 00:00:00 2001 From: makspll Date: Wed, 26 Feb 2025 08:17:12 +0000 Subject: [PATCH] create global `map` for allowing empty hashmap construction --- .../bevy_mod_scripting_functions/src/core.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/crates/bevy_mod_scripting_functions/src/core.rs b/crates/bevy_mod_scripting_functions/src/core.rs index 2142c5b3d5..8f22b99bcb 100644 --- a/crates/bevy_mod_scripting_functions/src/core.rs +++ b/crates/bevy_mod_scripting_functions/src/core.rs @@ -656,6 +656,25 @@ impl GlobalNamespace { &mut allocator, )) } + + /// Constructs a hash map. Useful for languages which do not make the distinction between lists and dictionaries. + /// + /// Arguments: + /// * `map_or_list`: The list or map to convert to a hash map. + /// Returns: + /// * `hashMap`: The converted hash map + fn map( + map_or_list: Union, Vec>, + ) -> HashMap { + match map_or_list.into_left() { + Ok(map) => map, + Err(list) => list + .into_iter() + .enumerate() + .map(|(k, v)| (k.to_string(), v)) + .collect(), + } + } } pub fn register_core_functions(app: &mut App) {