add fast path to hasAncestor

This commit is contained in:
Jeff Williams 2015-01-16 09:02:13 -08:00
parent 5b4e2c51f7
commit db9a570e2d
2 changed files with 11 additions and 0 deletions

View File

@ -217,6 +217,11 @@ exports.hasAncestor = function(parent, child) {
return hasAncestor;
}
// fast path for obvious non-ancestors
if (child.indexOf(parent) !== 0) {
return hasAncestor;
}
do {
memberof = exports.shorten(memberof).memberof;

View File

@ -293,6 +293,12 @@ describe('jsdoc/name', function() {
expect(hasAncestor).toBe(true);
});
it('should correctly identify when a non-ancestor is passed in', function() {
var hasAncestor = jsdoc.name.hasAncestor('module:foo', 'foo');
expect(hasAncestor).toBe(false);
});
it('should not say that a longname is its own ancestor', function() {
var hasAncestor = jsdoc.name.hasAncestor('module:foo', 'module:foo');