common-utility-functions
    Preparing search index...

    Function wrapSlice

    • Creates a slice of an array with wrapping behavior for indices that exceed array bounds. Unlike the standard Array.slice(), this function treats the array as circular, wrapping around to the beginning when indices exceed the array length, and wrapping to the end when indices are negative.

      Type Parameters

      • T

      Parameters

      • arr: T[]

        The array to slice from

      • startIndex: number

        The starting index (inclusive). Can be negative or exceed array length

      • endIndex: number

        The ending index (inclusive). Can be negative or exceed array length

      Returns T[]

      A new array containing elements from startIndex to endIndex with wrapping

      const numbers = [1, 2, 3, 4, 5];

      // Normal slice within bounds
      wrapSlice(numbers, 1, 3); // [2, 3, 4]

      // Wrapping beyond array length
      wrapSlice(numbers, 3, 7); // [4, 5, 1, 2, 3]

      // Negative indices wrap to end
      wrapSlice(numbers, -2, 1); // [4, 5, 1, 2]

      // Single element
      wrapSlice(numbers, 0, 0); // [1]

      // Wrapping multiple times
      wrapSlice(numbers, 0, 9); // [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

      1.0.0