Skip to content

Instantly share code, notes, and snippets.

@tivac
Forked from lsmith/isEmpty.js
Created June 28, 2010 19:20
Show Gist options
  • Save tivac/456239 to your computer and use it in GitHub Desktop.
Save tivac/456239 to your computer and use it in GitHub Desktop.
Y.isEmpty = function (o) {
if (o != null) {
if (Y.Node) {
o = (o._node || o._nodes || o);
}
if (o.nodeType) {
o = o.childNodes;
}
if (Y.Lang.isArray(o)) {
return !o.length;
}
if (Y.Lang.isObject(o)) {
if ('__count__' in o) {
return !o.__count__;
} else {
for (var k in o) {
// own properties are enumerated first
return !o.hasOwnProperty(k);
}
}
}
return (Y.Lang.isString(o)) ? !o.length : false;
}
return true;
};
new Y.Test.Case({
name : "Y.isEmpty Tests",
"boolean should be false" : function() {
Y.Assert.isFalse(Y.isEmpty(false));
},
"empty string should be true" : function() {
Y.Assert.isTrue(Y.isEmpty(""));
},
"string should be false" : function() {
Y.Assert.isFalse(Y.isEmpty("hi"));
},
"object with no properties should be true" : function() {
Y.Assert.isTrue(Y.isEmpty({ }));
},
"object with properties should be false" : function() {
Y.Assert.isFalse(Y.isEmpty({ "wooga" : 1, "fooga" : 2 }));
},
"empty array should be true" : function() {
Y.Assert.isTrue(Y.isEmpty([ ]));
},
"non-empty array should be false" : function() {
Y.Assert.isFalse(Y.isEmpty([ 1, 2, 3 ]));
},
"empty array-like object should be true" : function() {
Y.Assert.isTrue(Y.isEmpty(arguments));
},
"non-empty array-like object should be false" : function() {
var func = (function(a, b) {
Y.Assert.isFalse(Y.isEmpty(arguments));
})(1, 2);
},
"empty node should be true" : function() {
Y.Assert.isTrue(Y.isEmpty(Y.one("#empty")));
},
"non-empty node should be false" : function() {
Y.Assert.isFalse(Y.isEmpty(Y.one("#non-empty")));
},
"empty nodelist should be true" : function() {
Y.Assert.isTrue(Y.isEmpty(Y.all("#non-existant-id")));
},
"non-empty nodelist should be false" : function() {
Y.Assert.isFalse(Y.isEmpty(Y.all("#non-empty")));
},
"empty YUI object constructor created object should be true" : function() {
Y.Assert.isTrue(Y.isEmpty(Y.Object({ })));
},
"non-empty YUI object constructor created object should be true" : function() {
Y.Assert.isTrue(Y.isEmpty(Y.Object({ "booga" : 1 })));
}
});
<!DOCTYPE html>
<html>
<head>
<title>Y.isEmpty Tests</title>
<meta charset="utf-8" />
</head>
<body lang="en-US">
<div id="empty"></div>
<div id="non-empty">
<p></p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment