How can we retain the normalization logic when cloning a Map or Set?
As a proponent of immutable data structures, I consider cloning to be an essential function on data structures. For simple Map's we can just do new Map(map), but doing the same on a Map with normalization would make it lose the normalization logic. Has there been any consideration about how there might be a way to retain normalization when creating a copy? Perhaps a new method Map.prototype.clone()? Or a static method Map.from(map, options: { retainNormalization: boolean })?
Maps already aren’t cloneable - new Map(map) won’t work for Map subclasses. I think that’s an interesting problem to solve, but i think that’s something that should be investigated in a separate proposal.
@ljharb Fair enough. Although with a Map subclass one can do new MapSubclass(map) but I suppose, for normalization, in a similar vein would be to augment the factory function producing the said normalized Map to be able to accept an existing map as an argument. e.g.:
function createEmailToAccountMap(map = undefined) {
return new Map(map, {
coerceKey({email}) {
return email;
},
coerceValue(state) {
return state instanceof AccountState ?
state :
new AccountState(state);
}
});
}
Feel free to close the issue. Just a query that popped up in my head seeing this proposal. Thanks for the prompt reply!