Skip to content

Commit

Permalink
Clean getMatchingElement (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Aug 20, 2024
1 parent 4743d5e commit 9517124
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function elementReady(selector, {

// When it's ready, only stop if requested or found
if (isDomReady(target) && (stopOnDomReady || element)) {
return element ?? undefined; // No `null`
return element;
}

let current = element;
Expand Down Expand Up @@ -121,11 +121,14 @@ export function observeReadyElements(selector, {
};
}

function getMatchingElement({target, selector, predicate} = {}) {
if (predicate) {
const elements = target.querySelectorAll(selector);
return [...elements].find(element => predicate(element));
function getMatchingElement({target, selector, predicate}) {
if (!predicate) {
return target.querySelector(selector) ?? undefined; // No `null`
}

return target.querySelector(selector);
for (const element of target.querySelectorAll(selector)) {
if (predicate(element)) {
return element;
}
}
}

0 comments on commit 9517124

Please sign in to comment.