navigator.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. export class ArrayNavigator {
  6. constructor(items, start = 0, end = items.length, index = start - 1) {
  7. this.items = items;
  8. this.start = start;
  9. this.end = end;
  10. this.index = index;
  11. }
  12. current() {
  13. if (this.index === this.start - 1 || this.index === this.end) {
  14. return null;
  15. }
  16. return this.items[this.index];
  17. }
  18. next() {
  19. this.index = Math.min(this.index + 1, this.end);
  20. return this.current();
  21. }
  22. previous() {
  23. this.index = Math.max(this.index - 1, this.start - 1);
  24. return this.current();
  25. }
  26. first() {
  27. this.index = this.start;
  28. return this.current();
  29. }
  30. last() {
  31. this.index = this.end - 1;
  32. return this.current();
  33. }
  34. }