From 974134beeb64c57c402dc2b285b3a0ba4be5e6d4 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 27 Jan 2021 22:08:41 +0300 Subject: [PATCH] Add pageCount check, add getAdjacentIndexes --- src/components/Carousel/Carousel.svelte | 10 +- src/utils/page.js | 23 +++++ src/utils/page.test.js | 117 +++++++++++++++++++----- 3 files changed, 121 insertions(+), 29 deletions(-) diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index c75f774..569c574 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -9,6 +9,7 @@ addResizeEventListener, removeResizeEventListener } from '../../utils/event' + import { getAdjacentIndexes } from '../../utils/page' const directionFnDescription = { [NEXT]: showNextPage, @@ -60,19 +61,14 @@ let currentPageIndex = 0 $: originalCurrentPageIndex = currentPageIndex - Number(infinite); let pagesCount = 0 - $: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 0) // without clones + $: originalPagesCount = Math.max(pagesCount - (infinite ? 2 : 0), 1) // without clones let pageWidth = 0 let offset = 0 let pageWindowElement let pagesElement // used for lazy loading images, preloaded only current, adjacent and cloanable images - $: loaded = [ - ...new Set([ - 0, originalPagesCount-1, - originalCurrentPageIndex - 1, originalCurrentPageIndex, originalCurrentPageIndex + 1 - ]) - ].filter(index => index >= 0) + $: loaded = getAdjacentIndexes(originalCurrentPageIndex, originalPagesCount) function applyPageSizes() { const children = pagesElement.children diff --git a/src/utils/page.js b/src/utils/page.js index 489c413..389faa4 100644 --- a/src/utils/page.js +++ b/src/utils/page.js @@ -1,8 +1,10 @@ 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) { + 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) } @@ -12,10 +14,12 @@ export function getNextPageIndexFn(infinite) { } 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) { + 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 } @@ -25,5 +29,24 @@ export function getPrevPageIndexFn(infinite) { } 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) { + 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; + let rangeEnd = _pageIndex + 1; + rangeStart = rangeStart < 0 + ? infinite + ? pagesCount - 1 + : 0 + : rangeStart + rangeEnd = rangeEnd > pagesCount - 1 + ? infinite + ? 0 + : pagesCount - 1 + : rangeEnd + return [...new Set([rangeStart, rangeEnd, _pageIndex])].sort((a, b) => a - b) +} diff --git a/src/utils/page.test.js b/src/utils/page.test.js index 1a47877..0e9a2b1 100644 --- a/src/utils/page.test.js +++ b/src/utils/page.test.js @@ -3,8 +3,9 @@ import { getNextPageIndexInfinte, getPrevPageIndexLimited, getPrevPageIndexInfinte, - getPageIndex -} from './page.js'; + getPageIndex, + getAdjacentIndexes +} from './page.js' describe('getNextPageIndexLimited', () => { it('returns next page index as expected', () => { @@ -16,10 +17,17 @@ describe('getNextPageIndexLimited', () => { { currentPageIndex: 7, pagesCount: 3, expected: 2 }, ] testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { - expect(getNextPageIndexLimited(currentPageIndex, pagesCount)).toBe(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', () => { @@ -31,10 +39,17 @@ describe('getNextPageIndexInfinte', () => { { currentPageIndex: 7, pagesCount: 3, expected: 0 }, ] testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { - expect(getNextPageIndexInfinte(currentPageIndex, pagesCount)).toBe(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', () => { @@ -46,10 +61,17 @@ describe('getPrevPageIndexLimited', () => { { currentPageIndex: 7, pagesCount: 3, expected: 2 }, ] testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { - expect(getPrevPageIndexLimited(currentPageIndex, pagesCount)).toBe(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', () => { @@ -61,10 +83,17 @@ describe('getPrevPageIndexInfinte', () => { { currentPageIndex: 7, pagesCount: 3, expected: 1 }, ] testCases.forEach(({ currentPageIndex, pagesCount, expected }) => { - expect(getPrevPageIndexInfinte(currentPageIndex, pagesCount)).toBe(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', () => { @@ -76,7 +105,51 @@ describe('getPageIndex', () => { { pageIndex: 7, pagesCount: 3, expected: 2 }, ] testCases.forEach(({ pageIndex, pagesCount, expected }) => { - expect(getPageIndex(pageIndex, pagesCount)).toBe(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, 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, 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') + }) +})