Add pageCount check, add getAdjacentIndexes

This commit is contained in:
Vadim
2021-01-27 22:08:41 +03:00
parent fa8a25c81f
commit 974134beeb
3 changed files with 121 additions and 29 deletions

View File

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

View File

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