Move logic to fns
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
export function getNextPageIndexLimited(currentPageIndex, pagesCount) {
|
||||
export function getNextPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
}) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
return Math.min(Math.max(currentPageIndex + 1, 0), pagesCount - 1)
|
||||
}
|
||||
|
||||
export function getNextPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||
export function getNextPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
}) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
const newCurrentPageIndex = Math.max(currentPageIndex, 0) + 1
|
||||
return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0)
|
||||
@@ -13,12 +19,18 @@ export function getNextPageIndexFn(infinite) {
|
||||
return infinite ? getNextPageIndexInfinte : getNextPageIndexLimited
|
||||
}
|
||||
|
||||
export function getPrevPageIndexLimited(currentPageIndex, pagesCount) {
|
||||
export function getPrevPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
}) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
return Math.max(Math.min(currentPageIndex - 1, pagesCount - 1), 0)
|
||||
}
|
||||
|
||||
export function getPrevPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||
export function getPrevPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
}) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - 1
|
||||
return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1
|
||||
@@ -28,12 +40,19 @@ export function getPrevPageIndexFn(infinite) {
|
||||
return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited
|
||||
}
|
||||
|
||||
export function getPageIndex(pageIndex, pagesCount) {
|
||||
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 getAdjacentIndexes(pageIndex, pagesCount, infinite) {
|
||||
export function getAdjacentIndexes({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
infinite,
|
||||
}) {
|
||||
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
|
||||
const _pageIndex = Math.max(0, Math.min(pageIndex, pagesCount - 1))
|
||||
let rangeStart = _pageIndex - 1;
|
||||
@@ -50,3 +69,91 @@ export function getAdjacentIndexes(pageIndex, pagesCount, infinite) {
|
||||
: rangeEnd
|
||||
return [...new Set([rangeStart, rangeEnd, _pageIndex])].sort((a, b) => a - b)
|
||||
}
|
||||
|
||||
export function getClones({
|
||||
oneSideClonesCount,
|
||||
pagesContainerChildren,
|
||||
}) {
|
||||
// TODO: add fns to remove clones if needed
|
||||
const clonesToAppend = []
|
||||
for (let i=0; i<oneSideClonesCount; i++) {
|
||||
clonesToAppend.push(pagesContainerChildren[i].cloneNode(true))
|
||||
}
|
||||
|
||||
const clonesToPrepend = []
|
||||
const len = pagesContainerChildren.length
|
||||
for (let i=len-1; i>len-1-oneSideClonesCount; 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,
|
||||
}) {
|
||||
const pagesWindowWidth = pageWindowElement.clientWidth
|
||||
const pageWidth = pagesWindowWidth
|
||||
const pagesCount = pagesContainerChildren.length
|
||||
|
||||
return {
|
||||
pagesWindowWidth,
|
||||
pageWidth,
|
||||
pagesCount,
|
||||
}
|
||||
}
|
||||
|
||||
export function applyPageSizes({
|
||||
pagesContainerChildren,
|
||||
pageWidth,
|
||||
}) {
|
||||
for (let pageIndex=0; pageIndex<pagesContainerChildren.length; pageIndex++) {
|
||||
pagesContainerChildren[pageIndex].style.minWidth = `${pageWidth}px`
|
||||
pagesContainerChildren[pageIndex].style.maxWidth = `${pageWidth}px`
|
||||
}
|
||||
}
|
||||
|
||||
export function getCurrentPageIndexWithoutClones({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
oneSideClonesCount,
|
||||
infinite,
|
||||
}) {
|
||||
if (infinite) {
|
||||
if (currentPageIndex === pagesCount - 1) return 0
|
||||
if (currentPageIndex === 0) return (pagesCount - oneSideClonesCount * 2) - 1
|
||||
return currentPageIndex - 1
|
||||
}
|
||||
return currentPageIndex
|
||||
}
|
||||
|
||||
export function getPagesCountWithoutClones({
|
||||
pagesCount,
|
||||
oneSideClonesCount,
|
||||
}) {
|
||||
const bothSidesClonesCount = oneSideClonesCount * 2
|
||||
return Math.max(pagesCount - bothSidesClonesCount, 1)
|
||||
}
|
||||
|
||||
export function getOneSideClonesCount({
|
||||
infinite,
|
||||
}) {
|
||||
return infinite ? 1 : 0
|
||||
}
|
||||
|
||||
@@ -16,15 +16,25 @@ describe('getNextPageIndexLimited', () => {
|
||||
{ currentPageIndex: 2, pagesCount: 3, expected: 2 },
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getNextPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected)
|
||||
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)
|
||||
() => getNextPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
@@ -38,15 +48,25 @@ describe('getNextPageIndexInfinte', () => {
|
||||
{ currentPageIndex: 2, pagesCount: 3, expected: 0 },
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 0 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getNextPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected)
|
||||
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)
|
||||
() => getNextPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
@@ -60,15 +80,25 @@ describe('getPrevPageIndexLimited', () => {
|
||||
{ currentPageIndex: 2, pagesCount: 3, expected: 1 },
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getPrevPageIndexLimited(currentPageIndex, pagesCount)).toBe(expected)
|
||||
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)
|
||||
() => getPrevPageIndexLimited({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
@@ -82,15 +112,25 @@ describe('getPrevPageIndexInfinte', () => {
|
||||
{ currentPageIndex: 2, pagesCount: 3, expected: 1 },
|
||||
{ currentPageIndex: 7, pagesCount: 3, expected: 1 },
|
||||
]
|
||||
testCases.forEach(({ currentPageIndex, pagesCount, expected }) => {
|
||||
expect(getPrevPageIndexInfinte(currentPageIndex, pagesCount)).toBe(expected)
|
||||
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)
|
||||
() => getPrevPageIndexInfinte({
|
||||
currentPageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
@@ -104,15 +144,25 @@ describe('getPageIndex', () => {
|
||||
{ pageIndex: 2, pagesCount: 3, expected: 2 },
|
||||
{ pageIndex: 7, pagesCount: 3, expected: 2 },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getPageIndex(pageIndex, pagesCount)).toBe(expected)
|
||||
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)
|
||||
() => getPageIndex({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
@@ -127,8 +177,16 @@ describe('getAdjacentIndexes', () => {
|
||||
{ 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, true)).toEqual(expected)
|
||||
testCases.forEach(({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getAdjacentIndexes({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
infinite: true,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('returns indexes as expected if not infinite', () => {
|
||||
@@ -140,8 +198,16 @@ describe('getAdjacentIndexes', () => {
|
||||
{ pageIndex: 9, pagesCount: 10, expected: [8, 9] },
|
||||
{ pageIndex: 15, pagesCount: 10, expected: [8, 9] },
|
||||
]
|
||||
testCases.forEach(({ pageIndex, pagesCount, expected }) => {
|
||||
expect(getAdjacentIndexes(pageIndex, pagesCount, false)).toEqual(expected)
|
||||
testCases.forEach(({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
expected,
|
||||
}) => {
|
||||
expect(getAdjacentIndexes({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
infinite: false,
|
||||
})).toEqual(expected)
|
||||
})
|
||||
})
|
||||
it('throws error if pagesCount is less than 1', () => {
|
||||
@@ -149,7 +215,11 @@ describe('getAdjacentIndexes', () => {
|
||||
const pagesCount = 0
|
||||
const infinite = true
|
||||
expect(
|
||||
() => getAdjacentIndexes(pageIndex, pagesCount, infinite)
|
||||
() => getAdjacentIndexes({
|
||||
pageIndex,
|
||||
pagesCount,
|
||||
infinite,
|
||||
})
|
||||
).toThrowError('pagesCount must be at least 1')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user