You should definitely keep the index instead of a literal for what you called a cycle list:
class CycleList {
constructor(array) {
this.array = array;
this.value = 0;
}
get curr() {
return this.array[this.value %% this.array.length];
}
get next() {
return this.array[++this.value %% this.array.length];
}
get prev() {
return this.array[--this.value %% this.array.length];
}
step(offset) {
return this.array[(this.value += offset) %% this.array.length];
}
*[Symbol.iterator]() {
while (true) {
yield this.next;
}
}
}
const list = new CycleList(["ascending", "descending", "unspecified"]);
list.curr; // "ascending"
list.next; // "descending"
list.step(-2); // "unspecified"
Notice that it uses the proposed Modulo Operator %%.