From b42d27208061b18bbb61f2eab3015b7b32dc2638 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 8 Sep 2021 18:28:48 +0300 Subject: [PATCH] #69 : Code cleanup --- src/components/Carousel/Carousel.svelte | 4 +- src/utils/math.js | 49 ----------- src/utils/math.test.js | 112 ------------------------ src/utils/page.js | 23 +++-- src/utils/page.test.js | 106 ++++++++++++++++++++++ 5 files changed, 125 insertions(+), 169 deletions(-) diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index 9ca96ac..6402f4c 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -21,11 +21,11 @@ getCurrentPageIndexWithoutClones, getPagesCountWithoutClones, getClonesCount, + getPartialPageSize, } from '../../utils/page' import { get } from '../../utils/object' import { ProgressManager } from '../../utils/ProgressManager' import { wait } from '../../utils/interval' - import { getIsOdd, getPartialPageSize } from '../../utils/math' const dispatch = createEventDispatcher() @@ -210,7 +210,6 @@ } function addClones() { - console.log('addClones', clonesCount) const { clonesToAppend, clonesToPrepend, @@ -250,7 +249,6 @@ await tick() cleanupFns.push(store.subscribe(value => { currentPageIndex = value.currentPageIndex - console.log('currentPageIndex', currentPageIndex) })) cleanupFns.push(() => progressManager.reset()) if (pagesContainer && pageWindowElement) { diff --git a/src/utils/math.js b/src/utils/math.js index f9f67b6..3ecae8f 100644 --- a/src/utils/math.js +++ b/src/utils/math.js @@ -4,52 +4,3 @@ export const getDistance = (p1, p2) => { return Math.sqrt((xDist * xDist) + (yDist * yDist)); } - -// TODO: determine when partial offset is needed and apply partial offset -// TODO: determine how many clones should be from both sides - - -// TODO: refactor pagesToShow <= pagesToScroll -// TODO: think about case if pagesCount < pagesToShow and pagesCount < pagesToScroll - -// TODO: math to page -export function getPartialPageSize({ - pagesToScroll, - pagesToShow, - pagesCountWithoutClones -}) { - const overlap = pagesToScroll - pagesToShow - let _pages = pagesToShow - - while(true) { - const diff = pagesCountWithoutClones - _pages - overlap - if (diff < pagesToShow) { - return diff - } - _pages += pagesToShow + overlap - } - - - // if (pagesToShow <= pagesToScroll) { - // const overlap = pagesToScroll - pagesToShow - // let _pages = pagesToShow - - // while(true) { - // const diff = pagesCountWithoutClones - _pages - overlap - // if (diff < pagesToShow) { - // return diff - // } - // _pages += pagesToShow + overlap - // } - // } else { - // const overlap = pagesToShow - pagesToScroll - // let _pages = pagesToShow - // while(true) { - // const diff = pagesCountWithoutClones - _pages + overlap - // if (diff < pagesToShow) { - // return diff - // } - // _pages += pagesToShow - overlap - // } - // } -} diff --git a/src/utils/math.test.js b/src/utils/math.test.js index 5273fb1..1307b7c 100644 --- a/src/utils/math.test.js +++ b/src/utils/math.test.js @@ -1,6 +1,5 @@ import { getDistance, - getPartialPageSize } from './math.js' describe('getDistance', () => { @@ -18,114 +17,3 @@ describe('getDistance', () => { expect(getDistance(p1, p2)).toBe(5) }) }) - -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 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) - - - - }) -}) diff --git a/src/utils/page.js b/src/utils/page.js index 875fd32..ac14ff3 100644 --- a/src/utils/page.js +++ b/src/utils/page.js @@ -104,8 +104,6 @@ export function applyClones({ clonesToAppend, clonesToPrepend, }) { - console.log('clonesToPrepend', clonesToPrepend.length) - console.log('clonesToAppend', clonesToAppend.length) for (let i=0; i { ).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) + + 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) + }) +}) +