JSPM

matrix-transpose

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 47
  • Score
    100M100P100Q62059F
  • License MIT

Transposes a matrix by switching the row and column indices of a multidimensional array.

Package Exports

  • matrix-transpose
  • matrix-transpose/cjs/index.js

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

NPM

NPM version Build Status Coverage Status

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]
]

Replit | JSFiddle

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

Release and publish are automated by Release Please.

License

MIT