返回不包含 Ramda 中另一个对象的任何键或值的对象

问题描述 投票:0回答:0

ramda 的新手,构建一个mediator 类我有一个描述已注册(授权)频道/消息的对象。这些渠道在键和值方面都是独一无二的。在注册时,他们是这样传递的:

enum MyMessages {
 FirstMessage = 'first:message',
 SecondMessage = 'second:message'
};

mediator.addChannels(MyMessages); // With a validator to keep them unique.

要删除频道,我调用

mediator.removeChannels(MyMessages);
。 底层的命令式实现效果很好:

// Makes a shallow clone of the existing channels.
let result = { ...existingChannels };
// Iterates the channels to remove.
for (const [key, value] of Object.entries(channelsToRemove)) {
  // Keys match.
  if (Object.keys(result).includes(key)) {
    delete result[key];
  }
  // Values match.
  if (Object.values(result).includes(value)) {
    // Finds the key for the given value.
    const k = Object.keys(result).find((a: string) => result[a] === value);
    if (k) {
      delete result[k];
    }
  }
  // Channels to remove value matches an existing key.
  if (Object.keys(result).includes(value as string)) {
    delete result[value as string];
  }
  // Channels to remove key 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;

有点天真,可以重构,但最后,我得到了我的channels对象,没有删除的键/值。

我想用

ramda
功能替换它。

我可以用类似的东西得到重叠的键

R.without(R.keys(channelsToRemove), R.keys(existingChannels))

但我无法理解如何轻松获得最终对象(例如,没有第二个对象的键或值)。

const obj1 = {
  foo: 'bar',
  baz: 'sum'
}

const obj2 = {
  baz: 'hop'
}

const obj3 = {
  sum: 'ack'
}

因此,我希望这样的事情发生:

  • obj1 - obj2
    应该回来
    { foo: 'bar' }
  • obj1 - obj3
    应该回来
    { foo: 'bar' }
  • obj2 - obj3
    应该回来
    { baz: 'hop' }
typescript object functional-programming ramda.js mediator
© www.soinside.com 2019 - 2024. All rights reserved.