#69 : Remove unnecessary fns

This commit is contained in:
Vadim
2021-09-11 13:18:11 +03:00
parent 8c0a1fbd4c
commit 938a95ad3b
3 changed files with 74 additions and 67 deletions

View File

@@ -131,6 +131,7 @@
infinite,
pageIndex,
clonesCountHead: clonesCount.head,
clonesCountTail: clonesCount.tail,
particlesToScroll,
particlesCount,
particlesToShow,
@@ -292,6 +293,7 @@
infinite,
pageIndex: initialPageIndex,
clonesCountHead: clonesCount.head,
clonesCountTail: clonesCount.tail,
particlesToScroll,
particlesCount,
particlesToShow,
@@ -314,6 +316,7 @@
infinite,
pageIndex,
clonesCountHead: clonesCount.head,
clonesCountTail: clonesCount.tail,
particlesToScroll,
particlesCount,
particlesToShow,
@@ -376,8 +379,12 @@
await changePage(
() => store.prev({
infinite,
particlesCount,
currentPageIndex,
clonesCountHead: clonesCount.head,
clonesCountTail: clonesCount.tail,
particlesToScroll,
particlesCount,
particlesToShow,
}),
options,
)
@@ -387,9 +394,11 @@
await changePage(
() => store.next({
infinite,
currentPageIndex,
particlesCount,
particlesToScroll,
particlesToShow,
clonesCountHead: clonesCount.head,
clonesCountTail: clonesCount.tail,
}),
options,

View File

@@ -2,8 +2,7 @@ import {
writable,
} from 'svelte/store';
import {
getNextParticleIndexFn,
getPrevParticleIndexFn,
getParticleIndexByPageIndex,
} from './utils/page'
import {
getValueInRange,
@@ -37,18 +36,22 @@ function createStore() {
function next({
infinite,
currentPageIndex,
particlesCount,
particlesToScroll,
particlesToShow,
clonesCountHead,
clonesCountTail,
}) {
update(store => {
const newCurrentParticleIndex = getNextParticleIndexFn(infinite)({
currentParticleIndex: store.currentParticleIndex,
particlesCount,
particlesToScroll,
particlesToShow,
const newCurrentParticleIndex = getParticleIndexByPageIndex({
infinite,
pageIndex: currentPageIndex + 1,
clonesCountHead,
clonesCountTail,
particlesToScroll,
particlesCount,
particlesToShow,
})
return {
...store,
@@ -59,14 +62,22 @@ function createStore() {
function prev({
infinite,
particlesCount,
currentPageIndex,
clonesCountHead,
clonesCountTail,
particlesToScroll,
particlesCount,
particlesToShow,
}) {
update(store => {
const newCurrentParticleIndex = getPrevParticleIndexFn(infinite)({
currentParticleIndex: store.currentParticleIndex,
particlesCount,
const newCurrentParticleIndex = getParticleIndexByPageIndex({
infinite,
pageIndex: currentPageIndex - 1,
clonesCountHead,
clonesCountTail,
particlesToScroll,
particlesCount,
particlesToShow,
})
return {
...store,

View File

@@ -2,58 +2,6 @@ import {
getValueInRange,
} from './math'
export function getNextParticleIndexLimited({
currentParticleIndex,
particlesCount,
particlesToScroll,
particlesToShow,
}) {
if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
return getValueInRange(0, currentParticleIndex + particlesToScroll, particlesCount - particlesToShow)
}
export function getNextParticleIndexInfinte({
currentParticleIndex,
particlesCount,
particlesToScroll,
clonesCountTail,
}) {
if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
const newCurrentParticleIndex = Math.max(currentParticleIndex, 0) + Math.min(particlesCount - clonesCountTail - currentParticleIndex, particlesToScroll)
return newCurrentParticleIndex > particlesCount - 1 ? 0 : Math.max(newCurrentParticleIndex, 0)
}
export function getNextParticleIndexFn(infinite) {
return infinite ? getNextParticleIndexInfinte : getNextParticleIndexLimited
}
export function getPrevParticleIndexLimited({
currentParticleIndex,
particlesCount,
particlesToScroll,
}) {
if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
return getValueInRange(
0,
currentParticleIndex - Math.min(currentParticleIndex, particlesToScroll),
particlesCount - 1
)
}
export function getPrevParticleIndexInfinte({
currentParticleIndex,
particlesCount,
particlesToScroll,
}) {
if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
const newCurrentParticleIndex = Math.min(currentParticleIndex, particlesCount - 1) - Math.min(currentParticleIndex, particlesToScroll)
return newCurrentParticleIndex >= 0 ? Math.min(newCurrentParticleIndex, particlesCount - 1) : particlesCount - 1
}
export function getPrevParticleIndexFn(infinite) {
return infinite ? getPrevParticleIndexInfinte : getPrevParticleIndexLimited
}
export function getSizes({
pageWindowElement,
particlesContainerChildren,
@@ -123,15 +71,54 @@ export function getPagesCountByParticlesCount({
: Math.round(particlesCountWithoutClones / particlesToScroll)
}
export function getParticleIndexByPageIndexInfinite({
pageIndex,
clonesCountHead,
clonesCountTail,
particlesToScroll,
particlesCount,
}) {
return getValueInRange(
clonesCountHead,
Math.min(clonesCountHead + pageIndex * particlesToScroll, particlesCount - clonesCountTail),
particlesCount - 1
)
}
export function getParticleIndexByPageIndexLimited({
pageIndex,
particlesToScroll,
particlesCount,
particlesToShow,
}) {
return getValueInRange(
0,
Math.min(pageIndex * particlesToScroll, particlesCount - particlesToShow),
particlesCount - 1
)
}
export function getParticleIndexByPageIndex({
infinite,
pageIndex,
clonesCountHead,
clonesCountTail,
particlesToScroll,
particlesCount,
particlesToShow,
}) {
return infinite
? clonesCountHead + pageIndex * particlesToScroll
: Math.min(pageIndex * particlesToScroll, particlesCount - particlesToShow)
? getParticleIndexByPageIndexInfinite({
pageIndex,
clonesCountHead,
clonesCountTail,
particlesToScroll,
particlesCount,
})
: getParticleIndexByPageIndexLimited({
pageIndex,
particlesToScroll,
particlesCount,
particlesToShow,
})
}