From dc0a623c7f2b367d8805a95a5643720e03ca2249 Mon Sep 17 00:00:00 2001 From: Vadim Date: Wed, 8 Sep 2021 18:19:56 +0300 Subject: [PATCH] #69 : Update getPartialPageSize --- src/utils/math.js | 80 +++++++++------------- src/utils/math.test.js | 147 +++++++++++++++++++++-------------------- 2 files changed, 109 insertions(+), 118 deletions(-) diff --git a/src/utils/math.js b/src/utils/math.js index 00b60ab..f9f67b6 100644 --- a/src/utils/math.js +++ b/src/utils/math.js @@ -5,12 +5,6 @@ export const getDistance = (p1, p2) => { return Math.sqrt((xDist * xDist) + (yDist * yDist)); } -// 3 => true -export function getIsOdd(num) { - return Boolean(num % 2) -} - - // TODO: determine when partial offset is needed and apply partial offset // TODO: determine how many clones should be from both sides @@ -24,48 +18,38 @@ export function getPartialPageSize({ pagesToShow, pagesCountWithoutClones }) { + const overlap = pagesToScroll - pagesToShow + let _pages = pagesToShow - console.log('getPartialPageSize ==>', pagesToScroll, pagesToShow, pagesCountWithoutClones) - - if (pagesToShow <= pagesToScroll) { - const overlap = pagesToShow - pagesToScroll - - const d = (Math.max(pagesToShow-pagesToScroll), pagesToShow) - // console.log('overlap', overlap) - let _pages = d - // console.log('pages', _pages) - - while(true) { - // let fp = _pages - overlap - _pages = _pages - overlap + d - // console.log('pages', _pages) - const diff = pagesCountWithoutClones - _pages - if (diff < pagesToShow) { - // console.log('diff', diff) - // console.log('pagesToShow - diff', pagesToShow - Math.abs(diff) - overlap) - const res = diff + overlap - if (res < 0) { - return pagesToShow - overlap + res - } - console.log(res, diff) - return diff !== 0 ? Math.min(res, diff) : res - } - } - } else { - const overlap = pagesToShow - pagesToScroll - - // console.log('overlap', overlap) - let _pages = pagesToShow - console.log('pages', _pages) - - while(true) { - const diff = pagesCountWithoutClones - _pages + overlap - if (diff < pagesToShow) { - return diff - } - _pages += pagesToShow - overlap - console.log('pages', _pages) + 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 c46d06f..5273fb1 100644 --- a/src/utils/math.test.js +++ b/src/utils/math.test.js @@ -23,96 +23,103 @@ describe('getPartialPageSize', () => { it('getPartialPageSize', () => { // ==== pagesToShow <= pagesToScroll - // const r1 = getPartialPageSize({ - // pagesCount: 15, - // pagesToShow: 4, - // pagesToScroll: 5, - // }) - // expect(r1).toBe(0) + const r0 = getPartialPageSize({ + pagesCountWithoutClones: 9, + pagesToShow: 2, + pagesToScroll: 3, + }) + expect(r0).toBe(0) - // const r2 = getPartialPageSize({ - // pagesCount: 16, - // pagesToShow: 4, - // pagesToScroll: 5, - // }) - // expect(r2).toBe(1) + const r1 = getPartialPageSize({ + pagesCountWithoutClones: 15, + pagesToShow: 4, + pagesToScroll: 5, + }) + expect(r1).toBe(0) - // const r3 = getPartialPageSize({ - // pagesCount: 17, - // pagesToShow: 4, - // pagesToScroll: 5, - // }) - // expect(r3).toBe(2) + const r2 = getPartialPageSize({ + pagesCountWithoutClones: 16, + pagesToShow: 4, + pagesToScroll: 5, + }) + expect(r2).toBe(1) - // const r4 = getPartialPageSize({ - // pagesCount: 18, - // pagesToShow: 4, - // pagesToScroll: 5, - // }) - // expect(r4).toBe(3) + const r3 = getPartialPageSize({ + pagesCountWithoutClones: 17, + pagesToShow: 4, + pagesToScroll: 5, + }) + expect(r3).toBe(2) - // const r5 = getPartialPageSize({ - // pagesCount: 8, - // pagesToShow: 2, - // pagesToScroll: 2, - // }) - // expect(r5).toBe(0) + 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({ - // pagesCount: 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({ - // pagesCount: 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({ - // pagesCount: 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({ - // pagesCount: 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({ - // pagesCount: 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({ - // pagesCount: 8, - // pagesToShow: 3, - // pagesToScroll: 2, - // }) - // expect(r11).toBe(2) - const r12 = getPartialPageSize({ - pagesCount: 6, + pagesCountWithoutClones: 6, pagesToShow: 3, pagesToScroll: 1, }) expect(r12).toBe(2) - + const r13 = getPartialPageSize({ - pagesCount: 7, + pagesCountWithoutClones: 7, pagesToShow: 3, pagesToScroll: 1, })