功能可以检查空嵌套项目(包括数组,集合,字符串& Maps)

发布于 2025-01-20 00:39:25 字数 2272 浏览 2 评论 0原文

我试图在 vanilla JS 中提出一个解决方案(不使用任何第三方库)来检查给定的输入并确定它是否为“空”。

我有以下代码&我想要匹配的断言。 每个测试用例都有一个预期答案作为注释。

我已经尝试了几个函数来“深度检查” stackblitz 上的这些断言,但没有一个函数能够接近完全覆盖。

https://stackblitz.com/edit/node-ksxnjm

const assert = require('assert');



function isEmpty(obj) {
  return Object.keys(obj).every((k) => !Object.keys(obj[k]).length);
}

const test1 = {};                  // expect true
const test2 = { some: 'value' };   // expect false
const test3 = { some: {} };        // expect true
const test4 = [];                  // expect true
const test5 = [[]];                // expect true
const test6 = { some: [] };        // expect true
const test7 = { some: ['barry'] }; // expect false
const test8 = { some: new Map() }; // expect true
const test9 = {
  response: new Map([['body', new Map([['something', {}]])]]),
};                                 // expect true
const test10 = {
  response: '{"body":{"something":{}}}',
};                                 // expect true

const test11 = {
  something: { somethingElse: {} },
};                                 // expect true

assert.strictEqual(isEmpty(test1), true);
assert.strictEqual(isEmpty(test2), false);
assert.strictEqual(isEmpty(test3), true);
assert.strictEqual(isEmpty(test4), true);
assert.strictEqual(isEmpty(test5), true);
assert.strictEqual(isEmpty(test6), true);
assert.strictEqual(isEmpty(test7), false);
assert.strictEqual(isEmpty(test8), true);
assert.strictEqual(isEmpty(test9), true);
assert.strictEqual(isEmpty(test10), true);
assert.strictEqual(isEmpty(test11), true);

我创建的函数适用于大多数测试用例,但不适用于所有测试用例。 我正在努力覆盖的是嵌套对象和结构化对象。我对如何继续感到有点困惑。

我如何检查后面这些测试用例?

编辑:

const test12 = {
  something: {
    somethingElse: {
      number: 1,
      someSet: new Set(['garry']),
    },
  },
}; // should evaluate to false

const test13 = new Map([
    ['something', new Map([
        ['somethingElse', new Map([
            ['number', 1],
            ['someSet', new Set(['garry'])]
        ])]
    ])]
]); // should also evaluate to false

I am trying to come up with a solution in vanilla JS (without using any third party libraries) that checks a given input and determines whether it is 'empty' or not.

I have the following code & assertions that I'd like to match against.
Each test case has an expected answer as a comment.

I have tried several functions to 'deepCheck' these assertions on stackblitz but none have come close to getting full coverage.

https://stackblitz.com/edit/node-ksxnjm

const assert = require('assert');



function isEmpty(obj) {
  return Object.keys(obj).every((k) => !Object.keys(obj[k]).length);
}

const test1 = {};                  // expect true
const test2 = { some: 'value' };   // expect false
const test3 = { some: {} };        // expect true
const test4 = [];                  // expect true
const test5 = [[]];                // expect true
const test6 = { some: [] };        // expect true
const test7 = { some: ['barry'] }; // expect false
const test8 = { some: new Map() }; // expect true
const test9 = {
  response: new Map([['body', new Map([['something', {}]])]]),
};                                 // expect true
const test10 = {
  response: '{"body":{"something":{}}}',
};                                 // expect true

const test11 = {
  something: { somethingElse: {} },
};                                 // expect true

assert.strictEqual(isEmpty(test1), true);
assert.strictEqual(isEmpty(test2), false);
assert.strictEqual(isEmpty(test3), true);
assert.strictEqual(isEmpty(test4), true);
assert.strictEqual(isEmpty(test5), true);
assert.strictEqual(isEmpty(test6), true);
assert.strictEqual(isEmpty(test7), false);
assert.strictEqual(isEmpty(test8), true);
assert.strictEqual(isEmpty(test9), true);
assert.strictEqual(isEmpty(test10), true);
assert.strictEqual(isEmpty(test11), true);

The function I created works for the majority of these test cases, but not for all.
The one's I'm struggling to cover are the nested objects and strignified objects. I'm a bit stumped as to how I can proceed.

How can I check for these latter test cases?

EDIT:

const test12 = {
  something: {
    somethingElse: {
      number: 1,
      someSet: new Set(['garry']),
    },
  },
}; // should evaluate to false

const test13 = new Map([
    ['something', new Map([
        ['somethingElse', new Map([
            ['number', 1],
            ['someSet', new Set(['garry'])]
        ])]
    ])]
]); // should also evaluate to false

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪也成诗 2025-01-27 00:39:25

您可以使用这样的递归函数:

  • 如果对象具有value函数,请检查传递的值是否为对象
  • ,然后使用array.from(o.values)获取对象的值 的值
  • ())
  • (获取set 值是字符串,然后检查它是否是真实的vaue(您可以根据需要自定义此部分),
function isEmpty(o) {
  if (typeof o === "object") {
    let values = typeof o.values === "function"
                  ? Array.from(o.values())
                  : Object.values(o)
    
    return values.every(isEmpty)
  } else {
    var parsed = parseJsonString(o)
    return parsed ? isEmpty(parsed) : !o
  }
};

function parseJsonString(str) {
    try {
        return typeof str === 'string' && JSON.parse(str);
    } catch (e) {
        return '';
    }
}

这是可运行的摘要:

function isEmpty(o) {
  if (typeof o === "object") {
    let values = typeof o.values === "function"
                  ? Array.from(o.values())
                  : Object.values(o)
    
    return values.every(isEmpty)
  } else {
    var parsed = parseJsonString(o)
    return parsed ? isEmpty(parsed) : !o
  }
};

function parseJsonString(str) {
    try {
        return typeof str === 'string' && JSON.parse(str);
    } catch (e) {
        return '';
    }
}

const test1 = {};                  // expect true
const test2 = { some: 'value' };   // expect false
const test3 = { some: {} };        // expect true
const test4 = [];                  // expect true
const test5 = [[]];                // expect true
const test6 = { some: [] };        // expect true
const test7 = { some: ['barry'] }; // expect false
const test8 = { some: new Map() }; // expect true
const test9 = {
  response: new Map([['body', new Map([['something', {}]])]]),
};                                 // expect true
const test10 = {
  response: '{"body":{"something":{}}}',
};                                 // expect true

const test11 = {
  something: { somethingElse: {} },
};                                // expect true

const test12 = {
  something: {
    somethingElse: {
      number: 1,
      someSet: new Set(['garry']),
    }
  }
};                                // expect false

const test13 = new Map([
    ['something', new Map([
        ['somethingElse', new Map([
            ['number', 1],
            ['someSet', new Set(['garry'])]
        ])]
    ])]
])                                // expect false

console.log(
  [test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13].map(isEmpty)
);

You can use a recursive function like this:

  • Check if the value passed is an object
  • If the object has a values function, then get the values of the object using Array.from(o.values()) (Gets the values of Set, Map and Array objects)
  • Recursively call the isEmpty on every value
  • If the input value is a string, then check if it is a truthy vaue (You can customize this part based on your need)
function isEmpty(o) {
  if (typeof o === "object") {
    let values = typeof o.values === "function"
                  ? Array.from(o.values())
                  : Object.values(o)
    
    return values.every(isEmpty)
  } else {
    var parsed = parseJsonString(o)
    return parsed ? isEmpty(parsed) : !o
  }
};

function parseJsonString(str) {
    try {
        return typeof str === 'string' && JSON.parse(str);
    } catch (e) {
        return '';
    }
}

Here's a runnable snippet:

function isEmpty(o) {
  if (typeof o === "object") {
    let values = typeof o.values === "function"
                  ? Array.from(o.values())
                  : Object.values(o)
    
    return values.every(isEmpty)
  } else {
    var parsed = parseJsonString(o)
    return parsed ? isEmpty(parsed) : !o
  }
};

function parseJsonString(str) {
    try {
        return typeof str === 'string' && JSON.parse(str);
    } catch (e) {
        return '';
    }
}

const test1 = {};                  // expect true
const test2 = { some: 'value' };   // expect false
const test3 = { some: {} };        // expect true
const test4 = [];                  // expect true
const test5 = [[]];                // expect true
const test6 = { some: [] };        // expect true
const test7 = { some: ['barry'] }; // expect false
const test8 = { some: new Map() }; // expect true
const test9 = {
  response: new Map([['body', new Map([['something', {}]])]]),
};                                 // expect true
const test10 = {
  response: '{"body":{"something":{}}}',
};                                 // expect true

const test11 = {
  something: { somethingElse: {} },
};                                // expect true

const test12 = {
  something: {
    somethingElse: {
      number: 1,
      someSet: new Set(['garry']),
    }
  }
};                                // expect false

const test13 = new Map([
    ['something', new Map([
        ['somethingElse', new Map([
            ['number', 1],
            ['someSet', new Set(['garry'])]
        ])]
    ])]
])                                // expect false

console.log(
  [test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13].map(isEmpty)
);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文