Skip to content

Commit

Permalink
Refactor clone method in Cluster class to enhance data handling
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
Brian Joseph Petro committed Jan 22, 2025
1 parent 6f9cff9 commit 5143f5e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions smart-clusters/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,18 @@ export class Cluster extends CollectionItem {
* @returns {Promise<ClusterGroup>}
*/
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]];
Expand All @@ -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;
}
/**
Expand Down

0 comments on commit 5143f5e

Please sign in to comment.