Package Exports
- matrix-transpose
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (matrix-transpose) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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 Repl.it.
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 --save
Yarn:
$ yarn add matrix-transpose
CDN:
<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 test
Run tests in watch mode:
$ npm run test:watch
Lint files:
$ npm run lint
Fix lint errors:
$ npm run lint:fix
Release
Only collaborators with credentials can release and publish:
$ npm run release
$ git push --follow-tags && npm publish