Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.is() function #202

Merged
merged 3 commits into from
Jun 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ $('.apple').addClass('red').removeClass().html()

> See http://api.jquery.com/removeClass/ for more information.

#### .is( selector )
#### .is( function(index) )
Checks the current list of elements and returns `true` if _any_ of the elements match the selector. If using a predicate function, the function is executed in the context of the selected element, so `this` refers to the current element.


### Traversing

Expand Down
8 changes: 8 additions & 0 deletions lib/api/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,11 @@ var removeClass = exports.removeClass = function(value) {
).join(' ');
});
};

var is = exports.is = function (selector) {
if (selector) {
return this.filter(selector).length > 0;
}
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return !!(selector && this.filter(selector).length);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method more performant or just more terse?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latter. I'll take one line over four just about every time. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I have to change it for it to get merged or is this just a style preference?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can leave it as is if you prefer it that way. :)

I'm going to leave this pull request for @matthewmueller, since it's an API addition rather than a bug fix.

}

28 changes: 26 additions & 2 deletions test/api.attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ describe('$(...)', function() {
$apple.attr('href', 'http://github.com/"><script>alert("XSS!")</script><br');
expect($apple.html()).to.not.contain('<script>alert("XSS!")</script>');
});

it('(key, value) : should coerce values to a string', function() {
var $apple = $('.apple', fruits);
$apple.attr('data-test', 1);
expect($apple[0].attribs['data-test']).to.equal('1');
expect($apple.attr('data-test')).to.equal('1');
});
});
});

describe('.val', function() {
Expand Down Expand Up @@ -257,4 +257,28 @@ describe('$(...)', function() {

});

describe('.is', function () {
it('() should return false', function () {
expect($('li.apple', fruits).is()).to.be(false)
})
it('(true selector) should return true', function () {
expect($('#vegetables', vegetables).is('ul')).to.be(true)
})
it('(false selector) should return false', function () {
expect($('#vegetables', vegetables).is('div')).to.be(false)
})
it('(true predicate) should return true', function () {
var result = $('li', fruits).is(function() {
return this.hasClass('pear')
})
expect(result).to.be(true)
})
it('(false predicate) should return false', function () {
var result = $('li', fruits).last().is(function() {
return this.name === 'ul'
})
expect(result).to.be(false)
})
})

});