urlWindow function

List<int?> urlWindow(
  1. int current,
  2. int last, [
  3. int onEachSide = 3
])

Laravel's UrlWindow: all pages when there are fewer than onEachSide * 2 + 8, otherwise the first/last two pages and a slider around current. null stands for a ... separator.

Implementation

List<int?> urlWindow(int current, int last, [int onEachSide = 3]) {
  List<int> range(int from, int to) => [for (var p = from; p <= to; p++) p];
  if (last < onEachSide * 2 + 8) {
    return range(1, last);
  }
  final window = onEachSide + 4;
  if (current <= window) {
    return [...range(1, window + onEachSide), null, last - 1, last];
  }
  if (current > last - window) {
    return [1, 2, null, ...range(last - (window + (onEachSide - 1)), last)];
  }
  return [
    1,
    2,
    null,
    ...range(current - onEachSide, current + onEachSide),
    null,
    last - 1,
    last,
  ];
}