Function cycle

Cycle through the contents of an array. By default starts at index 0.

const c = arrayCycle([`apples`, `oranges`, `pears`]);
c.current; // `apples`
c.next(); // `oranges`
c.next(); // `pears`
c.next(); // `apples`
c.prev(); // `pears`

You can select an item by index or value:

c.select(1); // `oranges`
c.select(`pears`); // `pears`

Other features:

c.current;   // Current value
c.toArray(); // Copy of array being cycled over

Additional info:

  • Selecting by value uses === semantics.
  • Works with a copy of input array
  • Type Parameters

    • T

    Parameters

    • options: readonly T[] | T[]

      Array to cycle over

    Returns {
        next: (() => T);
        prev: (() => T);
        select: ((indexOrValue: number | T) => void);
        toArray: (() => T[]);
        get current(): T;
    }

    • next: (() => T)
        • (): T
        • Returns T

    • prev: (() => T)
        • (): T
        • Returns T

    • select: ((indexOrValue: number | T) => void)
        • (indexOrValue): void
        • Parameters

          • indexOrValue: number | T

          Returns void

    • toArray: (() => T[])
        • (): T[]
        • Returns T[]

    • get current(): T