Skip to content

Commit

Permalink
refactor: use .find where possible instead of .filter (#7815)
Browse files Browse the repository at this point in the history
  • Loading branch information
siimsams authored Jan 2, 2025
1 parent 44d3443 commit e5ad9fd
Show file tree
Hide file tree
Showing 13 changed files with 25 additions and 27 deletions.
4 changes: 1 addition & 3 deletions src/components-shared/get-element-params.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ function getParams(element, propName, propValue) {
attrsList.push({ name: propName, value: isObject(propValue) ? { ...propValue } : propValue });
}
attrsList.forEach((attr) => {
const moduleParam = modulesParamsList.filter(
(mParam) => attr.name.indexOf(`${mParam}-`) === 0,
)[0];
const moduleParam = modulesParamsList.find((mParam) => attr.name.startsWith(`${mParam}-`));
if (moduleParam) {
const parentObjName = attrToProp(moduleParam);
const subObjName = attrToProp(attr.name.split(`${moduleParam}-`)[1]);
Expand Down
6 changes: 3 additions & 3 deletions src/core/core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,9 @@ class Swiper {

getSlideIndexByData(index) {
return this.getSlideIndex(
this.slides.filter(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') * 1 === index,
)[0],
this.slides.find(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') * 1 === index
),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/events/onTouchEnd.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function onTouchEnd(event) {
if (e.pointerId !== data.pointerId) return;
targetTouch = e;
} else {
targetTouch = [...e.changedTouches].filter((t) => t.identifier === data.touchId)[0];
targetTouch = [...e.changedTouches].find((t) => t.identifier === data.touchId);
if (!targetTouch || targetTouch.identifier !== data.touchId) return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/events/onTouchMove.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function onTouchMove(event) {

let targetTouch;
if (e.type === 'touchmove') {
targetTouch = [...e.changedTouches].filter((t) => t.identifier === data.touchId)[0];
targetTouch = [...e.changedTouches].find((t) => t.identifier === data.touchId);
if (!targetTouch || targetTouch.identifier !== data.touchId) return;
} else {
targetTouch = e;
Expand Down
2 changes: 1 addition & 1 deletion src/core/loop/loopFix.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default function loopFix({

if (typeof activeSlideIndex === 'undefined') {
activeSlideIndex = swiper.getSlideIndex(
slides.filter((el) => el.classList.contains(params.slideActiveClass))[0],
slides.find((el) => el.classList.contains(params.slideActiveClass)),
);
} else {
activeIndex = activeSlideIndex;
Expand Down
8 changes: 4 additions & 4 deletions src/core/slide/slideToLoop.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export default function slideToLoop(index = 0, speed, runCallbacks = true, inter
let targetSlideIndex;
if (gridEnabled) {
const slideIndex = newIndex * swiper.params.grid.rows;
targetSlideIndex = swiper.slides.filter(
targetSlideIndex = swiper.slides.find(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') * 1 === slideIndex,
)[0].column;
).column;
} else {
targetSlideIndex = swiper.getSlideIndexByData(newIndex);
}
Expand Down Expand Up @@ -70,9 +70,9 @@ export default function slideToLoop(index = 0, speed, runCallbacks = true, inter

if (gridEnabled) {
const slideIndex = newIndex * swiper.params.grid.rows;
newIndex = swiper.slides.filter(
newIndex = swiper.slides.find(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') * 1 === slideIndex,
)[0].column;
).column;
} else {
newIndex = swiper.getSlideIndexByData(newIndex);
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/update/updateActiveIndex.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default function updateActiveIndex(newActiveIndex) {
if (swiper.virtual && params.virtual.enabled && params.loop) {
realIndex = getVirtualRealIndex(activeIndex);
} else if (gridEnabled) {
const firstSlideInColumn = swiper.slides.filter((slideEl) => slideEl.column === activeIndex)[0];
const firstSlideInColumn = swiper.slides.find((slideEl) => slideEl.column === activeIndex);
let activeSlideIndex = parseInt(firstSlideInColumn.getAttribute('data-swiper-slide-index'), 10);
if (Number.isNaN(activeSlideIndex)) {
activeSlideIndex = Math.max(swiper.slides.indexOf(firstSlideInColumn), 0);
Expand Down
6 changes: 3 additions & 3 deletions src/core/update/updateSlidesClasses.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export default function updateSlidesClasses() {
}
} else {
if (gridEnabled) {
activeSlide = slides.filter((slideEl) => slideEl.column === activeIndex)[0];
nextSlide = slides.filter((slideEl) => slideEl.column === activeIndex + 1)[0];
prevSlide = slides.filter((slideEl) => slideEl.column === activeIndex - 1)[0];
activeSlide = slides.find((slideEl) => slideEl.column === activeIndex);
nextSlide = slides.find((slideEl) => slideEl.column === activeIndex + 1);
prevSlide = slides.find((slideEl) => slideEl.column === activeIndex - 1);
} else {
activeSlide = slides[activeIndex];
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/autoplay/autoplay.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export default function Autoplay({ swiper, extendParams, on, emit, params }) {
const getSlideDelay = () => {
let activeSlideEl;
if (swiper.virtual && swiper.params.virtual.enabled) {
activeSlideEl = swiper.slides.filter((slideEl) =>
activeSlideEl = swiper.slides.find((slideEl) =>
slideEl.classList.contains('swiper-slide-active'),
)[0];
);
} else {
activeSlideEl = swiper.slides[swiper.activeIndex];
}
Expand Down
4 changes: 2 additions & 2 deletions src/modules/hash-navigation/hash-navigation.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default function HashNavigation({ swiper, extendParams, emit, on }) {
watchState: false,
getSlideIndex(_s, hash) {
if (swiper.virtual && swiper.params.virtual.enabled) {
const slideWithHash = swiper.slides.filter(
const slideWithHash = swiper.slides.find(
(slideEl) => slideEl.getAttribute('data-hash') === hash,
)[0];
);
if (!slideWithHash) return 0;
const index = parseInt(slideWithHash.getAttribute('data-swiper-slide-index'), 10);
return index;
Expand Down
4 changes: 2 additions & 2 deletions src/modules/pagination/pagination.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,10 @@ export default function Pagination({ swiper, extendParams, on, emit }) {
el = [...swiper.el.querySelectorAll(params.el)];
// check if it belongs to another nested Swiper
if (el.length > 1) {
el = el.filter((subEl) => {
el = el.find((subEl) => {
if (elementParents(subEl, '.swiper')[0] !== swiper.el) return false;
return true;
})[0];
});
}
}
if (Array.isArray(el) && el.length === 1) el = el[0];
Expand Down
4 changes: 2 additions & 2 deletions src/modules/thumbs/thumbs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ export default function Thumb({ swiper, extendParams, on }) {
let newThumbsIndex;
let direction;
if (thumbsSwiper.params.loop) {
const newThumbsSlide = thumbsSwiper.slides.filter(
const newThumbsSlide = thumbsSwiper.slides.find(
(slideEl) => slideEl.getAttribute('data-swiper-slide-index') === `${swiper.realIndex}`,
)[0];
);
newThumbsIndex = thumbsSwiper.slides.indexOf(newThumbsSlide);

direction = swiper.activeIndex > swiper.previousIndex ? 'next' : 'prev';
Expand Down
4 changes: 2 additions & 2 deletions src/shared/effect-virtual-transition-end.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ export default function effectVirtualTransitionEnd({
const getSlide = (el) => {
if (!el.parentElement) {
// assume shadow root
const slide = swiper.slides.filter(
const slide = swiper.slides.find(
(slideEl) => slideEl.shadowRoot && slideEl.shadowRoot === el.parentNode,
)[0];
);
return slide;
}
return el.parentElement;
Expand Down

0 comments on commit e5ad9fd

Please sign in to comment.