It is common to need to make a page selection interface. It typically looks like this:
prev 1 ... 3 4 [5] 6 7 ... 173 next
Which means there are totally 173 pages, and you are on the 5th page currently.
This challenge requires you take the total number of pages and the current page number as input, and output a string (or an array) to "display" the page selector.
Input
2 positive integers
- current page number
- count of pages
It is guaranteed that 1 <= current <= total.
Output
Output a string or an array which represent the ui for page selectors.
- If output as string, a single space (U+0020) should be used between each pages.
- If output as an array, the array should produce same result as the string after convert each item to string and join them with a single space.
- The three dots (
...
) is not optional for array output.
- The three dots (
Details
- If current == 1, no "prev" will be outputted, otherwise, "prev" comes first.
- If current == total, no "next" will be outputted, otherwise, "next" comes last.
- The first page (1) and the last page (total) should always be outputted.
- The current page, (current - 1) page, (current - 2) page, (current + 1) page, (current + 2) page should be outputted as long as the are in the range of [1..total].
- No other pages numbers should be outputted.
- Pages outputted should be sorted in ascending order.
- Output should not contain duplicate page numbers.
- Current page should be highlighted by wrap it in a pair of
[]
. - If there is a gap between any neighbors, three dots (
...
) should be inserted.
Test Cases
Current Total Output
1 1 [1]
1 2 [1] 2 next
1 10 [1] 2 3 ... 10 next
3 3 prev 1 2 [3]
3 6 prev 1 2 [3] 4 5 6 next
4 6 prev 1 2 3 [4] 5 6 next
4 7 prev 1 2 3 [4] 5 6 7 next
3 10 prev 1 2 [3] 4 5 ... 10 next
5 10 prev 1 ... 3 4 [5] 6 7 ... 10 next
10 10 prev 1 ... 8 9 [10]
52 173 prev 1 ... 50 51 [52] 53 54 ... 173 next
Rules
- This is code-golf, the shortest code wins!
4 , 6
as a test case. Similar to the3, 6
case, but ensures dots are not added to the left side. \$\endgroup\$4,7
test case would be greatly appreciated - would insure that both of the boundary cases that exclude ellipses may be concurrently met \$\endgroup\$