Skip to content

Instantly share code, notes, and snippets.

@Andaeiii
Last active April 1, 2022 15:40
Show Gist options
  • Save Andaeiii/8d9016134042e9ccf2b942dd89ebf8a8 to your computer and use it in GitHub Desktop.
Save Andaeiii/8d9016134042e9ccf2b942dd89ebf8a8 to your computer and use it in GitHub Desktop.
My pagination construct usinga arrays to build up links... with " . . . . " used to denote existing links before and after the active link
let arr = paginate(index, len);
const paginate = (index, len) => {
let curIndex = (index === null) ? 1 : index;
let prvArr = (curIndex == 1) ? [] : (curIndex == 2) ? [1] : (curIndex == 3) ? [1, 2] : ['..', curIndex - 1];
let nxtArr = (curIndex == len) ? [] : (curIndex == len - 1) ? [len] : (curIndex == len - 2) ? [len - 2, len - 1] : [curIndex + 1, '...'];
return [1, ...prvArr, curIndex, ...nxtArr, len]
.filter((v, i, s) => s.indexOf(v) === i)
.map(v => v === '..' ? '...' : v)
.map(v => v === '...' ? '.....' : v);
}
@Andaeiii
Copy link
Author

provide the given length and the current index, and this code gives you the page index and the

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment