utils: array util functions (any types)

This commit is contained in:
Chesterkxng
2025-04-10 18:34:04 +02:00
parent caae769a69
commit c4d78a6c0c

26
src/utils/array.ts Normal file
View File

@@ -0,0 +1,26 @@
/**
* Transpose a 2D array (matrix).
* @param {any[][]} matrix - The 2D array to transpose.
* @returns {any[][]} - The transposed 2D array.
**/
export function transpose<T>(matrix: T[][]): any[][] {
return matrix[0].map((_, colIndex) => matrix.map((row) => row[colIndex]));
}
/**
* Normalize and fill a 2D array to ensure all rows have the same length.
* @param {any[][]} matrix - The 2D array to normalize and fill.
* @param {any} fillValue - The value to fill in for missing elements.
* @returns {any[][]} - The normalized and filled 2D array.
* **/
export function normalizeAndFill<T>(matrix: T[][], fillValue: T): T[][] {
const maxLength = Math.max(...matrix.map((row) => row.length));
return matrix.map((row) => {
const filledRow = [...row];
while (filledRow.length < maxLength) {
filledRow.push(fillValue);
}
return filledRow;
});
}