#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

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

View File

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

View File

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

View File

@@ -6,6 +6,5 @@ export const getDistance = (p1, p2) => {
}
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))
}

View File

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

View File

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