Ramda:如何递归地删除具有空值、空数组和空列表的对象中的键?

Ramda: How to remove keys in objects with null values, empty arrays and empty lists recursively?

类似于,但我正在寻找可以递归工作的东西。这样我就可以解决 AJV 和 JSON 架构的“功能”,其中 null !== undefined.

我从这个开始...这是为了删除空值但不能递归工作

import R from 'ramda';
describe('filter null values', () => {
  it('should filter out null values', () => {
    const specimen = {
      tasks: [
        { id: 'foo', blank: '', zero: 0, nool: null },
        { nool: null },
        { id: '', blank: null, zero: 0, nool: null },
      ],
      useless: { nool: null },
      uselessArray: [{ nool: null }],
      nool: null,
    };
    const expectation = {
      tasks: [
        { id: 'foo', blank: '', zero: 0 },
        { id: '', zero: 0 },
      ],
    };
    const removeNulls = R.reject(R.equals(null));
    expect(removeNulls(specimen)).toEqual(expectation);
  });
});

映射传递的项目。如果该值是对象(或数组),则递归调用当前值的 removeNulls。映射值后,拒绝所有 undefinednull 或空的非字符串值(参见 R.isEmpty)。

const { pipe, map, when, is, reject, ifElse, F, either, isEmpty, isNil } = R;

const removeNulls = pipe(
  map(when(is(Object), v => removeNulls(v))),
  reject(ifElse(is(String), F, either(isEmpty, isNil))),
);

const specimen = {"tasks":[{"id":"foo","blank":"","zero":0,"nool":null},{"nool":null},{"id":"","blank":null,"zero":0,"nool":null}],"useless":{"nool":null},"uselessArray":[{"nool":null}],"nool":null};

const result = removeNulls(specimen);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js" integrity="sha512-rZHvUXcc1zWKsxm7rJ8lVQuIr1oOmm7cShlvpV0gWf0RvbcJN6x96al/Rp2L2BI4a4ZkT2/YfVe/8YvB2UHzQw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>