@@ -323,6 +323,46 @@ namespace nfo {
323323 return ZipperOutput (arr);
324324 }
325325
326+ /* *
327+ * Get the indexed zipper output for the given components.
328+ *
329+ * @param comps An array of component types to zip.
330+ * @return A ZipperOutput containing the id of the entity and it's zipped components.
331+ * @throws std::runtime_error if the input is not an array.
332+ */
333+ ZipperOutput get_indexed_zipper (const ZipperInput &comps)
334+ {
335+ if (!comps.isArray ())
336+ throw std::runtime_error (" getIndexedZipper: need an array of comps as parameter" );
337+
338+ std::size_t max = SIZE_MAX ;
339+ std::map<std::string, SparseArray<emscripten::val> *> arrays;
340+ for (int i = 0 ; i < comps[" length" ].as <unsigned int >(); i++) {
341+ SparseArray<emscripten::val> &components = get_components (Component (comps[i]));
342+ arrays[get_js_class_name (comps[i])] = &components;
343+ max = (std::min)(components.size (), max);
344+ }
345+
346+ emscripten::val arr = emscripten::val::array ();
347+ std:size_t zipper_idx = 0 ;
348+ for (std::size_t idx = 0 ; idx < max; idx++) {
349+ emscripten::val obj = emscripten::val::object ();
350+ bool need_to_add = true ;
351+ for (const auto &[name, sparse_array] : arrays) {
352+ if (!(*sparse_array)[idx].has_value ()) {
353+ need_to_add = false ;
354+ break ;
355+ }
356+ obj.set (name, (*sparse_array)[idx].value ());
357+ }
358+ if (need_to_add) {
359+ obj.set (" id" , idx);
360+ arr.set (zipper_idx++, obj);
361+ }
362+ }
363+ return ZipperOutput (arr);
364+ }
365+
326366 private:
327367 std::unordered_map<std::string, std::any> _components_arrays;
328368
0 commit comments