incrementListIndex.js 481 B

12345678910111213141516171819
  1. function incrementListIndex(current, dir, opt) {
  2. var len = opt.choices.realLength;
  3. var 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. module.exports = incrementListIndex;