Implement a function called "rotate" that will take an array and return a new array with the elements rotated by n spaces.
If n is positive, the array should be rotated to the right. If n is negative, then it should be rotated to the left. If n is 0, the array should remain unchanged.
For example:
var data = [1, 2, 3, 4, 5];
rotate(data, 1) // => [5, 1, 2, 3, 4]
rotate(data, 2) // => [4, 5, 1, 2, 3]
rotate(data, 3) // => [3, 4, 5, 1, 2]
rotate(data, 4) // => [2, 3, 4, 5, 1]
rotate(data, 5) // => [1, 2, 3, 4, 5]
rotate(data, 0) // => [1, 2, 3, 4, 5]
rotate(data, -1) // => [2, 3, 4, 5, 1]
rotate(data, -2) // => [3, 4, 5, 1, 2]
rotate(data, -3) // => [4, 5, 1, 2, 3]
rotate(data, -4) // => [5, 1, 2, 3, 4]
rotate(data, -5) // => [1, 2, 3, 4, 5]
Furthermore, this function should work with any type of array input, not just numbers:
rotate(['a', 'b', 'c'], 1) // => ['c', 'a', 'b']
rotate([1.0, 2.0, 3.0], 1) // => [3.0, 1.0, 2.0]
rotate([true, true, false], 1) // => [false, true, true]
Additionally, the rotation should wrap around if the index exceeds the length of the array:
For example:
var data = [1, 2, 3, 4, 5]
rotate(data, 7) // => [4, 5, 1, 2, 3]
rotate(data, 11) // => [5, 1, 2, 3, 4]
rotate(data, 12478) // => [3, 4, 5, 1, 2]