Merge branch 'main' into feature/#69_Add-pages-to-show-pages-to-scroll

# Conflicts:
#	src/components/Carousel/Carousel.svelte
#	src/store.js
#	src/utils/page.js
This commit is contained in:
Vadim
2021-09-05 21:37:05 +03:00
4 changed files with 194 additions and 57 deletions

View File

@@ -1,9 +1,17 @@
export function getNextPageIndexLimited(currentPageIndex, pagesCount, pagesToScroll) {
export function getNextPageIndexLimited({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
return Math.min(Math.max(currentPageIndex + pagesToScroll, 0), pagesCount - 1)
}
export function getNextPageIndexInfinte(currentPageIndex, pagesCount, pagesToScroll) {
export function getNextPageIndexInfinte({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
const newCurrentPageIndex = Math.max(currentPageIndex, 0) + pagesToScroll
return newCurrentPageIndex > pagesCount - 1 ? 0 : Math.max(newCurrentPageIndex, 0)
@@ -13,12 +21,20 @@ export function getNextPageIndexFn(infinite) {
return infinite ? getNextPageIndexInfinte : getNextPageIndexLimited
}
export function getPrevPageIndexLimited(currentPageIndex, pagesCount, pagesToScroll) {
export function getPrevPageIndexLimited({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
return Math.max(Math.min(currentPageIndex - pagesToScroll, pagesCount - 1), 0)
}
export function getPrevPageIndexInfinte(currentPageIndex, pagesCount, pagesToScroll) {
export function getPrevPageIndexInfinte({
currentPageIndex,
pagesCount,
pagesToScroll,
}) {
if (pagesCount < 1) throw new Error('pagesCount must be at least 1')
const newCurrentPageIndex = Math.min(currentPageIndex, pagesCount - 1) - pagesToScroll
return newCurrentPageIndex >= 0 ? Math.min(newCurrentPageIndex, pagesCount - 1) : pagesCount - 1
@@ -28,12 +44,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;
@@ -53,9 +76,9 @@ export function getAdjacentIndexes(pageIndex, pagesCount, infinite) {
export function getClones({
oneSideClonesCount,
pagesContainerChildren
pagesContainerChildren,
}) {
// TODO: add fns to remove clones
// TODO: add fns to remove clones if needed
const clonesToAppend = []
for (let i=0; i<oneSideClonesCount; i++) {
clonesToAppend.push(pagesContainerChildren[i].cloneNode(true))
@@ -141,10 +164,12 @@ export function getPagesCountWithoutClones({
1)
}
// TODO: check
export function getOneSideClonesCount({
infinite,
pagesToScroll,
pagesToShow,
}) {
return Math.max(pagesToScroll, pagesToShow) // max - show 4, scroll 3, pages 7
return infinite
? Math.max(pagesToScroll, pagesToShow) // max - show 4, scroll 3, pages 7
: 0
}

View File

@@ -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')
})
})