Skip to content

Commit

Permalink
refactor: Move is to traversing, optimize (#1908)
Browse files Browse the repository at this point in the history
Moved so it can use `getFilterFn`.
  • Loading branch information
fb55 authored Jun 5, 2021
1 parent c81a512 commit 1c6fa3e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 78 deletions.
52 changes: 0 additions & 52 deletions src/api/attributes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,56 +1028,4 @@ describe('$(...)', () => {
);
});
});

describe('.is', () => {
it('() : should return false', () => {
expect($('li.apple').is()).toBe(false);
});

it('(true selector) : should return true', () => {
expect(cheerio('#vegetables', vegetables).is('ul')).toBe(true);
});

it('(false selector) : should return false', () => {
expect(cheerio('#vegetables', vegetables).is('div')).toBe(false);
});

it('(true selection) : should return true', () => {
const $vegetables = cheerio('li', vegetables);
expect($vegetables.is($vegetables.eq(1))).toBe(true);
});

it('(false selection) : should return false', () => {
const $vegetableList = cheerio(vegetables);
const $vegetables = $vegetableList.find('li');
expect($vegetables.is($vegetableList)).toBe(false);
});

it('(true element) : should return true', () => {
const $vegetables = cheerio('li', vegetables);
expect($vegetables.is($vegetables[0])).toBe(true);
});

it('(false element) : should return false', () => {
const $vegetableList = cheerio(vegetables);
const $vegetables = $vegetableList.find('li');
expect($vegetables.is($vegetableList[0])).toBe(false);
});

it('(true predicate) : should return true', () => {
const result = $('li').is(function () {
return this.tagName === 'li' && $(this).hasClass('pear');
});
expect(result).toBe(true);
});

it('(false predicate) : should return false', () => {
const result = $('li')
.last()
.is(function () {
return this.tagName === 'ul';
});
expect(result).toBe(false);
});
});
});
23 changes: 0 additions & 23 deletions src/api/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { text } from '../static';
import { isTag, domEach, camelCase, cssCase } from '../utils';
import type { Node, Element } from 'domhandler';
import type { Cheerio } from '../cheerio';
import { AcceptedFilters } from '../types';
const hasOwn = Object.prototype.hasOwnProperty;
const rspace = /\s+/;
const dataAttrPrefix = 'data-';
Expand Down Expand Up @@ -1000,25 +999,3 @@ export function toggleClass<T extends Node, R extends ArrayLike<T>>(

return this;
}

/**
* Checks the current list of elements and returns `true` if *any* of the
* elements match the selector. If using an element or Cheerio selection,
* returns `true` if *any* of the elements match. If using a predicate function,
* the function is executed in the context of the selected element, so `this`
* refers to the current element.
*
* @category Attributes
* @param selector - Selector for the selection.
* @returns Whether or not the selector matches an element of the instance.
* @see {@link https://api.jquery.com/is/}
*/
export function is<T>(
this: Cheerio<T>,
selector?: AcceptedFilters<T>
): boolean {
if (selector) {
return this.filter(selector).length > 0;
}
return false;
}
53 changes: 53 additions & 0 deletions src/api/traversing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
text,
forms,
mixedText,
vegetables,
} from '../__fixtures__/fixtures';

function getText(el: Cheerio<Element>) {
Expand Down Expand Up @@ -1528,4 +1529,56 @@ describe('$(...)', () => {
expect($div.addBack()).toBe($div);
});
});

describe('.is', () => {
it('() : should return false', () => {
expect($('li.apple').is()).toBe(false);
});

it('(true selector) : should return true', () => {
expect(cheerio('#vegetables', vegetables).is('ul')).toBe(true);
});

it('(false selector) : should return false', () => {
expect(cheerio('#vegetables', vegetables).is('div')).toBe(false);
});

it('(true selection) : should return true', () => {
const $vegetables = cheerio('li', vegetables);
expect($vegetables.is($vegetables.eq(1))).toBe(true);
});

it('(false selection) : should return false', () => {
const $vegetableList = cheerio(vegetables);
const $vegetables = $vegetableList.find('li');
expect($vegetables.is($vegetableList)).toBe(false);
});

it('(true element) : should return true', () => {
const $vegetables = cheerio('li', vegetables);
expect($vegetables.is($vegetables[0])).toBe(true);
});

it('(false element) : should return false', () => {
const $vegetableList = cheerio(vegetables);
const $vegetables = $vegetableList.find('li');
expect($vegetables.is($vegetableList[0])).toBe(false);
});

it('(true predicate) : should return true', () => {
const result = $('li').is(function () {
return this.tagName === 'li' && $(this).hasClass('pear');
});
expect(result).toBe(true);
});

it('(false predicate) : should return false', () => {
const result = $('li')
.last()
.is(function () {
return this.tagName === 'ul';
});
expect(result).toBe(false);
});
});
});
32 changes: 29 additions & 3 deletions src/api/traversing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,9 +683,7 @@ function getFilterFn<T>(
match: FilterFunction<T> | Cheerio<T> | T
): (el: T, i: number) => boolean {
if (typeof match === 'function') {
return function (el, i) {
return (match as FilterFunction<T>).call(el, i, el);
};
return (el, i) => (match as FilterFunction<T>).call(el, i, el);
}
if (isCheerio<T>(match)) {
return (el) => match.is(el);
Expand Down Expand Up @@ -799,6 +797,34 @@ export function filter<T>(
return container._make<unknown>(result);
}

/**
* Checks the current list of elements and returns `true` if *any* of the
* elements match the selector. If using an element or Cheerio selection,
* returns `true` if *any* of the elements match. If using a predicate function,
* the function is executed in the context of the selected element, so `this`
* refers to the current element.
*
* @category Attributes
* @param selector - Selector for the selection.
* @returns Whether or not the selector matches an element of the instance.
* @see {@link https://api.jquery.com/is/}
*/
export function is<T>(
this: Cheerio<T>,
selector?: AcceptedFilters<T>
): boolean {
const nodes = this.toArray();
return typeof selector === 'string'
? select.some(
(nodes as unknown as Node[]).filter(isTag),
selector,
this.options
)
: selector
? nodes.some(getFilterFn<T>(selector))
: false;
}

/**
* Remove elements from the set of matched elements. Given a Cheerio object that
* represents a set of DOM elements, the `.not()` method constructs a new
Expand Down

0 comments on commit 1c6fa3e

Please sign in to comment.