зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1440116 - Add an `ObjectUtils.isEmpty` helper. r=markh
MozReview-Commit-ID: 1Y4XW3dKUPT --HG-- extra : rebase_source : 9a46656c876c8081e17f15427166f7b2ad9ad879
This commit is contained in:
Родитель
7d1da2c7e5
Коммит
f3b50c17fe
|
@ -52,6 +52,26 @@ var ObjectUtils = {
|
|||
*/
|
||||
strict(obj) {
|
||||
return _strict(obj);
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns `true` if `obj` is an array without elements, an object without
|
||||
* enumerable properties, or a falsy primitive; `false` otherwise.
|
||||
*/
|
||||
isEmpty(obj) {
|
||||
if (!obj) {
|
||||
return true;
|
||||
}
|
||||
if (typeof obj != "object") {
|
||||
return false;
|
||||
}
|
||||
if (Array.isArray(obj)) {
|
||||
return !obj.length;
|
||||
}
|
||||
for (let key in obj) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -107,3 +107,32 @@ add_task(async function test_deepEqual() {
|
|||
|
||||
Assert.ok(deepEqual(checkEquiv(e, f), checkEquiv(f, e)));
|
||||
});
|
||||
|
||||
add_task(async function test_isEmpty() {
|
||||
Assert.ok(ObjectUtils.isEmpty(""), "Empty strings should be empty");
|
||||
Assert.ok(ObjectUtils.isEmpty(0), "0 should be empty");
|
||||
Assert.ok(ObjectUtils.isEmpty(NaN), "NaN should be empty");
|
||||
Assert.ok(ObjectUtils.isEmpty(), "Undefined should be empty");
|
||||
Assert.ok(ObjectUtils.isEmpty(null), "Null should be empty");
|
||||
Assert.ok(ObjectUtils.isEmpty(false), "False should be empty");
|
||||
|
||||
Assert.ok(ObjectUtils.isEmpty({}),
|
||||
"Objects without properties should be empty");
|
||||
Assert.ok(ObjectUtils.isEmpty(Object.defineProperty({}, "a", {
|
||||
value: 1,
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: true
|
||||
})), "Objects without enumerable properties should be empty");
|
||||
Assert.ok(ObjectUtils.isEmpty([]), "Arrays without elements should be empty");
|
||||
|
||||
Assert.ok(!ObjectUtils.isEmpty([1]),
|
||||
"Arrays with elements should not be empty");
|
||||
Assert.ok(!ObjectUtils.isEmpty({ a: 1 }),
|
||||
"Objects with properties should not be empty");
|
||||
|
||||
function A() {}
|
||||
A.prototype.b = 2;
|
||||
Assert.ok(!ObjectUtils.isEmpty(new A()),
|
||||
"Objects with inherited enumerable properties should not be empty");
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче