-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtranspose.ts
More file actions
35 lines (33 loc) · 939 Bytes
/
transpose.ts
File metadata and controls
35 lines (33 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { isConsistent2DArray } from "../array";
import { isArray } from "../base";
/**
* Transposes a two-dimensional array (matrix), effectively swapping its rows and columns.
*
* @example
* ```javascript
* const arr = [
* [1, 2, 3],
* [4, 5, 6],
* [7, 8, 9]
* ];
*
* const result = transpose(arr);
* // [
* // [1, 4, 7],
* // [2, 5, 8],
* // [3, 6, 9]
* // ]
* ```
*
* @param {unknown} arr - The 2D array (matrix) to transpose. Its elements can be of any type.
* @returns {Array<Array<unknown>>} A new 2D array representing the transposed matrix. The elements maintain their original types.
* @throws {@link Error}
* @since 1.0.0
* @version 1.0.0
*/
export function transpose(arr: unknown): Array<Array<unknown>> {
if (!isArray(arr) || !isConsistent2DArray(arr)) {
throw new Error("Input must be a 2D array.");
}
return arr[0].map((_, colIndex) => arr.map((row) => row[colIndex]));
}