#69 : Rename entities

This commit is contained in:
Vadim
2021-09-10 13:44:44 +03:00
parent f911338c4b
commit b45d959efe
9 changed files with 571 additions and 581 deletions

View File

@@ -13,12 +13,12 @@
removeResizeEventListener removeResizeEventListener
} from '../../utils/event' } from '../../utils/event'
import { import {
getPageSizes, getSizes,
applyPageSizes, applyParticleSizes,
getCurrentScrollIndex, getCurrentPageIndex,
getPartialPageSize, getPartialPageSize,
getScrollsCount, getPagesCount,
getPageIndexByScrollIndex, getParticleIndexByPageIndex,
} from '../../utils/page' } from '../../utils/page'
import { import {
getClones, getClones,
@@ -65,7 +65,6 @@
* Page to start on * Page to start on
*/ */
export let initialPageIndex = 0 export let initialPageIndex = 0
$: initialScrollIndex = initialPageIndex
/** /**
* Transition duration (ms) * Transition duration (ms)
@@ -112,28 +111,27 @@
export let swiping = true export let swiping = true
/** /**
* Number of pages to show * Number of particles to show
*/ */
export let pagesToShow = 1 export let pagesToShow = 1
/** /**
* Number of pages to scroll * Number of particles to scroll
*/ */
export let pagesToScroll = 1 export let pagesToScroll = 1
export async function goTo(pageIndex, options) { export async function goTo(pageIndex, options) {
const scrollIndex = pageIndex
const animated = get(options, 'animated', true) const animated = get(options, 'animated', true)
if (typeof scrollIndex !== 'number') { if (typeof pageIndex !== 'number') {
throw new Error('pageIndex should be a number') throw new Error('pageIndex should be a number')
} }
await showPage(getPageIndexByScrollIndex({ await showPage(getParticleIndexByPageIndex({
infinite, infinite,
scrollIndex, pageIndex,
clonesCountHead: clonesCount.head, clonesCountHead: clonesCount.head,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
pagesToShow, particlesToShow,
}), { animated }) }), { animated })
} }
@@ -151,35 +149,35 @@
$: clonesCount = getClonesCount({ $: clonesCount = getClonesCount({
infinite, infinite,
pagesToShow, particlesToShow,
partialPageSize, partialPageSize,
}) })
let currentPageIndex = 0 let currentParticleIndex = 0
$: currentScrollIndex = getCurrentScrollIndex({ $: currentPageIndex = getCurrentPageIndex({
currentPageIndex, currentParticleIndex,
pagesCount, particlesCount,
headClonesCount: clonesCount.head, headClonesCount: clonesCount.head,
infinite, infinite,
pagesToScroll, particlesToScroll,
}) })
$: dispatch('pageChange', currentScrollIndex) $: dispatch('pageChange', currentPageIndex)
let pagesCount = 0 let particlesCount = 0
let pagesCountWithoutClones = 1 let pagesCountWithoutClones = 1
$: scrollsCount = getScrollsCount({ $: pagesCount = getPagesCount({
infinite, infinite,
pagesCountWithoutClones, pagesCountWithoutClones,
pagesToScroll, particlesToScroll,
}) })
let partialPageSize = 0 let partialPageSize = 0
let pagesWindowWidth = 0 let pageWindowWidth = 0
let pageWidth = 0 let particleWidth = 0
let offset = 0 let offset = 0
let pageWindowElement let pageWindowElement
let pagesContainer let particlesContainer
let focused = false let focused = false
let progressValue let progressValue
@@ -203,27 +201,27 @@
// used for lazy loading images, preloaded only current, adjacent and cloanable images // used for lazy loading images, preloaded only current, adjacent and cloanable images
$: loaded = getAdjacentIndexes({ $: loaded = getAdjacentIndexes({
infinite, infinite,
scrollIndex: currentScrollIndex, pageIndex: currentPageIndex,
scrollsCount, pagesCount,
pagesCount: pagesCountWithoutClones, particlesCount: pagesCountWithoutClones,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
}).pageIndexes }).particleIndexes
function initPageSizes() { function initPageSizes() {
const sizes = getPageSizes({ const sizes = getSizes({
pageWindowElement, pageWindowElement,
pagesContainerChildren: pagesContainer.children, particlesContainerChildren: particlesContainer.children,
pagesToShow, particlesToShow,
}) })
applyPageSizes({ applyParticleSizes({
pagesContainerChildren: pagesContainer.children, particlesContainerChildren: particlesContainer.children,
pageWidth: sizes.pageWidth, particleWidth: sizes.particleWidth,
}) })
pagesWindowWidth = sizes.pagesWindowWidth pageWindowWidth = sizes.pageWindowWidth
pageWidth = sizes.pageWidth particleWidth = sizes.particleWidth
pagesCount = sizes.pagesCount particlesCount = sizes.particlesCount
offsetPage({ offsetPage({
animated: false, animated: false,
@@ -237,10 +235,10 @@
} = getClones({ } = getClones({
headClonesCount: clonesCount.head, headClonesCount: clonesCount.head,
tailClonesCount: clonesCount.tail, tailClonesCount: clonesCount.tail,
pagesContainerChildren: pagesContainer.children, particlesContainerChildren: particlesContainer.children,
}) })
applyClones({ applyClones({
pagesContainer, particlesContainer,
clonesToAppend, clonesToAppend,
clonesToPrepend, clonesToPrepend,
}) })
@@ -250,8 +248,8 @@
// prevent progress change if not infinite for first and last page // prevent progress change if not infinite for first and last page
if ( if (
!infinite && ( !infinite && (
(autoplayDirection === NEXT && currentPageIndex === pagesCount - 1) || (autoplayDirection === NEXT && currentParticleIndex === particlesCount - 1) ||
(autoplayDirection === PREV && currentPageIndex === 0) (autoplayDirection === PREV && currentParticleIndex === 0)
) )
) { ) {
progressManager.reset() progressManager.reset()
@@ -269,29 +267,29 @@
(async () => { (async () => {
await tick() await tick()
cleanupFns.push(store.subscribe(value => { cleanupFns.push(store.subscribe(value => {
currentPageIndex = value.currentPageIndex currentParticleIndex = value.currentParticleIndex
})) }))
cleanupFns.push(() => progressManager.reset()) cleanupFns.push(() => progressManager.reset())
if (pagesContainer && pageWindowElement) { if (particlesContainer && pageWindowElement) {
pagesCountWithoutClones = pagesContainer.children.length pagesCountWithoutClones = particlesContainer.children.length
partialPageSize = getPartialPageSize({ partialPageSize = getPartialPageSize({
pagesToScroll, particlesToScroll,
pagesToShow, particlesToShow,
pagesCountWithoutClones, pagesCountWithoutClones,
}) })
await tick() await tick()
infinite && addClones() infinite && addClones()
// TODO: validate initialScrollIndex // TODO: validate initialPageIndex
store.init(getPageIndexByScrollIndex({ store.init(getParticleIndexByPageIndex({
infinite, infinite,
scrollIndex: initialScrollIndex, pageIndex: initialPageIndex,
clonesCountHead: clonesCount.head, clonesCountHead: clonesCount.head,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
pagesToShow, particlesToShow,
})) }))
initPageSizes() initPageSizes()
@@ -307,13 +305,13 @@
}) })
async function handlePageChange(pageIndex) { async function handlePageChange(pageIndex) {
await showPage(getPageIndexByScrollIndex({ await showPage(getParticleIndexByPageIndex({
infinite, infinite,
scrollIndex: pageIndex, pageIndex,
clonesCountHead: clonesCount.head, clonesCountHead: clonesCount.head,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
pagesToShow, particlesToShow,
})) }))
} }
@@ -322,7 +320,7 @@
return new Promise((resolve) => { return new Promise((resolve) => {
// _duration is an offset animation time // _duration is an offset animation time
_duration = animated ? duration : 0 _duration = animated ? duration : 0
offset = -currentPageIndex * pageWidth offset = -currentParticleIndex * particleWidth
setTimeout(() => { setTimeout(() => {
resolve() resolve()
}, _duration) }, _duration)
@@ -333,10 +331,10 @@
async function jumpIfNeeded() { async function jumpIfNeeded() {
let jumped = false let jumped = false
if (infinite) { if (infinite) {
if (currentPageIndex === 0) { if (currentParticleIndex === 0) {
await showPage(pagesCount - clonesCount.total, { animated: false }) await showPage(particlesCount - clonesCount.total, { animated: false })
jumped = true jumped = true
} else if (currentPageIndex === pagesCount - clonesCount.tail) { } else if (currentParticleIndex === particlesCount - clonesCount.tail) {
await showPage(clonesCount.head, { animated: false }) await showPage(clonesCount.head, { animated: false })
jumped = true jumped = true
} }
@@ -359,11 +357,11 @@
!jumped && applyAutoplayIfNeeded(autoplay) // no need to wait it finishes !jumped && applyAutoplayIfNeeded(autoplay) // no need to wait it finishes
} }
async function showPage(pageIndex, options) { async function showPage(particleIndex, options) {
await changePage( await changePage(
() => store.moveToPage({ () => store.moveToParticle({
pageIndex, particleIndex,
pagesCount, particlesCount,
}), }),
options options
) )
@@ -373,8 +371,8 @@
await changePage( await changePage(
() => store.prev({ () => store.prev({
infinite, infinite,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
}), }),
options, options,
) )
@@ -384,8 +382,8 @@
await changePage( await changePage(
() => store.next({ () => store.next({
infinite, infinite,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
clonesCountTail: clonesCount.tail, clonesCountTail: clonesCount.tail,
}), }),
options, options,
@@ -407,7 +405,7 @@
} }
function handleSwipeEnd() { function handleSwipeEnd() {
if (!swiping) return if (!swiping) return
showPage(currentPageIndex) showPage(currentParticleIndex)
} }
async function handleSwipeFailed() { async function handleSwipeFailed() {
if (!swiping) return if (!swiping) return
@@ -429,7 +427,7 @@
<div class="sc-carousel__arrow-container"> <div class="sc-carousel__arrow-container">
<Arrow <Arrow
direction="prev" direction="prev"
disabled={!infinite && currentScrollIndex === 0} disabled={!infinite && currentPageIndex === 0}
on:click={showPrevPage} on:click={showPrevPage}
/> />
</div> </div>
@@ -447,7 +445,7 @@
> >
<div <div
class="sc-carousel__pages-container" class="sc-carousel__pages-container"
use:swipeable="{{ thresholdProvider: () => pagesWindowWidth/3 }}" use:swipeable="{{ thresholdProvider: () => pageWindowWidth/3 }}"
on:swipeStart={handleSwipeStart} on:swipeStart={handleSwipeStart}
on:swipeMove={handleSwipeMove} on:swipeMove={handleSwipeMove}
on:swipeEnd={handleSwipeEnd} on:swipeEnd={handleSwipeEnd}
@@ -458,7 +456,7 @@
transition-duration: {_duration}ms; transition-duration: {_duration}ms;
transition-timing-function: {timingFunction}; transition-timing-function: {timingFunction};
" "
bind:this={pagesContainer} bind:this={particlesContainer}
> >
<slot {loaded}></slot> <slot {loaded}></slot>
</div> </div>
@@ -473,7 +471,7 @@
<div class="sc-carousel__arrow-container"> <div class="sc-carousel__arrow-container">
<Arrow <Arrow
direction="next" direction="next"
disabled={!infinite && currentScrollIndex === scrollsCount - 1} disabled={!infinite && currentPageIndex === pagesCount - 1}
on:click={showNextPage} on:click={showNextPage}
/> />
</div> </div>
@@ -483,13 +481,13 @@
{#if dots} {#if dots}
<slot <slot
name="dots" name="dots"
currentPageIndex={currentScrollIndex} currentPageIndex={currentPageIndex}
pagesCount={pagesCountWithoutClones} pagesCount={pagesCountWithoutClones}
showPage={handlePageChange} showPage={handlePageChange}
> >
<Dots <Dots
pagesCount={scrollsCount} pagesCount={pagesCount}
currentPageIndex={currentScrollIndex} currentPageIndex={currentPageIndex}
on:pageChange={event => handlePageChange(event.detail)} on:pageChange={event => handlePageChange(event.detail)}
></Dots> ></Dots>
</slot> </slot>

View File

@@ -2,74 +2,73 @@ import {
writable, writable,
} from 'svelte/store'; } from 'svelte/store';
import { import {
getNextPageIndexFn, getNextParticleIndexFn,
getPrevPageIndexFn, getPrevParticleIndexFn,
getPageIndex,
} from './utils/page' } from './utils/page'
import {
getValueInRange,
} from './utils/math'
const initState = { const initState = {
currentPageIndex: 0, currentParticleIndex: 0,
} }
function createStore() { function createStore() {
const { subscribe, set, update } = writable(initState); const { subscribe, set, update } = writable(initState);
function init(initialPageIndex) { function init(initialParticleIndex) {
set({ set({
...initState, ...initState,
currentPageIndex: initialPageIndex, currentParticleIndex: initialParticleIndex,
}) })
} }
function moveToPage({ function moveToParticle({
pageIndex, particleIndex,
pagesCount, particlesCount,
}) { }) {
update(store => { update(store => {
return { return {
...store, ...store,
currentPageIndex: getPageIndex({ currentParticleIndex: getValueInRange(0, particleIndex, particlesCount - 1),
pageIndex,
pagesCount,
}),
} }
}) })
} }
function next({ function next({
infinite, infinite,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
clonesCountTail, clonesCountTail,
}) { }) {
update(store => { update(store => {
const newCurrentPageIndex = getNextPageIndexFn(infinite)({ const newCurrentParticleIndex = getNextParticleIndexFn(infinite)({
currentPageIndex: store.currentPageIndex, currentParticleIndex: store.currentParticleIndex,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
clonesCountTail, clonesCountTail,
}) })
return { return {
...store, ...store,
currentPageIndex: newCurrentPageIndex, currentParticleIndex: newCurrentParticleIndex,
} }
}) })
} }
function prev({ function prev({
infinite, infinite,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
}) { }) {
update(store => { update(store => {
const newCurrentPageIndex = getPrevPageIndexFn(infinite)({ const newCurrentParticleIndex = getPrevParticleIndexFn(infinite)({
currentPageIndex: store.currentPageIndex, currentParticleIndex: store.currentParticleIndex,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
}) })
return { return {
...store, ...store,
currentPageIndex: newCurrentPageIndex, currentParticleIndex: newCurrentParticleIndex,
} }
}) })
} }
@@ -79,7 +78,7 @@ function createStore() {
next, next,
prev, prev,
init, init,
moveToPage, moveToParticle,
}; };
} }

View File

@@ -1,18 +1,18 @@
export function getClones({ export function getClones({
headClonesCount, headClonesCount,
tailClonesCount, tailClonesCount,
pagesContainerChildren, particlesContainerChildren,
}) { }) {
// TODO: add fns to remove clones if needed // TODO: add fns to remove clones if needed
const clonesToAppend = [] const clonesToAppend = []
for (let i=0; i<tailClonesCount; i++) { for (let i=0; i<tailClonesCount; i++) {
clonesToAppend.push(pagesContainerChildren[i].cloneNode(true)) clonesToAppend.push(particlesContainerChildren[i].cloneNode(true))
} }
const clonesToPrepend = [] const clonesToPrepend = []
const len = pagesContainerChildren.length const len = particlesContainerChildren.length
for (let i=len-1; i>len-1-headClonesCount; i--) { for (let i=len-1; i>len-1-headClonesCount; i--) {
clonesToPrepend.push(pagesContainerChildren[i].cloneNode(true)) clonesToPrepend.push(particlesContainerChildren[i].cloneNode(true))
} }
return { return {
@@ -22,27 +22,27 @@ export function getClones({
} }
export function applyClones({ export function applyClones({
pagesContainer, particlesContainer,
clonesToAppend, clonesToAppend,
clonesToPrepend, clonesToPrepend,
}) { }) {
for (let i=0; i<clonesToAppend.length; i++) { for (let i=0; i<clonesToAppend.length; i++) {
pagesContainer.append(clonesToAppend[i]) particlesContainer.append(clonesToAppend[i])
} }
for (let i=0; i<clonesToPrepend.length; i++) { for (let i=0; i<clonesToPrepend.length; i++) {
pagesContainer.prepend(clonesToPrepend[i]) particlesContainer.prepend(clonesToPrepend[i])
} }
} }
export function getClonesCount({ export function getClonesCount({
infinite, infinite,
pagesToShow, particlesToShow,
partialPageSize, partialPageSize,
}) { }) {
const clonesCount = infinite const clonesCount = infinite
? { ? {
head: partialPageSize || pagesToShow, head: partialPageSize || particlesToShow,
tail: pagesToShow, tail: particlesToShow,
} : { } : {
head: 0, head: 0,
tail: 0, tail: 0,

View File

@@ -1,16 +1,16 @@
import { getValueInRange } from './math' import { getValueInRange } from './math'
export function getIndexesOfPagesWithoutClonesInScroll({ export function getIndexesOfPagesWithoutClonesInScroll({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
}) { }) {
const overlap = scrollIndex === 0 ? 0 : pagesToShow - pagesToScroll const overlap = pageIndex === 0 ? 0 : particlesToShow - particlesToScroll
const from = scrollIndex * pagesToShow - scrollIndex * overlap const from = pageIndex * particlesToShow - pageIndex * overlap
const to = from + Math.max(pagesToShow, pagesToScroll) - 1 const to = from + Math.max(particlesToShow, particlesToScroll) - 1
const indexes = [] const indexes = []
for (let i=from; i<=Math.min(pagesCount - 1, to); i++) { for (let i=from; i<=Math.min(particlesCount - 1, to); i++) {
indexes.push(i) indexes.push(i)
} }
return indexes return indexes
@@ -18,44 +18,44 @@ export function getIndexesOfPagesWithoutClonesInScroll({
export function getAdjacentIndexes({ export function getAdjacentIndexes({
infinite, infinite,
scrollIndex, pageIndex,
scrollsCount,
pagesCount, pagesCount,
pagesToShow, particlesCount,
pagesToScroll, particlesToShow,
particlesToScroll,
}) { }) {
const _scrollIndex = getValueInRange(0, scrollIndex, scrollsCount - 1) const _pageIndex = getValueInRange(0, pageIndex, pagesCount - 1)
let rangeStart = _scrollIndex - 1 let rangeStart = _pageIndex - 1
let rangeEnd = _scrollIndex + 1 let rangeEnd = _pageIndex + 1
rangeStart = infinite rangeStart = infinite
? rangeStart < 0 ? scrollsCount - 1 : rangeStart ? rangeStart < 0 ? pagesCount - 1 : rangeStart
: Math.max(0, rangeStart) : Math.max(0, rangeStart)
rangeEnd = infinite rangeEnd = infinite
? rangeEnd > scrollsCount - 1 ? 0 : rangeEnd ? rangeEnd > pagesCount - 1 ? 0 : rangeEnd
: Math.min(scrollsCount - 1, rangeEnd) : Math.min(pagesCount - 1, rangeEnd)
const scrollIndexes = [...new Set([ const pageIndexes = [...new Set([
rangeStart, rangeStart,
_scrollIndex, _pageIndex,
rangeEnd, rangeEnd,
// because of these values outputs for infinite/non-infinites are the same // because of these values outputs for infinite/non-infinites are the same
0, // needed to clone first scroll pages 0, // needed to clone first page particles
scrollsCount - 1, // needed to clone last scroll pages pagesCount - 1, // needed to clone last page particles
])].sort((a, b) => a - b) ])].sort((a, b) => a - b)
const pageIndexes = scrollIndexes.flatMap( const particleIndexes = pageIndexes.flatMap(
scrollIndex => getIndexesOfPagesWithoutClonesInScroll({ pageIndex => getIndexesOfPagesWithoutClonesInScroll({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
}) })
) )
return { return {
scrollIndexes, pageIndexes,
pageIndexes: [...new Set(pageIndexes)].sort((a, b) => a - b), particleIndexes: [...new Set(particleIndexes)].sort((a, b) => a - b),
} }
} }

View File

@@ -4,68 +4,68 @@ import {
} from './lazy.js' } from './lazy.js'
describe('getIndexesOfPagesWithoutClonesInScroll', () => { describe('getIndexesOfPagesWithoutClonesInScroll', () => {
it('returns correct range if pagesToShow < pagesToScroll', () => { it('returns correct range if particlesToShow < particlesToScroll', () => {
const testCases = [ const testCases = [
{ scrollIndex: 0, pagesToShow: 3, pagesCount: 9, pagesToScroll: 4, expected: [0, 1, 2, 3] }, { pageIndex: 0, particlesToShow: 3, particlesCount: 9, particlesToScroll: 4, expected: [0, 1, 2, 3] },
{ scrollIndex: 1, pagesToShow: 3, pagesCount: 9, pagesToScroll: 4, expected: [4, 5, 6, 7] }, { pageIndex: 1, particlesToShow: 3, particlesCount: 9, particlesToScroll: 4, expected: [4, 5, 6, 7] },
{ scrollIndex: 2, pagesToShow: 3, pagesCount: 9, pagesToScroll: 4, expected: [8] }, { pageIndex: 2, particlesToShow: 3, particlesCount: 9, particlesToScroll: 4, expected: [8] },
] ]
testCases.forEach(({ testCases.forEach(({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
expected, expected,
}) => { }) => {
expect(getIndexesOfPagesWithoutClonesInScroll({ expect(getIndexesOfPagesWithoutClonesInScroll({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
})).toEqual(expected) })).toEqual(expected)
}) })
}) })
it('returns correct range if pagesToShow > pagesToScroll', () => { it('returns correct range if particlesToShow > particlesToScroll', () => {
const testCases = [ const testCases = [
{ scrollIndex: 0, pagesToShow: 4, pagesToScroll: 3, pagesCount: 8, expected: [0, 1, 2, 3] }, { pageIndex: 0, particlesToShow: 4, particlesToScroll: 3, particlesCount: 8, expected: [0, 1, 2, 3] },
{ scrollIndex: 1, pagesToShow: 4, pagesToScroll: 3, pagesCount: 8, expected: [3, 4, 5, 6] }, { pageIndex: 1, particlesToShow: 4, particlesToScroll: 3, particlesCount: 8, expected: [3, 4, 5, 6] },
{ scrollIndex: 2, pagesToShow: 4, pagesToScroll: 3, pagesCount: 8, expected: [6, 7] }, { pageIndex: 2, particlesToShow: 4, particlesToScroll: 3, particlesCount: 8, expected: [6, 7] },
] ]
testCases.forEach(({ testCases.forEach(({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
expected, expected,
}) => { }) => {
expect(getIndexesOfPagesWithoutClonesInScroll({ expect(getIndexesOfPagesWithoutClonesInScroll({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
})).toEqual(expected) })).toEqual(expected)
}) })
}) })
it('returns correct range if pagesToShow == pagesToScroll', () => { it('returns correct range if particlesToShow == particlesToScroll', () => {
const testCases = [ const testCases = [
{ scrollIndex: 0, pagesToShow: 2, pagesToScroll: 2, pagesCount: 5, expected: [0, 1] }, { pageIndex: 0, particlesToShow: 2, particlesToScroll: 2, particlesCount: 5, expected: [0, 1] },
{ scrollIndex: 1, pagesToShow: 2, pagesToScroll: 2, pagesCount: 5, expected: [2, 3] }, { pageIndex: 1, particlesToShow: 2, particlesToScroll: 2, particlesCount: 5, expected: [2, 3] },
{ scrollIndex: 2, pagesToShow: 2, pagesToScroll: 2, pagesCount: 5, expected: [4] }, { pageIndex: 2, particlesToShow: 2, particlesToScroll: 2, particlesCount: 5, expected: [4] },
] ]
testCases.forEach(({ testCases.forEach(({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
expected, expected,
}) => { }) => {
expect(getIndexesOfPagesWithoutClonesInScroll({ expect(getIndexesOfPagesWithoutClonesInScroll({
scrollIndex, pageIndex,
pagesToShow, particlesToShow,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
})).toEqual(expected) })).toEqual(expected)
}) })
}) })
@@ -75,87 +75,87 @@ describe('getAdjacentIndexes', () => {
it('returns indexes as expected if it is infinite', () => { it('returns indexes as expected if it is infinite', () => {
const testCases = [ const testCases = [
{ {
scrollIndex: 0, pageIndex: 0,
scrollsCount: 2, pagesCount: 2,
pagesCount: 4, particlesCount: 4,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1], pageIndexes: [0, 1],
pageIndexes: [0, 1, 2, 3], particleIndexes: [0, 1, 2, 3],
}, },
}, },
{ {
scrollIndex: -5, pageIndex: -5,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1, 4], pageIndexes: [0, 1, 4],
pageIndexes: [0, 1, 2, 3, 8, 9], particleIndexes: [0, 1, 2, 3, 8, 9],
}, },
}, },
{ {
scrollIndex: 0, pageIndex: 0,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1, 4], pageIndexes: [0, 1, 4],
pageIndexes: [0, 1, 2, 3, 8, 9], particleIndexes: [0, 1, 2, 3, 8, 9],
}, },
}, },
{ {
scrollIndex: 2, pageIndex: 2,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1, 2, 3, 4], pageIndexes: [0, 1, 2, 3, 4],
pageIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], particleIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
}, },
}, },
{ {
scrollIndex: 4, pageIndex: 4,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 3, 4], pageIndexes: [0, 3, 4],
pageIndexes: [0, 1, 6, 7, 8, 9], particleIndexes: [0, 1, 6, 7, 8, 9],
}, },
}, },
{ {
scrollIndex: 15, pageIndex: 15,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 3, 4], pageIndexes: [0, 3, 4],
pageIndexes: [0, 1, 6, 7, 8, 9], particleIndexes: [0, 1, 6, 7, 8, 9],
}, },
}, },
] ]
testCases.forEach(({ testCases.forEach(({
scrollIndex, pageIndex,
scrollsCount,
pagesCount, pagesCount,
pagesToShow, particlesCount,
pagesToScroll, particlesToShow,
particlesToScroll,
expected, expected,
}) => { }) => {
expect(getAdjacentIndexes({ expect(getAdjacentIndexes({
infinite: true, infinite: true,
scrollIndex, pageIndex,
scrollsCount,
pagesCount, pagesCount,
pagesToShow, particlesCount,
pagesToScroll, particlesToShow,
particlesToScroll,
})).toEqual(expected) })).toEqual(expected)
}) })
}) })
@@ -163,87 +163,87 @@ describe('getAdjacentIndexes', () => {
it('returns indexes as expected if it is not infinite', () => { it('returns indexes as expected if it is not infinite', () => {
const testCases = [ const testCases = [
{ {
scrollIndex: 0, pageIndex: 0,
scrollsCount: 2, pagesCount: 2,
pagesCount: 4, particlesCount: 4,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1], pageIndexes: [0, 1],
pageIndexes: [0, 1, 2, 3], particleIndexes: [0, 1, 2, 3],
}, },
}, },
{ {
scrollIndex: -5, pageIndex: -5,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1, 4], pageIndexes: [0, 1, 4],
pageIndexes: [0, 1, 2, 3, 8, 9], particleIndexes: [0, 1, 2, 3, 8, 9],
}, },
}, },
{ {
scrollIndex: 0, pageIndex: 0,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1, 4], pageIndexes: [0, 1, 4],
pageIndexes: [0, 1, 2, 3, 8, 9], particleIndexes: [0, 1, 2, 3, 8, 9],
}, },
}, },
{ {
scrollIndex: 2, pageIndex: 2,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 1, 2, 3, 4], pageIndexes: [0, 1, 2, 3, 4],
pageIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], particleIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
}, },
}, },
{ {
scrollIndex: 4, pageIndex: 4,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 3, 4], pageIndexes: [0, 3, 4],
pageIndexes: [0, 1, 6, 7, 8, 9], particleIndexes: [0, 1, 6, 7, 8, 9],
}, },
}, },
{ {
scrollIndex: 15, pageIndex: 15,
scrollsCount: 5, pagesCount: 5,
pagesCount: 10, particlesCount: 10,
pagesToShow: 2, particlesToShow: 2,
pagesToScroll: 2, particlesToScroll: 2,
expected: { expected: {
scrollIndexes: [0, 3, 4], pageIndexes: [0, 3, 4],
pageIndexes: [0, 1, 6, 7, 8, 9], particleIndexes: [0, 1, 6, 7, 8, 9],
}, },
}, },
] ]
testCases.forEach(({ testCases.forEach(({
scrollIndex, pageIndex,
scrollsCount,
pagesCount, pagesCount,
pagesToShow, particlesCount,
pagesToScroll, particlesToShow,
particlesToScroll,
expected, expected,
}) => { }) => {
expect(getAdjacentIndexes({ expect(getAdjacentIndexes({
infinite: false, infinite: false,
scrollIndex, pageIndex,
scrollsCount,
pagesCount, pagesCount,
pagesToShow, particlesCount,
pagesToScroll, particlesToShow,
particlesToScroll,
})).toEqual(expected) })).toEqual(expected)
}) })
}) })

View File

@@ -6,6 +6,5 @@ export const getDistance = (p1, p2) => {
} }
export function getValueInRange(min, value, max) { export function getValueInRange(min, value, max) {
// if (min > max) throw new Error(`min (${min}) should be more than or equal to max (${max})`)
return Math.max(min, Math.min(value, max)) return Math.max(min, Math.min(value, max))
} }

View File

@@ -1,5 +1,6 @@
import { import {
getDistance, getDistance,
getValueInRange,
} from './math.js' } from './math.js'
describe('getDistance', () => { describe('getDistance', () => {
@@ -17,3 +18,28 @@ describe('getDistance', () => {
expect(getDistance(p1, p2)).toBe(5) expect(getDistance(p1, p2)).toBe(5)
}) })
}) })
describe('getValueInRange', () => {
it('returns value in range as expected', () => {
const testCases = [
{ min: 0, value: -5, max: 3, expected: 0 },
{ min: 0, value: 0, max: 3, expected: 0 },
{ min: 0, value: 1, max: 3, expected: 1 },
{ min: 0, value: 2, max: 3, expected: 2 },
{ min: 0, value: 7, max: 3, expected: 3 },
]
testCases.forEach(({
min,
value,
max,
expected,
}) => {
expect(getValueInRange(
min,
value,
max,
)).toBe(expected)
})
})
})

View File

@@ -2,143 +2,135 @@ import {
getValueInRange, getValueInRange,
} from './math' } from './math'
export function getNextPageIndexLimited({ export function getNextParticleIndexLimited({
currentPageIndex, currentParticleIndex,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
}) { }) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
return currentPageIndex + Math.min(pagesCount - (currentPageIndex + 1) - pagesToScroll, pagesToScroll) return currentParticleIndex + Math.min(particlesCount - (currentParticleIndex + 1) - particlesToScroll, particlesToScroll)
} }
export function getNextPageIndexInfinte({ export function getNextParticleIndexInfinte({
currentPageIndex, currentParticleIndex,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
clonesCountTail, clonesCountTail,
}) { }) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
const newCurrentPageIndex = Math.max(currentPageIndex, 0) + Math.min(pagesCount - clonesCountTail - currentPageIndex, pagesToScroll) const newCurrentParticleIndex = Math.max(currentParticleIndex, 0) + Math.min(particlesCount - clonesCountTail - currentParticleIndex, particlesToScroll)
return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0) return newCurrentParticleIndex > particlesCount - 1 ? 0 : Math.max(newCurrentParticleIndex, 0)
} }
export function getNextPageIndexFn(infinite) { export function getNextParticleIndexFn(infinite) {
return infinite ? getNextPageIndexInfinte : getNextPageIndexLimited return infinite ? getNextParticleIndexInfinte : getNextParticleIndexLimited
} }
export function getPrevPageIndexLimited({ export function getPrevParticleIndexLimited({
currentPageIndex, currentParticleIndex,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
}) { }) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
return getValueInRange( return getValueInRange(
0, 0,
currentPageIndex - Math.min(currentPageIndex, pagesToScroll), currentParticleIndex - Math.min(currentParticleIndex, particlesToScroll),
pagesCount - 1 particlesCount - 1
) )
} }
export function getPrevPageIndexInfinte({ export function getPrevParticleIndexInfinte({
currentPageIndex, currentParticleIndex,
pagesCount, particlesCount,
pagesToScroll, particlesToScroll,
}) { }) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1') if (particlesCount < 1) throw new Error('particlesCount must be at least 1')
const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - Math.min(currentPageIndex, pagesToScroll) const newCurrentParticleIndex = Math.min(currentParticleIndex, particlesCount - 1) - Math.min(currentParticleIndex, particlesToScroll)
return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1 return newCurrentParticleIndex >= 0 ? Math.min(newCurrentParticleIndex, particlesCount - 1) : particlesCount - 1
} }
export function getPrevPageIndexFn(infinite) { export function getPrevParticleIndexFn(infinite) {
return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited return infinite ? getPrevParticleIndexInfinte : getPrevParticleIndexLimited
} }
export function getPageIndex({ export function getSizes({
pageIndex,
pagesCount,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1)
}
export function getPageSizes({
pageWindowElement, pageWindowElement,
pagesContainerChildren, particlesContainerChildren,
pagesToShow, particlesToShow,
}) { }) {
const pagesWindowWidth = pageWindowElement.clientWidth const pageWindowWidth = pageWindowElement.clientWidth
const pageWidth = pagesWindowWidth / pagesToShow const particleWidth = pageWindowWidth / particlesToShow
const pagesCount = pagesContainerChildren.length const particlesCount = particlesContainerChildren.length
return { return {
pagesWindowWidth, pageWindowWidth,
pageWidth, particleWidth,
pagesCount, particlesCount,
} }
} }
export function applyPageSizes({ export function applyParticleSizes({
pagesContainerChildren, particlesContainerChildren,
pageWidth, particleWidth,
}) { }) {
for (let pageIndex=0; pageIndex<pagesContainerChildren.length; pageIndex++) { for (let particleIndex=0; particleIndex<particlesContainerChildren.length; particleIndex++) {
pagesContainerChildren[pageIndex].style.minWidth = `${pageWidth}px` particlesContainerChildren[particleIndex].style.minWidth = `${particleWidth}px`
pagesContainerChildren[pageIndex].style.maxWidth = `${pageWidth}px` particlesContainerChildren[particleIndex].style.maxWidth = `${particleWidth}px`
} }
} }
export function getCurrentScrollIndex({ export function getCurrentPageIndex({
currentPageIndex, currentParticleIndex,
pagesCount, particlesCount,
headClonesCount, headClonesCount,
infinite, infinite,
pagesToScroll, particlesToScroll,
}) { }) {
if (infinite) { if (infinite) {
if (currentPageIndex === pagesCount - headClonesCount) return 0 if (currentParticleIndex === particlesCount - headClonesCount) return 0
if (currentPageIndex === 0) return pagesCount - headClonesCount if (currentParticleIndex === 0) return particlesCount - headClonesCount
return Math.floor((currentPageIndex - headClonesCount) / pagesToScroll) return Math.floor((currentParticleIndex - headClonesCount) / particlesToScroll)
} }
return Math.ceil(currentPageIndex / pagesToScroll) return Math.ceil(currentParticleIndex / particlesToScroll)
} }
// TODO: think about case if pagesCount < pagesToShow and pagesCount < pagesToScroll // TODO: think about case if particlesCount < particlesToShow and particlesCount < particlesToScroll
export function getPartialPageSize({ export function getPartialPageSize({
pagesToScroll, particlesToScroll,
pagesToShow, particlesToShow,
pagesCountWithoutClones pagesCountWithoutClones, // TODO: rename
}) { }) {
const overlap = pagesToScroll - pagesToShow const overlap = particlesToScroll - particlesToShow
let _pages = pagesToShow let particlesCount = particlesToShow
while(true) { while(true) {
const diff = pagesCountWithoutClones - _pages - overlap const diff = pagesCountWithoutClones - particlesCount - overlap
if (diff < pagesToShow) { if (diff < particlesToShow) {
return diff return diff
} }
_pages += pagesToShow + overlap particlesCount += particlesToShow + overlap
} }
} }
export function getScrollsCount({ export function getPagesCount({
infinite, infinite,
pagesCountWithoutClones, pagesCountWithoutClones,
pagesToScroll, particlesToScroll,
}) { }) {
return infinite return infinite
? Math.ceil(pagesCountWithoutClones / pagesToScroll) ? Math.ceil(pagesCountWithoutClones / particlesToScroll)
: Math.round(pagesCountWithoutClones / pagesToScroll) : Math.round(pagesCountWithoutClones / particlesToScroll)
} }
export function getPageIndexByScrollIndex({ export function getParticleIndexByPageIndex({
infinite, infinite,
scrollIndex, pageIndex,
clonesCountHead, clonesCountHead,
pagesToScroll, particlesToScroll,
pagesCount, particlesCount,
pagesToShow, particlesToShow,
}) { }) {
return infinite return infinite
? clonesCountHead + scrollIndex * pagesToScroll ? clonesCountHead + pageIndex * particlesToScroll
: Math.min(scrollIndex * pagesToScroll, pagesCount - pagesToShow) : Math.min(pageIndex * particlesToScroll, particlesCount - particlesToShow)
} }

View File

@@ -1,274 +1,250 @@
import { import {
getNextPageIndexLimited, getNextParticleIndexLimited,
getNextPageIndexInfinte, getNextParticleIndexInfinte,
getPrevPageIndexLimited, getPrevParticleIndexLimited,
getPrevPageIndexInfinte, getPrevParticleIndexInfinte,
getPageIndex,
getAdjacentIndexes, getAdjacentIndexes,
getPartialPageSize,
} from './page.js' } from './page.js'
// describe('getNextPageIndexLimited', () => { // describe('getNextParticleIndexLimited', () => {
// it('returns next page index as expected', () => { // it('returns next page index as expected', () => {
// const testCases = [ // const testCases = [
// { currentPageIndex: -5, pagesCount: 3, expected: 0 }, // { currentParticleIndex: -5, particlesCount: 3, particlesToScroll: 1, expected: 0 },
// { currentPageIndex: 0, pagesCount: 3, expected: 1 }, // { currentParticleIndex: 0, particlesCount: 3, particlesToScroll: 1, expected: 1 },
// { currentPageIndex: 1, pagesCount: 3, expected: 2 }, // { currentParticleIndex: 1, particlesCount: 3, particlesToScroll: 1, expected: 2 },
// { currentPageIndex: 2, pagesCount: 3, expected: 2 }, // { currentParticleIndex: 2, particlesCount: 3, particlesToScroll: 1, expected: 2 },
// { currentPageIndex: 7, pagesCount: 3, expected: 2 }, // { currentParticleIndex: 7, particlesCount: 3, particlesToScroll: 1, expected: 2 },
// ] // ]
// testCases.forEach(({ // testCases.forEach(({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// particlesToScroll,
// expected, // expected,
// }) => { // }) => {
// expect(getNextPageIndexLimited({ // expect(getNextParticleIndexLimited({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// particlesToScroll,
// })).toBe(expected) // })).toBe(expected)
// }) // })
// }) // })
// it('throws error if pagesCount is less than 1', () => { // it('throws error if particlesCount is less than 1', () => {
// const currentPageIndex = 5 // const currentParticleIndex = 5
// const pagesCount = 0 // const particlesCount = 0
// const particlesToScroll = 1
// expect( // expect(
// () => getNextPageIndexLimited({ // () => getNextParticleIndexLimited({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// particlesToScroll,
// }) // })
// ).toThrowError('pagesCount must be at least 1') // ).toThrowError('particlesCount must be at least 1')
// }) // })
// }) // })
// describe('getNextPageIndexInfinte', () => { // describe('getNextParticleIndexInfinte', () => {
// it('returns next page index as expected', () => { // it('returns next page index as expected', () => {
// const testCases = [ // const testCases = [
// { currentPageIndex: -5, pagesCount: 3, expected: 1 }, // { currentParticleIndex: -5, particlesCount: 3, expected: 1 },
// { currentPageIndex: 0, pagesCount: 3, expected: 1 }, // { currentParticleIndex: 0, particlesCount: 3, expected: 1 },
// { currentPageIndex: 1, pagesCount: 3, expected: 2 }, // { currentParticleIndex: 1, particlesCount: 3, expected: 2 },
// { currentPageIndex: 2, pagesCount: 3, expected: 0 }, // { currentParticleIndex: 2, particlesCount: 3, expected: 0 },
// { currentPageIndex: 7, pagesCount: 3, expected: 0 }, // { currentParticleIndex: 7, particlesCount: 3, expected: 0 },
// ] // ]
// testCases.forEach(({ // testCases.forEach(({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// expected, // expected,
// }) => { // }) => {
// expect(getNextPageIndexInfinte({ // expect(getNextParticleIndexInfinte({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// })).toBe(expected) // })).toBe(expected)
// }) // })
// }) // })
// it('throws error if pagesCount is less than 1', () => { // it('throws error if particlesCount is less than 1', () => {
// const currentPageIndex = 5 // const currentParticleIndex = 5
// const pagesCount = 0 // const particlesCount = 0
// expect( // expect(
// () => getNextPageIndexInfinte({ // () => getNextParticleIndexInfinte({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// }) // })
// ).toThrowError('pagesCount must be at least 1') // ).toThrowError('particlesCount must be at least 1')
// }) // })
// }) // })
// describe('getPrevPageIndexLimited', () => { // describe('getPrevParticleIndexLimited', () => {
// it('returns prev page index as expected', () => { // it('returns prev page index as expected', () => {
// const testCases = [ // const testCases = [
// { currentPageIndex: -5, pagesCount: 3, expected: 0 }, // { currentParticleIndex: -5, particlesCount: 3, expected: 0 },
// { currentPageIndex: 0, pagesCount: 3, expected: 0 }, // { currentParticleIndex: 0, particlesCount: 3, expected: 0 },
// { currentPageIndex: 1, pagesCount: 3, expected: 0 }, // { currentParticleIndex: 1, particlesCount: 3, expected: 0 },
// { currentPageIndex: 2, pagesCount: 3, expected: 1 }, // { currentParticleIndex: 2, particlesCount: 3, expected: 1 },
// { currentPageIndex: 7, pagesCount: 3, expected: 2 }, // { currentParticleIndex: 7, particlesCount: 3, expected: 2 },
// ] // ]
// testCases.forEach(({ // testCases.forEach(({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// expected, // expected,
// }) => { // }) => {
// expect(getPrevPageIndexLimited({ // expect(getPrevParticleIndexLimited({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// })).toBe(expected) // })).toBe(expected)
// }) // })
// }) // })
// it('throws error if pagesCount is less than 1', () => { // it('throws error if particlesCount is less than 1', () => {
// const currentPageIndex = 5 // const currentParticleIndex = 5
// const pagesCount = 0 // const particlesCount = 0
// expect( // expect(
// () => getPrevPageIndexLimited({ // () => getPrevParticleIndexLimited({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// }) // })
// ).toThrowError('pagesCount must be at least 1') // ).toThrowError('particlesCount must be at least 1')
// }) // })
// }) // })
// describe('getPrevPageIndexInfinte', () => { // describe('getPrevParticleIndexInfinte', () => {
// it('returns prev page index as expected', () => { // it('returns prev page index as expected', () => {
// const testCases = [ // const testCases = [
// { currentPageIndex: -5, pagesCount: 3, expected: 2 }, // { currentParticleIndex: -5, particlesCount: 3, expected: 2 },
// { currentPageIndex: 0, pagesCount: 3, expected: 2 }, // { currentParticleIndex: 0, particlesCount: 3, expected: 2 },
// { currentPageIndex: 1, pagesCount: 3, expected: 0 }, // { currentParticleIndex: 1, particlesCount: 3, expected: 0 },
// { currentPageIndex: 2, pagesCount: 3, expected: 1 }, // { currentParticleIndex: 2, particlesCount: 3, expected: 1 },
// { currentPageIndex: 7, pagesCount: 3, expected: 1 }, // { currentParticleIndex: 7, particlesCount: 3, expected: 1 },
// ] // ]
// testCases.forEach(({ // testCases.forEach(({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// expected, // expected,
// }) => { // }) => {
// expect(getPrevPageIndexInfinte({ // expect(getPrevParticleIndexInfinte({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// })).toBe(expected) // })).toBe(expected)
// }) // })
// }) // })
// it('throws error if pagesCount is less than 1', () => { // it('throws error if particlesCount is less than 1', () => {
// const currentPageIndex = 5 // const currentParticleIndex = 5
// const pagesCount = 0 // const particlesCount = 0
// expect( // expect(
// () => getPrevPageIndexInfinte({ // () => getPrevParticleIndexInfinte({
// currentPageIndex, // currentParticleIndex,
// pagesCount, // particlesCount,
// }) // })
// ).toThrowError('pagesCount must be at least 1') // ).toThrowError('particlesCount must be at least 1')
// }) // })
// }) // })
// describe('getPageIndex', () => { describe('getPartialPageSize', () => {
// it('returns normalized page index as expected', () => { it('returns result as expected if particlesToShow <= particlesToScroll', () => {
// const testCases = [ const testCases = [
// { pageIndex: -5, pagesCount: 3, expected: 0 }, {
// { pageIndex: 0, pagesCount: 3, expected: 0 }, particlesCountWithoutClones: 9,
// { pageIndex: 1, pagesCount: 3, expected: 1 }, particlesToShow: 2,
// { pageIndex: 2, pagesCount: 3, expected: 2 }, particlesToScroll: 3,
// { pageIndex: 7, pagesCount: 3, expected: 2 }, expected: 0,
// ] }, {
// testCases.forEach(({ particlesCountWithoutClones: 15,
// pageIndex, particlesToShow: 4,
// pagesCount, particlesToScroll: 5,
// expected, expected: 0,
// }) => { }, {
// expect(getPageIndex({ particlesCountWithoutClones: 16,
// pageIndex, particlesToShow: 4,
// pagesCount, particlesToScroll: 5,
// })).toBe(expected) expected: 1,
// }) }, {
// }) particlesCountWithoutClones: 17,
// it('throws error if pagesCount is less than 1', () => { particlesToShow: 4,
// const pageIndex = 5 particlesToScroll: 5,
// const pagesCount = 0 expected: 2,
// expect( }, {
// () => getPageIndex({ particlesCountWithoutClones: 18,
// pageIndex, particlesToShow: 4,
// pagesCount, particlesToScroll: 5,
// }) expected: 3,
// ).toThrowError('pagesCount must be at least 1') }, {
// }) particlesCountWithoutClones: 8,
// }) particlesToShow: 2,
particlesToScroll: 2,
expected: 0,
}
]
// describe('getPartialPageSize', () => { testCases.forEach(({
// it('getPartialPageSize', () => { particlesCountWithoutClones,
// // ==== pagesToShow <= pagesToScroll particlesToShow,
// const r0 = getPartialPageSize({ particlesToScroll,
// pagesCountWithoutClones: 9, expected,
// pagesToShow: 2, }) => {
// pagesToScroll: 3, expect(getPartialPageSize({
// }) particlesCountWithoutClones,
// expect(r0).toBe(0) particlesToShow,
particlesToScroll,
})).toBe(expected)
})
})
// const r1 = getPartialPageSize({ it('returns result as expected if particlesToShow > particlesToScroll', () => {
// pagesCountWithoutClones: 15, const testCases = [
// pagesToShow: 4, {
// pagesToScroll: 5, particlesCountWithoutClones: 8,
// }) particlesToShow: 4,
// expect(r1).toBe(0) particlesToScroll: 2,
expected: 2,
// const r2 = getPartialPageSize({ }, {
// pagesCountWithoutClones: 16, particlesCountWithoutClones: 7,
// pagesToShow: 4, particlesToShow: 4,
// pagesToScroll: 5, particlesToScroll: 3,
// }) expected: 1,
// expect(r2).toBe(1) }, {
particlesCountWithoutClones: 8,
// const r3 = getPartialPageSize({ particlesToShow: 4,
// pagesCountWithoutClones: 17, particlesToScroll: 3,
// pagesToShow: 4, expected: 2,
// pagesToScroll: 5, }, {
// }) particlesCountWithoutClones: 8,
// expect(r3).toBe(2) particlesToShow: 2,
particlesToScroll: 2,
// const r4 = getPartialPageSize({ expected: 0,
// pagesCountWithoutClones: 18, }, {
// pagesToShow: 4, particlesCountWithoutClones: 9,
// pagesToScroll: 5, particlesToShow: 4,
// }) particlesToScroll: 3,
// expect(r4).toBe(3) expected: 3,
}, {
// const r5 = getPartialPageSize({ particlesCountWithoutClones: 8,
// pagesCountWithoutClones: 8, particlesToShow: 3,
// pagesToShow: 2, particlesToScroll: 2,
// pagesToScroll: 2, expected: 2,
// }) }, {
// expect(r5).toBe(0) particlesCountWithoutClones: 6,
particlesToShow: 3,
// // ====== pagesToScroll < pagesToShow particlesToScroll: 1,
expected: 2,
// const r6 = getPartialPageSize({ }, {
// pagesCountWithoutClones: 8, particlesCountWithoutClones: 7,
// pagesToShow: 4, particlesToShow: 3,
// pagesToScroll: 2, particlesToScroll: 1,
// }) expected: 2,
// expect(r6).toBe(2) }
]
// const r7 = getPartialPageSize({
// pagesCountWithoutClones: 7,
// pagesToShow: 4,
// pagesToScroll: 3,
// })
// expect(r7).toBe(1)
// const r8 = getPartialPageSize({
// pagesCountWithoutClones: 8,
// pagesToShow: 4,
// pagesToScroll: 3,
// })
// expect(r8).toBe(2)
// const r9 = getPartialPageSize({
// pagesCountWithoutClones: 8,
// pagesToShow: 2,
// pagesToScroll: 2,
// })
// expect(r9).toBe(0)
// const r10 = getPartialPageSize({
// pagesCountWithoutClones: 9,
// pagesToShow: 4,
// pagesToScroll: 3,
// })
// expect(r10).toBe(3)
// const r11 = getPartialPageSize({
// pagesCountWithoutClones: 8,
// pagesToShow: 3,
// pagesToScroll: 2,
// })
// expect(r11).toBe(2)
// const r12 = getPartialPageSize({
// pagesCountWithoutClones: 6,
// pagesToShow: 3,
// pagesToScroll: 1,
// })
// expect(r12).toBe(2)
// const r13 = getPartialPageSize({
// pagesCountWithoutClones: 7,
// pagesToShow: 3,
// pagesToScroll: 1,
// })
// expect(r13).toBe(2)
// })
// })
testCases.forEach(({
particlesCountWithoutClones,
particlesToShow,
particlesToScroll,
expected,
}) => {
expect(getPartialPageSize({
particlesCountWithoutClones,
particlesToShow,
particlesToScroll,
})).toBe(expected)
})
})
})