1.0.3 • Published 1 year ago
matrix-transpose v1.0.3
matrix-transpose
Transposes a matrix by switching the row and column indices of a multidimensional array:
transpose(array)In other words, it flips a matrix over its diagonal. Inspired by the Replit.
Example
const { transpose } = require('matrix-transpose');
transpose([
[1, 2],
[3, 4],
[5, 6],
]);Output:
[
[1, 3, 5],
[2, 4, 6]
]Install
NPM:
npm install matrix-transpose --saveYarn:
yarn add matrix-transposeCDN:
<script src="https://unpkg.com/matrix-transpose@latest/umd/matrix-transpose.min.js"></script>
<script>
window.MatrixTranspose.transpose(/* array */);
</script>Usage
Import module:
// ES Modules
import { transpose } from 'matrix-transpose';
// CommonJS
const { transpose } = require('matrix-transpose');Transpose matrix:
transpose([
[1, 2],
[3, 4],
[5, 6],
]);Output:
[
[1, 3, 5],
[2, 4, 6]
]Transpose matrix with inconsistent column lengths:
transpose([[1], [2, 3], [4, 5, 6]]);Output:
[
[1, 2, 4],
[, 3, 5],
[, , 6]
]Options
excludeEmpty
When option excludeEmpty is set to true, then empty items are excluded:
transpose([[1], [2, 3], [4, 5, 6]], { excludeEmpty: true });Output:
[[1, 2, 4], [3, 5], [6]]Testing
Run tests with coverage:
npm testRun tests in watch mode:
npm run test:watchLint files:
npm run lintFix lint errors:
npm run lint:fixRelease
Release and publish are automated by Release Please.
