UNPKG

485 BJavaScriptView Raw
1function incrementListIndex(current, dir, opt) {
2 const len = opt.choices.realLength;
3 const shouldLoop = 'loop' in opt ? Boolean(opt.loop) : true;
4 if (dir === 'up') {
5 if (current > 0) {
6 return current - 1;
7 }
8 return shouldLoop ? len - 1 : current;
9 }
10 if (dir === 'down') {
11 if (current < len - 1) {
12 return current + 1;
13 }
14 return shouldLoop ? 0 : current;
15 }
16 throw new Error('dir must be up or down');
17}
18
19module.exports = incrementListIndex;