Skip to content

Commit

Permalink
speed up getAdjacentEl (#726)
Browse files Browse the repository at this point in the history
* use element accessors

* don't change params
  • Loading branch information
tinovyatkin authored and jshjohnson committed Oct 31, 2019
1 parent 9e11db8 commit 5afe8b5
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/scripts/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
export const getRandomNumber = (min, max) =>
Math.floor(Math.random() * (max - min) + min);

export const generateChars = length => {
let chars = '';

for (let i = 0; i < length; i++) {
const randomChar = getRandomNumber(0, 36);
chars += randomChar.toString(36);
}

return chars;
};
export const generateChars = length =>
Array.from({ length }, () => getRandomNumber(0, 36).toString(36)).join('');

export const generateId = (element, prefix) => {
let id =
Expand Down Expand Up @@ -44,19 +36,30 @@ export const wrap = (element, wrapper = document.createElement('div')) => {
*/
export const findAncestorByAttrName = (el, attr) => el.closest(`[${attr}]`);

export const getAdjacentEl = (startEl, className, direction = 1) => {
if (!startEl || !className) {
return;
}
export const getAdjacentEl =
/**
* @param {Element} startEl
* @param {string} selector
* @param {1 | -1} direction
* @returns {Element | undefined}
*/
(startEl, selector, direction = 1) => {
if (!(startEl instanceof Element) || typeof selector !== 'string') {
return undefined;
}

const parent = startEl.parentNode.parentNode;
const children = Array.from(parent.querySelectorAll(className));
const prop = `${direction > 0 ? 'next' : 'previous'}ElementSibling`;

const startPos = children.indexOf(startEl);
const operatorDirection = direction > 0 ? 1 : -1;
let sibling = startEl[prop];
while (sibling) {
if (sibling.matches(selector)) {
return sibling;
}
sibling = sibling[prop];
}

return children[startPos + operatorDirection];
};
return sibling;
};

export const isScrolledIntoView = (el, parent, direction = 1) => {
if (!el) {
Expand Down

0 comments on commit 5afe8b5

Please sign in to comment.