Skip to content

Commit

Permalink
refactor: Change order of arguments of internal domEach (#1892)
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 authored May 28, 2021
1 parent cba456e commit feda230
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
22 changes: 11 additions & 11 deletions src/api/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,11 @@ export function attr<T extends Node>(
throw new Error('Bad combination of arguments.');
}
}
return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
if (isTag(el)) setAttr(el, name, value.call(el, i, el.attribs[name]));
});
}
return domEach(this, (_, el) => {
return domEach(this, (el) => {
if (!isTag(el)) return;

if (typeof name === 'object') {
Expand Down Expand Up @@ -362,12 +362,12 @@ export function prop<T extends Node>(
if (typeof name === 'object') {
throw new Error('Bad combination of arguments.');
}
return domEach(this, (j, el) => {
if (isTag(el)) setProp(el, name, value.call(el, j, getProp(el, name)));
return domEach(this, (el, i) => {
if (isTag(el)) setProp(el, name, value.call(el, i, getProp(el, name)));
});
}

return domEach(this, (__, el) => {
return domEach(this, (el) => {
if (!isTag(el)) return;

if (typeof name === 'object') {
Expand Down Expand Up @@ -566,7 +566,7 @@ export function data<T extends Node>(

// Set the value (with attr map support)
if (typeof name === 'object' || value !== undefined) {
domEach(this, (_, el) => {
domEach(this, (el) => {
if (isTag(el))
if (typeof name === 'object') setData(el, name);
else setData(el, name, value as unknown);
Expand Down Expand Up @@ -711,7 +711,7 @@ export function removeAttr<T extends Node>(
const attrNames = splitNames(name);

for (let i = 0; i < attrNames.length; i++) {
domEach(this, (_, elem) => {
domEach(this, (elem) => {
if (isTag(elem)) removeAttribute(elem, attrNames[i]);
});
}
Expand Down Expand Up @@ -791,7 +791,7 @@ export function addClass<T extends Node, R extends ArrayLike<T>>(
): R {
// Support functions
if (typeof value === 'function') {
return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
if (isTag(el)) {
const className = el.attribs.class || '';
addClass.call([el], value.call(el, i, className));
Expand Down Expand Up @@ -858,7 +858,7 @@ export function removeClass<T extends Node, R extends ArrayLike<T>>(
): R {
// Handle if value is a function
if (typeof name === 'function') {
return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
if (isTag(el))
removeClass.call([el], name.call(el, i, el.attribs.class || ''));
});
Expand All @@ -868,7 +868,7 @@ export function removeClass<T extends Node, R extends ArrayLike<T>>(
const numClasses = classes.length;
const removeAll = arguments.length === 0;

return domEach(this, (_, el) => {
return domEach(this, (el) => {
if (!isTag(el)) return;

if (removeAll) {
Expand Down Expand Up @@ -933,7 +933,7 @@ export function toggleClass<T extends Node, R extends ArrayLike<T>>(
): R {
// Support functions
if (typeof value === 'function') {
return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
if (isTag(el)) {
toggleClass.call(
[el],
Expand Down
4 changes: 2 additions & 2 deletions src/api/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export function css<T extends Node>(
// When `prop` is a "plain" object
(typeof prop === 'object' && !Array.isArray(prop))
) {
return domEach(this, (idx, el) => {
return domEach(this, (el, i) => {
if (isTag(el)) {
// `prop` can't be an array here anymore.
setCss(el, prop as string, val, idx);
setCss(el, prop as string, val, i);
}
});
}
Expand Down
22 changes: 11 additions & 11 deletions src/api/manipulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function _insert(
) {
const lastIdx = this.length - 1;

return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
if (!hasChildren(el)) return;
const domSrc =
typeof elems[0] === 'function'
Expand Down Expand Up @@ -585,7 +585,7 @@ export function after<T extends Node>(
): Cheerio<T> {
const lastIdx = this.length - 1;

return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
const { parent } = el;
if (!DomUtils.hasChildren(el) || !parent) {
return;
Expand Down Expand Up @@ -645,7 +645,7 @@ export function insertAfter<T extends Node>(

const clones: T[] = [];

domEach(this._makeDomArray(target), (_, el) => {
this._makeDomArray(target).forEach((el) => {
const clonedSelf = this.clone().toArray();
const { parent } = el;
if (!parent) {
Expand Down Expand Up @@ -699,7 +699,7 @@ export function before<T extends Node>(
): Cheerio<T> {
const lastIdx = this.length - 1;

return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
const { parent } = el;
if (!DomUtils.hasChildren(el) || !parent) {
return;
Expand Down Expand Up @@ -757,7 +757,7 @@ export function insertBefore<T extends Node>(

const clones: T[] = [];

domEach(targetArr, (_, el) => {
domEach(targetArr, (el) => {
const clonedSelf = this.clone().toArray();
const { parent } = el;
if (!parent) {
Expand Down Expand Up @@ -806,7 +806,7 @@ export function remove<T extends Node>(
// Filter if we have selector
const elems = selector ? this.filter(selector) : this;

domEach(elems, (_, el) => {
domEach(elems, (el) => {
DomUtils.removeElement(el);
el.prev = el.next = el.parent = null;
});
Expand Down Expand Up @@ -839,7 +839,7 @@ export function replaceWith<T extends Node>(
this: Cheerio<T>,
content: AcceptedElems<Node>
): Cheerio<T> {
return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
const { parent } = el;
if (!parent) {
return;
Expand Down Expand Up @@ -883,7 +883,7 @@ export function replaceWith<T extends Node>(
* @see {@link https://api.jquery.com/empty/}
*/
export function empty<T extends Node>(this: Cheerio<T>): Cheerio<T> {
return domEach(this, (_, el) => {
return domEach(this, (el) => {
if (!DomUtils.hasChildren(el)) return;
el.children.forEach((child) => {
child.next = child.prev = child.parent = null;
Expand Down Expand Up @@ -930,7 +930,7 @@ export function html<T extends Node>(
// Keep main options unchanged
const opts = { ...this.options, context: null as NodeWithChildren | null };

return domEach(this, (_, el) => {
return domEach(this, (el) => {
if (!DomUtils.hasChildren(el)) return;
el.children.forEach((child) => {
child.next = child.prev = child.parent = null;
Expand Down Expand Up @@ -993,13 +993,13 @@ export function text<T extends Node>(
}
if (typeof str === 'function') {
// Function support
return domEach(this, (i, el) => {
return domEach(this, (el, i) => {
text.call(this._make(el), str.call(el, i, staticText([el])));
});
}

// Append text node to each selected elements
return domEach(this, (_, el) => {
return domEach(this, (el) => {
if (!DomUtils.hasChildren(el)) return;
el.children.forEach((child) => {
child.next = child.prev = child.parent = null;
Expand Down
16 changes: 8 additions & 8 deletions src/api/traversing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export function parent<T extends Node>(
): Cheerio<Element> {
const set: Element[] = [];

domEach(this, (_, elem) => {
domEach(this, (elem) => {
const parentElem = elem.parent;
if (
parentElem &&
Expand Down Expand Up @@ -245,7 +245,7 @@ export function closest<T extends Node>(
return this._make(set);
}

domEach(this, (_, elem) => {
domEach(this, (elem) => {
const closestElem = traverseParents(this, elem, selector, 1)[0];

// Do not add duplicate elements to the set
Expand Down Expand Up @@ -278,7 +278,7 @@ export function next<T extends Node>(
): Cheerio<Element> {
const elems: Element[] = [];

domEach(this, (_, elem) => {
domEach(this, (elem) => {
while (elem.next) {
elem = elem.next;
if (isTag(elem)) {
Expand Down Expand Up @@ -315,7 +315,7 @@ export function nextAll<T extends Node>(
): Cheerio<Element> {
const elems: Element[] = [];

domEach(this, (_, elem: Node) => {
domEach(this, (elem: Node) => {
while (elem.next) {
elem = elem.next;
if (isTag(elem) && !elems.includes(elem)) {
Expand Down Expand Up @@ -361,7 +361,7 @@ export function nextUntil<T extends Node>(
untilNode = selector;
}

domEach(this, (_, elem) => {
domEach(this, (elem) => {
while (elem.next) {
elem = elem.next;
if (
Expand Down Expand Up @@ -405,7 +405,7 @@ export function prev<T extends Node>(
): Cheerio<Element> {
const elems: Element[] = [];

domEach(this, (_, elem: Node) => {
domEach(this, (elem: Node) => {
while (elem.prev) {
elem = elem.prev;
if (isTag(elem)) {
Expand Down Expand Up @@ -443,7 +443,7 @@ export function prevAll<T extends Node>(
): Cheerio<Element> {
const elems: Element[] = [];

domEach(this, (_, elem) => {
domEach(this, (elem) => {
while (elem.prev) {
elem = elem.prev;
if (isTag(elem) && !elems.includes(elem)) {
Expand Down Expand Up @@ -489,7 +489,7 @@ export function prevUntil<T extends Node>(
untilNode = selector;
}

domEach(this, (_, elem) => {
domEach(this, (elem) => {
while (elem.prev) {
elem = elem.prev;
if (
Expand Down
2 changes: 1 addition & 1 deletion src/cheerio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ describe('cheerio', () => {
it('cloneDom : should be able clone single Elements', () => {
const main = cheerio('<p>Cheerio</p>') as Cheerio<Element>;
let result: Element[] = [];
utils.domEach<Element>(main, (_, el) => {
utils.domEach<Element>(main, (el) => {
result = [...result, ...utils.cloneDom(el)];
});
expect(result).toHaveLength(1);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export function cssCase(str: string): string {
*/
export function domEach<T extends Node, Arr extends ArrayLike<T> = Cheerio<T>>(
array: Arr,
fn: (index: number, elem: T) => void | false
fn: (elem: T, index: number) => void
): Arr {
const len = array.length;
for (let i = 0; i < len && fn(i, array[i]) !== false; i++);
for (let i = 0; i < len; i++) fn(array[i], i);
return array;
}

Expand Down

0 comments on commit feda230

Please sign in to comment.