功能可以检查空嵌套项目(包括数组,集合,字符串& Maps)
我试图在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用这样的递归函数:
value
函数,请检查传递的值是否为对象array.from(o.values)获取对象的值 的值
set
值是字符串,然后检查它是否是真实的vaue(您可以根据需要自定义此部分),这是可运行的摘要:
You can use a recursive function like this:
values
function, then get the values of the object usingArray.from(o.values())
(Gets the values ofSet
,Map
andArray
objects)Here's a runnable snippet: