Skip to content

Commit 11fdba2

Browse files
authored
feat: create global map for allowing empty hashmap construction (#329)
1 parent 4af18c0 commit 11fdba2

File tree

1 file changed

+19
-0
lines changed
  • crates/bevy_mod_scripting_functions/src

1 file changed

+19
-0
lines changed

crates/bevy_mod_scripting_functions/src/core.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,25 @@ impl GlobalNamespace {
656656
&mut allocator,
657657
))
658658
}
659+
660+
/// Constructs a hash map. Useful for languages which do not make the distinction between lists and dictionaries.
661+
///
662+
/// Arguments:
663+
/// * `map_or_list`: The list or map to convert to a hash map.
664+
/// Returns:
665+
/// * `hashMap`: The converted hash map
666+
fn map(
667+
map_or_list: Union<HashMap<String, ScriptValue>, Vec<ScriptValue>>,
668+
) -> HashMap<String, ScriptValue> {
669+
match map_or_list.into_left() {
670+
Ok(map) => map,
671+
Err(list) => list
672+
.into_iter()
673+
.enumerate()
674+
.map(|(k, v)| (k.to_string(), v))
675+
.collect(),
676+
}
677+
}
659678
}
660679

661680
pub fn register_core_functions(app: &mut App) {

0 commit comments

Comments
 (0)