#69 : Split fns
This commit is contained in:
@@ -13,19 +13,22 @@
|
||||
removeResizeEventListener
|
||||
} from '../../utils/event'
|
||||
import {
|
||||
getAdjacentIndexes,
|
||||
getClones,
|
||||
applyClones,
|
||||
getPageSizes,
|
||||
applyPageSizes,
|
||||
getCurrentScrollIndex,
|
||||
getPagesCountWithoutClones,
|
||||
getClonesCount,
|
||||
getPartialPageSize,
|
||||
getScrollsCount,
|
||||
getPageIndexByScrollIndex,
|
||||
getIndexesOfPagesWithoutClonesInScroll,
|
||||
} from '../../utils/page'
|
||||
import {
|
||||
getClones,
|
||||
applyClones,
|
||||
getPagesCountWithoutClones,
|
||||
getClonesCount,
|
||||
} from '../../utils/clones'
|
||||
import {
|
||||
getAdjacentIndexes,
|
||||
} from '../../utils/lazy'
|
||||
import { get } from '../../utils/object'
|
||||
import { ProgressManager } from '../../utils/ProgressManager'
|
||||
import { wait } from '../../utils/interval'
|
||||
@@ -281,8 +284,6 @@
|
||||
await tick()
|
||||
infinite && addClones()
|
||||
|
||||
|
||||
|
||||
// TODO: validate initialScrollIndex
|
||||
store.init(getPageIndexByScrollIndex({
|
||||
infinite,
|
||||
|
||||
55
src/utils/clones.js
Normal file
55
src/utils/clones.js
Normal file
@@ -0,0 +1,55 @@
|
||||
export function getClones({
|
||||
headClonesCount,
|
||||
tailClonesCount,
|
||||
pagesContainerChildren,
|
||||
}) {
|
||||
// TODO: add fns to remove clones if needed
|
||||
const clonesToAppend = []
|
||||
for (let i=0; i<tailClonesCount; i++) {
|
||||
clonesToAppend.push(pagesContainerChildren[i].cloneNode(true))
|
||||
}
|
||||
|
||||
const clonesToPrepend = []
|
||||
const len = pagesContainerChildren.length
|
||||
for (let i=len-1; i>len-1-headClonesCount; i--) {
|
||||
clonesToPrepend.push(pagesContainerChildren[i].cloneNode(true))
|
||||
}
|
||||
|
||||
return {
|
||||
clonesToAppend,
|
||||
clonesToPrepend,
|
||||
}
|
||||
}
|
||||
|
||||
export function applyClones({
|
||||
pagesContainer,
|
||||
clonesToAppend,
|
||||
clonesToPrepend,
|
||||
}) {
|
||||
for (let i=0; i<clonesToAppend.length; i++) {
|
||||
pagesContainer.append(clonesToAppend[i])
|
||||
}
|
||||
for (let i=0; i<clonesToPrepend.length; i++) {
|
||||
pagesContainer.prepend(clonesToPrepend[i])
|
||||
}
|
||||
}
|
||||
|
||||
export function getClonesCount({
|
||||
infinite,
|
||||
pagesToShow,
|
||||
partialPageSize,
|
||||
}) {
|
||||
const clonesCount = infinite
|
||||
? {
|
||||
head: partialPageSize || pagesToShow,
|
||||
tail: pagesToShow,
|
||||
} : {
|
||||
head: 0,
|
||||
tail: 0,
|
||||
}
|
||||
|
||||
return {
|
||||
...clonesCount,
|
||||
total: clonesCount.head + clonesCount.tail,
|
||||
}
|
||||
}
|
||||
61
src/utils/lazy.js
Normal file
61
src/utils/lazy.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import { getValueInRange } from './math'
|
||||
|
||||
export function getIndexesOfPagesWithoutClonesInScroll({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
}) {
|
||||
const overlap = scrollIndex === 0 ? 0 : pagesToShow - pagesToScroll
|
||||
const from = scrollIndex * pagesToShow - scrollIndex * overlap
|
||||
const to = from + Math.max(pagesToShow, pagesToScroll) - 1
|
||||
const indexes = []
|
||||
for (let i=from; i<=Math.min(pagesCount - 1, to); i++) {
|
||||
indexes.push(i)
|
||||
}
|
||||
return indexes
|
||||
}
|
||||
|
||||
export function getAdjacentIndexes({
|
||||
infinite,
|
||||
scrollIndex,
|
||||
scrollsCount,
|
||||
pagesCount,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
}) {
|
||||
const _scrollIndex = getValueInRange(0, scrollIndex, scrollsCount - 1)
|
||||
|
||||
let rangeStart = _scrollIndex - 1
|
||||
let rangeEnd = _scrollIndex + 1
|
||||
|
||||
rangeStart = infinite
|
||||
? rangeStart < 0 ? scrollsCount - 1 : rangeStart
|
||||
: Math.max(0, rangeStart)
|
||||
|
||||
rangeEnd = infinite
|
||||
? rangeEnd > scrollsCount - 1 ? 0 : rangeEnd
|
||||
: Math.min(scrollsCount - 1, rangeEnd)
|
||||
|
||||
const scrollIndexes = [...new Set([
|
||||
rangeStart,
|
||||
_scrollIndex,
|
||||
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
|
||||
])].sort((a, b) => a - b)
|
||||
const pageIndexes = scrollIndexes.flatMap(
|
||||
scrollIndex => getIndexesOfPagesWithoutClonesInScroll({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
})
|
||||
)
|
||||
return {
|
||||
scrollIndexes,
|
||||
pageIndexes: [...new Set(pageIndexes)].sort((a, b) => a - b),
|
||||
}
|
||||
}
|
||||
250
src/utils/lazy.test.js
Normal file
250
src/utils/lazy.test.js
Normal file
@@ -0,0 +1,250 @@
|
||||
import {
|
||||
getIndexesOfPagesWithoutClonesInScroll,
|
||||
getAdjacentIndexes,
|
||||
} from './lazy.js'
|
||||
|
||||
describe('getIndexesOfPagesWithoutClonesInScroll', () => {
|
||||
it('returns correct range if pagesToShow < pagesToScroll', () => {
|
||||
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] },
|
||||
]
|
||||
testCases.forEach(({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getIndexesOfPagesWithoutClonesInScroll({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns correct range if pagesToShow > pagesToScroll', () => {
|
||||
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] },
|
||||
]
|
||||
testCases.forEach(({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getIndexesOfPagesWithoutClonesInScroll({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns correct range if pagesToShow == pagesToScroll', () => {
|
||||
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] },
|
||||
]
|
||||
testCases.forEach(({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getIndexesOfPagesWithoutClonesInScroll({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAdjacentIndexes', () => {
|
||||
it('returns indexes as expected if it is infinite', () => {
|
||||
const testCases = [
|
||||
{
|
||||
scrollIndex: 0,
|
||||
scrollsCount: 2,
|
||||
pagesCount: 4,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1],
|
||||
pageIndexes: [0, 1, 2, 3],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: -5,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1, 4],
|
||||
pageIndexes: [0, 1, 2, 3, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 0,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1, 4],
|
||||
pageIndexes: [0, 1, 2, 3, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 2,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1, 2, 3, 4],
|
||||
pageIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 4,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 3, 4],
|
||||
pageIndexes: [0, 1, 6, 7, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 15,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 3, 4],
|
||||
pageIndexes: [0, 1, 6, 7, 8, 9],
|
||||
},
|
||||
},
|
||||
]
|
||||
testCases.forEach(({
|
||||
scrollIndex,
|
||||
scrollsCount,
|
||||
pagesCount,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getAdjacentIndexes({
|
||||
infinite: true,
|
||||
scrollIndex,
|
||||
scrollsCount,
|
||||
pagesCount,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
|
||||
it('returns indexes as expected if it is not infinite', () => {
|
||||
const testCases = [
|
||||
{
|
||||
scrollIndex: 0,
|
||||
scrollsCount: 2,
|
||||
pagesCount: 4,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1],
|
||||
pageIndexes: [0, 1, 2, 3],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: -5,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1, 4],
|
||||
pageIndexes: [0, 1, 2, 3, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 0,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1, 4],
|
||||
pageIndexes: [0, 1, 2, 3, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 2,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 1, 2, 3, 4],
|
||||
pageIndexes: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 4,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 3, 4],
|
||||
pageIndexes: [0, 1, 6, 7, 8, 9],
|
||||
},
|
||||
},
|
||||
{
|
||||
scrollIndex: 15,
|
||||
scrollsCount: 5,
|
||||
pagesCount: 10,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
expected: {
|
||||
scrollIndexes: [0, 3, 4],
|
||||
pageIndexes: [0, 1, 6, 7, 8, 9],
|
||||
},
|
||||
},
|
||||
]
|
||||
testCases.forEach(({
|
||||
scrollIndex,
|
||||
scrollsCount,
|
||||
pagesCount,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getAdjacentIndexes({
|
||||
infinite: false,
|
||||
scrollIndex,
|
||||
scrollsCount,
|
||||
pagesCount,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -4,3 +4,8 @@ export const getDistance = (p1, p2) => {
|
||||
|
||||
return Math.sqrt((xDist * xDist) + (yDist * yDist));
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
import {
|
||||
getValueInRange,
|
||||
} from './math'
|
||||
|
||||
export function getNextPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
pagesToScroll,
|
||||
}) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
console.log('next', pagesCount, currentPageIndex, pagesCount - currentPageIndex)
|
||||
return currentPageIndex + Math.min(pagesCount - (currentPageIndex+1) - pagesToScroll, pagesToScroll)
|
||||
return currentPageIndex + Math.min(pagesCount - (currentPageIndex + 1) - pagesToScroll, pagesToScroll)
|
||||
}
|
||||
|
||||
export function getNextPageIndexInfinte({
|
||||
@@ -29,12 +32,11 @@ export function getPrevPageIndexLimited({
|
||||
pagesToScroll,
|
||||
}) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
return Math.max(
|
||||
Math.min(
|
||||
currentPageIndex - Math.min(currentPageIndex, pagesToScroll),
|
||||
pagesCount - 1
|
||||
),
|
||||
0)
|
||||
return getValueInRange(
|
||||
0,
|
||||
currentPageIndex - Math.min(currentPageIndex, pagesToScroll),
|
||||
pagesCount - 1
|
||||
)
|
||||
}
|
||||
|
||||
export function getPrevPageIndexInfinte({
|
||||
@@ -59,101 +61,6 @@ export function getPageIndex({
|
||||
return pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1)
|
||||
}
|
||||
|
||||
export function getIndexesOfPagesWithoutClonesInScroll({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
}) {
|
||||
const overlap = scrollIndex === 0 ? 0 : pagesToShow - pagesToScroll
|
||||
const from = scrollIndex * pagesToShow - scrollIndex * overlap
|
||||
const to = from + Math.max(pagesToShow, pagesToScroll) - 1
|
||||
console.log('=======>', from, to)
|
||||
const indexes = []
|
||||
for (let i=from; i<=Math.min(pagesCount - 1, to); i++) {
|
||||
indexes.push(i)
|
||||
}
|
||||
return indexes
|
||||
}
|
||||
|
||||
export function getAdjacentIndexes({
|
||||
infinite,
|
||||
scrollIndex,
|
||||
scrollsCount,
|
||||
pagesCount,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
}) {
|
||||
const _scrollIndex = Math.max(0, Math.min(scrollIndex, scrollsCount - 1))
|
||||
|
||||
let rangeStart = _scrollIndex - 1
|
||||
let rangeEnd = _scrollIndex + 1
|
||||
|
||||
rangeStart = infinite
|
||||
? rangeStart < 0 ? scrollsCount - 1 : rangeStart
|
||||
: Math.max(0, rangeStart)
|
||||
|
||||
rangeEnd = infinite
|
||||
? rangeEnd > scrollsCount - 1 ? 0 : rangeEnd
|
||||
: Math.min(scrollsCount - 1, rangeEnd)
|
||||
|
||||
const scrollIndexes = [...new Set([
|
||||
rangeStart,
|
||||
_scrollIndex,
|
||||
rangeEnd,
|
||||
0, // needed to clone first scroll pages
|
||||
scrollsCount - 1, // needed to clone last scroll pages
|
||||
])].sort((a, b) => a - b)
|
||||
const pageIndexes = scrollIndexes.flatMap(
|
||||
scrollIndex => getIndexesOfPagesWithoutClonesInScroll({
|
||||
scrollIndex,
|
||||
pagesToShow,
|
||||
pagesToScroll,
|
||||
pagesCount,
|
||||
})
|
||||
)
|
||||
return {
|
||||
scrollIndexes,
|
||||
pageIndexes: [...new Set(pageIndexes)].sort((a, b) => a - b),
|
||||
}
|
||||
}
|
||||
|
||||
export function getClones({
|
||||
headClonesCount,
|
||||
tailClonesCount,
|
||||
pagesContainerChildren,
|
||||
}) {
|
||||
// TODO: add fns to remove clones if needed
|
||||
const clonesToAppend = []
|
||||
for (let i=0; i<tailClonesCount; i++) {
|
||||
clonesToAppend.push(pagesContainerChildren[i].cloneNode(true))
|
||||
}
|
||||
|
||||
const clonesToPrepend = []
|
||||
const len = pagesContainerChildren.length
|
||||
for (let i=len-1; i>len-1-headClonesCount; i--) {
|
||||
clonesToPrepend.push(pagesContainerChildren[i].cloneNode(true))
|
||||
}
|
||||
|
||||
return {
|
||||
clonesToAppend,
|
||||
clonesToPrepend,
|
||||
}
|
||||
}
|
||||
|
||||
export function applyClones({
|
||||
pagesContainer,
|
||||
clonesToAppend,
|
||||
clonesToPrepend,
|
||||
}) {
|
||||
for (let i=0; i<clonesToAppend.length; i++) {
|
||||
pagesContainer.append(clonesToAppend[i])
|
||||
}
|
||||
for (let i=0; i<clonesToPrepend.length; i++) {
|
||||
pagesContainer.prepend(clonesToPrepend[i])
|
||||
}
|
||||
}
|
||||
|
||||
export function getPageSizes({
|
||||
pageWindowElement,
|
||||
pagesContainerChildren,
|
||||
@@ -195,40 +102,6 @@ export function getCurrentScrollIndex({
|
||||
return Math.ceil(currentPageIndex / pagesToScroll)
|
||||
}
|
||||
|
||||
export function getPagesCountWithoutClones({
|
||||
pagesCount,
|
||||
infinite,
|
||||
totalClonesCount,
|
||||
pagesToScroll,
|
||||
}) {
|
||||
return Math.max(
|
||||
Math.ceil(
|
||||
(pagesCount - (infinite ? totalClonesCount : 0)) / pagesToScroll
|
||||
),
|
||||
1)
|
||||
}
|
||||
|
||||
export function getClonesCount({
|
||||
infinite,
|
||||
pagesToShow,
|
||||
partialPageSize,
|
||||
}) {
|
||||
// Math.max(pagesToScroll, pagesToShow) // max - show 4, scroll 3, pages 7
|
||||
const clonesCount = infinite
|
||||
? {
|
||||
head: partialPageSize || pagesToShow,
|
||||
tail: pagesToShow,
|
||||
} : {
|
||||
head: 0,
|
||||
tail: 0,
|
||||
}
|
||||
|
||||
return {
|
||||
...clonesCount,
|
||||
total: clonesCount.head + clonesCount.tail,
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: think about case if pagesCount < pagesToShow and pagesCount < pagesToScroll
|
||||
export function getPartialPageSize({
|
||||
pagesToScroll,
|
||||
|
||||
@@ -4,328 +4,271 @@ import {
|
||||
getPrevPageIndexLimited,
|
||||
getPrevPageIndexInfinte,
|
||||
getPageIndex,
|
||||
getAdjacentIndexes
|
||||
getAdjacentIndexes,
|
||||
} from './page.js'
|
||||
|
||||
describe('getNextPageIndexLimited', () => {
|
||||
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 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getNextPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getNextPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
// describe('getNextPageIndexLimited', () => {
|
||||
// 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 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getNextPageIndexLimited({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if pagesCount is less than 1', () => {
|
||||
// const currentPageIndex = 5
|
||||
// const pagesCount = 0
|
||||
// expect(
|
||||
// () => getNextPageIndexLimited({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })
|
||||
// ).toThrowError('pagesCount must be at least 1')
|
||||
// })
|
||||
// })
|
||||
|
||||
describe('getNextPageIndexInfinte', () => {
|
||||
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 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getNextPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getNextPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
// describe('getNextPageIndexInfinte', () => {
|
||||
// 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 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getNextPageIndexInfinte({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if pagesCount is less than 1', () => {
|
||||
// const currentPageIndex = 5
|
||||
// const pagesCount = 0
|
||||
// expect(
|
||||
// () => getNextPageIndexInfinte({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })
|
||||
// ).toThrowError('pagesCount must be at least 1')
|
||||
// })
|
||||
// })
|
||||
|
||||
describe('getPrevPageIndexLimited', () => {
|
||||
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 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getPrevPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPrevPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
// describe('getPrevPageIndexLimited', () => {
|
||||
// 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 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getPrevPageIndexLimited({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if pagesCount is less than 1', () => {
|
||||
// const currentPageIndex = 5
|
||||
// const pagesCount = 0
|
||||
// expect(
|
||||
// () => getPrevPageIndexLimited({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })
|
||||
// ).toThrowError('pagesCount must be at least 1')
|
||||
// })
|
||||
// })
|
||||
|
||||
describe('getPrevPageIndexInfinte', () => {
|
||||
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 },
|
||||
]
|
||||
testCases.forEach(({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getPrevPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})).toBe(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const currentPageIndex = 5
|
||||
const pagesCount = 0
|
||||
expect(
|
||||
() => getPrevPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
// describe('getPrevPageIndexInfinte', () => {
|
||||
// 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 },
|
||||
// ]
|
||||
// testCases.forEach(({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// expected,
|
||||
// }) => {
|
||||
// expect(getPrevPageIndexInfinte({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })).toBe(expected)
|
||||
// })
|
||||
// })
|
||||
// it('throws error if pagesCount is less than 1', () => {
|
||||
// const currentPageIndex = 5
|
||||
// const pagesCount = 0
|
||||
// expect(
|
||||
// () => getPrevPageIndexInfinte({
|
||||
// currentPageIndex,
|
||||
// pagesCount,
|
||||
// })
|
||||
// ).toThrowError('pagesCount 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('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('getAdjacentIndexes', () => {
|
||||
it('returns indexes as expected if infinite', () => {
|
||||
const testCases = [
|
||||
{ pageIndex: 0, pagesCount: 1, expected: [0] },
|
||||
{ pageIndex: -5, pagesCount: 10, expected: [0, 1, 9] },
|
||||
{ pageIndex: 0, pagesCount: 10, expected: [0, 1, 9] },
|
||||
{ pageIndex: 5, pagesCount: 10, expected: [4, 5, 6] },
|
||||
{ pageIndex: 9, pagesCount: 10, expected: [0, 8, 9] },
|
||||
{ pageIndex: 15, pagesCount: 10, expected: [0, 8, 9] },
|
||||
]
|
||||
testCases.forEach(({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getAdjacentIndexes({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
infinite: true,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('returns indexes as expected if not infinite', () => {
|
||||
const testCases = [
|
||||
{ pageIndex: 0, pagesCount: 1, expected: [0] },
|
||||
{ pageIndex: -5, pagesCount: 10, expected: [0, 1] },
|
||||
{ pageIndex: 0, pagesCount: 10, expected: [0, 1] },
|
||||
{ pageIndex: 5, pagesCount: 10, expected: [4, 5, 6] },
|
||||
{ pageIndex: 9, pagesCount: 10, expected: [8, 9] },
|
||||
{ pageIndex: 15, pagesCount: 10, expected: [8, 9] },
|
||||
]
|
||||
testCases.forEach(({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getAdjacentIndexes({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
infinite: false,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
const pageIndex = 5
|
||||
const pagesCount = 0
|
||||
const infinite = true
|
||||
expect(
|
||||
() => getAdjacentIndexes({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
infinite,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
// describe('getPartialPageSize', () => {
|
||||
// it('getPartialPageSize', () => {
|
||||
// // ==== pagesToShow <= pagesToScroll
|
||||
// const r0 = getPartialPageSize({
|
||||
// pagesCountWithoutClones: 9,
|
||||
// pagesToShow: 2,
|
||||
// pagesToScroll: 3,
|
||||
// })
|
||||
// expect(r0).toBe(0)
|
||||
|
||||
describe('getPartialPageSize', () => {
|
||||
it('getPartialPageSize', () => {
|
||||
// ==== pagesToShow <= pagesToScroll
|
||||
const r0 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 9,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 3,
|
||||
})
|
||||
expect(r0).toBe(0)
|
||||
// const r1 = getPartialPageSize({
|
||||
// pagesCountWithoutClones: 15,
|
||||
// pagesToShow: 4,
|
||||
// pagesToScroll: 5,
|
||||
// })
|
||||
// expect(r1).toBe(0)
|
||||
|
||||
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 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 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 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)
|
||||
|
||||
const r5 = getPartialPageSize({
|
||||
pagesCountWithoutClones: 8,
|
||||
pagesToShow: 2,
|
||||
pagesToScroll: 2,
|
||||
})
|
||||
expect(r5).toBe(0)
|
||||
// // ====== pagesToScroll < pagesToShow
|
||||
|
||||
// ====== pagesToScroll < pagesToShow
|
||||
// const r6 = getPartialPageSize({
|
||||
// pagesCountWithoutClones: 8,
|
||||
// pagesToShow: 4,
|
||||
// pagesToScroll: 2,
|
||||
// })
|
||||
// expect(r6).toBe(2)
|
||||
|
||||
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 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 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 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 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 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 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)
|
||||
})
|
||||
})
|
||||
// const r13 = getPartialPageSize({
|
||||
// pagesCountWithoutClones: 7,
|
||||
// pagesToShow: 3,
|
||||
// pagesToScroll: 1,
|
||||
// })
|
||||
// expect(r13).toBe(2)
|
||||
// })
|
||||
// })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user