From 5143f5e5f9958def11c47201e8c42ddf58189158 Mon Sep 17 00:00:00 2001 From: Brian Joseph Petro Date: Wed, 22 Jan 2025 11:13:44 -0500 Subject: [PATCH] Refactor clone method in Cluster class to enhance data handling - Updated the #clone method to create a new data object with nullified key and center properties, while preserving essential structure. - Improved handling of optional chaining for remove_centers and add_centers options, ensuring robustness in cluster management. - Streamlined the create_or_update call by passing the new data object directly, enhancing clarity and maintainability of the cluster creation process. --- smart-clusters/cluster.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/smart-clusters/cluster.js b/smart-clusters/cluster.js index f9c6464b..344e12d3 100644 --- a/smart-clusters/cluster.js +++ b/smart-clusters/cluster.js @@ -155,8 +155,18 @@ export class Cluster extends CollectionItem { * @returns {Promise} */ async #clone(opts = {}) { - const new_data = JSON.parse(JSON.stringify(this.data)); + const new_data = { + ...JSON.parse(JSON.stringify(this.data || {})), + // filters: {}, // keep filters + // members: {}, // keep members + // center: {}, // keep centers (will be recomputed) + key: null, + center_vec: null, + group_key: null + }; if(!new_data.center) new_data.center = {}; + opts.remove_centers = opts.remove_centers?.map(center => typeof center === 'string' ? center : center.key) || []; + opts.add_centers = opts.add_centers?.map(center => typeof center === 'string' ? center : center.key) || []; if(opts.remove_centers) { for(let i = 0; i < opts.remove_centers.length; i++) { delete new_data.center[opts.remove_centers[i]]; @@ -167,7 +177,7 @@ export class Cluster extends CollectionItem { new_data.center[opts.add_centers[i]] = { weight: 1 }; } } - const new_cluster = this.collection.create_or_update({ data: new_data }); + const new_cluster = this.collection.create_or_update(new_data); return new_cluster; } /**