Fix pages count

This commit is contained in:
Vadim
2021-11-30 15:48:22 +03:00
parent f915c7b70d
commit 4605e29679
4 changed files with 43 additions and 3 deletions

View File

@@ -53,7 +53,7 @@
/**
* Infinite looping
*/
export let infinite
export let infinite = false
$: {
data.infinite = infinite
}

View File

@@ -113,6 +113,7 @@ function createCarousel(onChange) {
infinite: data.infinite,
particlesCountWithoutClones: data.particlesCountWithoutClones,
particlesToScroll: data.particlesToScroll,
particlesToShow: data.particlesToShow,
})
},
setParticlesToShow({ data }) {

View File

@@ -60,14 +60,21 @@ export function _getPagesCountByParticlesCountInfinite({
export function _getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
}) {
return Math.round(particlesCountWithoutClones / particlesToScroll)
const partialPageSize = getPartialPageSize({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
})
return Math.ceil(particlesCountWithoutClones / particlesToScroll) - partialPageSize
}
export function getPagesCountByParticlesCount({
infinite,
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
}) {
return infinite
? _getPagesCountByParticlesCountInfinite({
@@ -77,6 +84,7 @@ export function getPagesCountByParticlesCount({
: _getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
})
}

View File

@@ -435,17 +435,42 @@ describe('_getPagesCountByParticlesCountLimited', () => {
const testCases = [{
particlesCountWithoutClones: 5,
particlesToScroll: 2,
expected: 3,
particlesToShow: 3,
expected: 2,
}]
testCases.forEach(({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
expected,
}) => {
expect(_getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
})).toBe(expected)
})
})
it('returns result as expected if particlesCountWithoutClones: 9; particlesToScroll: 2 (particlesToShow: 3)', () => {
const testCases = [{
particlesCountWithoutClones: 9,
particlesToScroll: 2,
particlesToShow: 3,
expected: 4,
}]
testCases.forEach(({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
expected,
}) => {
expect(_getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
})).toBe(expected)
})
})
@@ -454,17 +479,20 @@ describe('_getPagesCountByParticlesCountLimited', () => {
const testCases = [{
particlesCountWithoutClones: 6,
particlesToScroll: 2,
particlesToShow: 2,
expected: 3,
}]
testCases.forEach(({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
expected,
}) => {
expect(_getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
})).toBe(expected)
})
})
@@ -473,17 +501,20 @@ describe('_getPagesCountByParticlesCountLimited', () => {
const testCases = [{
particlesCountWithoutClones: 5,
particlesToScroll: 3,
particlesToShow: 2,
expected: 2,
}]
testCases.forEach(({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
expected,
}) => {
expect(_getPagesCountByParticlesCountLimited({
particlesCountWithoutClones,
particlesToScroll,
particlesToShow,
})).toBe(expected)
})
})