Exploring ramda for the first time, I am creating a mediator class that involves an object detailing authorized channels and messages. These channels should be unique in both their keys and values. During registration, they are passed as follows:
enum MyMessages {
FirstMessage = 'first:message',
SecondMessage = 'second:message'
};
mediator.addChannels(MyMessages); // Ensuring uniqueness with a validator.
To delete channels, I use
mediator.removeChannels(MyMessages);
.
The current imperative implementation functions properly:
// Creates a shallow copy of the existing channels.
let result = { ...existingChannels };
// Iterate through channels to be removed.
for (const [key, value] of Object.entries(channelsToRemove)) {
// Matching keys.
if (Object.keys(result).includes(key)) {
delete result[key];
}
// Matching values.
if (Object.values(result).includes(value)) {
// Find the key corresponding to the value.
const k = Object.keys(result).find((a: string) => result[a] === value);
if (k) {
delete result[k];
}
}
// Value of channel to remove matches an existing key.
if (Object.keys(result).includes(value as string)) {
delete result[value as string];
}
// Key of channel to remove matches an existing value.
if (Object.values(result).includes(key)) {
const k = Object.keys(result).find((a: string) => result[a] === key);
if (k) {
delete result[k];
}
}
}
return result;
This code could benefit from refactoring, but ultimately achieves the goal of obtaining the updated channels object post deletion of keys / values.
I aim to replace this logic with ramda
functions.
I can identify overlapping keys using something like
R.without(R.keys(channelsToRemove), R.keys(existingChannels))
However, I am facing challenges in figuring out how to obtain the final object easily (i.e., without the keys or values of the second object).
const obj1 = {
foo: 'bar',
baz: 'sum'
}
const obj2 = {
baz: 'hop'
}
const obj3 = {
sum: 'ack'
}
The desired outcome is as follows:
obj1 - obj2
should yield{ foo: 'bar' }
: key overlaps key.obj1 - obj3
should give{ foo: 'bar' }
: key overlaps value.obj2 - obj3
should produce{ baz: 'hop' }
: unique keys and values.