npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

matrix-slicer

v2.8.0

Published

Slices a matrices into columns, rows, diagonals and submatrices.

Downloads

79

Readme

matrix-slicer

Bower version npm version devDependency Status Travis Build Status Appveyor Build Status Coverage Status

JavaScript utility for slicing a matrix into elements, columns, rows, diagonals and submatrices.

  1. Installation
  2. Usage
  3. Methods
  4. Test
  5. License

Installation

Npm

$ npm install matrix-slicer --save

Yarn

$ yarn add matrix-slicer

Bower

$ bower install matrix-slicer --save

Usage

Javascript

Install Bower package or just put the script in your project.

<script src="/lib/matrix-slicer/index.js"></script>
<!-- In case of Bower: <script src="/bower_components/matrix-slicer/index.js"></script> -->
<script>
    var m = new Matrix([
        [1, 2],
        [3, 4],
    ]);
</script>

AMD

Install Bower package or just put the script in your project.

require([
    'matrix-slicer',
], function (Matrix) {
    var m = new Matrix([
        [1, 2],
        [3, 4],
    ]);
});

CommonJS

Install npm package and use it.

var Matrix = require('matrix-slicer');

var m = new Matrix([
    [1, 2],
    [3, 4],
]);

ES6

Install npm package and use it.

import Matrix from 'matrix-slicer';

const m = new Matrix([
    [1, 2],
    [3, 4],
]);

Methods

1. Creating instance of the Matrix

Syntax

new Matrix(matrix | width, height[, element = 0 | callback])

Where:

  • matrix - regular matrix (an array of arrays with similar lengths);
  • width - number of columns;
  • height - number of rows;
  • element - an element with which the matrix will be filled. By default, this is 0;
  • callback - function that produces an element of the matrix, taking three arguments:
  • i - index (zero-based) of the column of generated the element;
  • j - index (zero-based) of the row of generated the element;
  • m - the width of generated matrix was passed earlier;
  • n - the height of generated matrix was passed earlier;
  • matrix - the matrix with previously generated elements.

Example

// Regular matrix
const m = new Matrix([
    ['bird', 'dog'],
    ['cat', 'elephant'],
]); // => instance of matrix [['bird', 'dog'], ['cat', 'elephant']]

// By dimensions
const m = new Matrix(3, 2); // => instance of matrix [[0, 0, 0], [0, 0, 0]]

// By dimensions and filler
const m = new Matrix(2, 2, 'Foo'); // => instance of matrix [['Foo', 'Foo'], ['Foo', 'Foo']]

// By dimensions and callback function to generate elements
const m = new Matrix(2, 2, function (i, j, m, n, matrix) {
    return i + j;
}); // => instance of matrix [[0, 1], [1, 2]]

2. Get Matrix

Syntax

m.get()

Example

m.get(); // => [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]

3. Get Element(s)

Syntax

m.getElem(x, y)

Where:

  • x - index (zero-based) of the column. If it is negative, the coordinate calculated from the end (width + x).
  • y - index (zero-based) of the row. If it is negative, the coordinate calculated from the end (height + y).

m.getElems(fromX, fromY[, toX = <width>, toY = <height>])

Where:

  • fromX - index (zero-based) of the column of start element. If it is negative, the coordinate calculated from the end (width + fromX).
  • fromY - index (zero-based) of the row of start element. If it is negative, the coordinate calculated from the end (height + fromY).
  • toX - index (zero-based) of the column till which extraction will be going. If it is negative, the coordinate calculated from the end (width + toX).
  • toY - index (zero-based) of the row till which extraction will be going. If it is negative, the coordinate calculated from the end (height + toY).

Example

// Get an Element by coordinates (zero-based)
m.getElem(0, 1); // => 'D'

// Get a set of Elements
m.getElems(1, 0, 2, 1); // => ['B', 'C', 'D', 'E', 'F']

4. Get Row(s)

Syntax

m.getRow(y)

Where:

  • y - index (zero-based) of the row. If it is negative, the coordinate calculated from the end (height + y).

m.getRows(fromY[, toY = <height>])

Where:

  • fromY - index (zero-based) of the start row. If it is negative, the coordinate calculated from the end (height + fromY).
  • toY - index (zero-based) of the row till which extraction will be going. If it is negative, the coordinate calculated from the end (height + toY).

Example

// Get a Row by index (zero-based)
m.getRow(1); // => ['D', 'E', 'F']

// Get a set of Rows
m.getRows(0, 2); // => [['A', 'B', 'C'], ['D', 'E', 'F']]

5. Get Column(s)

Syntax

m.getColumn(x)

Where:

  • x - index (zero-based) of the column. If it is negative, the coordinate calculated from the end (width + x).

m.getColumns(fromX[, toX = <width>])

Where:

  • fromX - index (zero-based) of the start column. If it is negative, the coordinate calculated from the end (width + fromX).
  • toX - index (zero-based) of the column till which extraction will be going. If it is negative, the coordinate calculated from the end (width + toX).

Example

// Get a Column by index (zero-based)
m.getColumn(2); // => ['C', 'F', 'I']

// Get a set of Columns
m.getColumns(1, 3); // => [['B', 'E', 'H'], ['C', 'F', 'I']]

6. Get Major Diagonal(s)

Syntax

m.getDiagonalMaj(index)

Where:

  • index - index (zero-based) of the diagonal. If it is negative, the coordinate calculated from the end (diagonals_amount + index).

m.getDiagonalsMaj(fromIndex[, toIndex = <diagonals_ammount>])

Where:

  • fromIndex - index (zero-based) of the start major diagonal. If it is negative, the value calculated from the end (diagonals_amount + fromIndex).
  • toIndex - index (zero-based) of the major diagonal till which extraction will be going. If it is negative, the value calculated from the end (diagonals_amount + toIndex).

Example

// Get a major Diagonal by index (zero-based)
m.getDiagonalMaj(1); // => ['B', 'F']

// Get a set of major Diagonals
m.getDiagonalsMaj(2, 4); // => [['A', 'E', 'I'], ['D', 'H']]

7. Get Minor Diagonal(s)

Syntax

m.getDiagonalMin(index)

Where:

  • index - index (zero-based) of the diagonal. If it is negative, the coordinate calculated from the end (diagonals_amount + index).

m.getDiagonalsMin(fromIndex[, toIndex = <diagonals_ammount>])

Where:

  • fromIndex - index (zero-based) of the start minor diagonal. If it is negative, the value calculated from the end (diagonals_amount + fromIndex).
  • toIndex - index (zero-based) of the minor diagonal till which extraction will be going. If it is negative, the value calculated from the end (diagonals_amount + toIndex).

Example

// Get a minor Diagonal by index (zero-based)
m.getDiagonalMin(4); // => ['I']

// Get a set of major Diagonals
m.getDiagonalsMin(-3, -1); // => [['B', 'D'], ['C', 'E', 'G']]

8. Get Submatrix

Syntax

m.getSubmatrix(fromX, fromY[, toX = <width>, toY = <height>])

Where:

  • fromX - index (zero-based) of the column of start element. If it is negative, the coordinate calculated from the end (width + fromX).
  • fromY - index (zero-based) of the row of start element. If it is negative, the coordinate calculated from the end (height + fromY).
  • toX - index (zero-based) of the column till which extraction will be going. If it is negative, the coordinate calculated from the end (width + toX).
  • toY - index (zero-based) of the row till which extraction will be going. If it is negative, the coordinate calculated from the end (height + toY).

Example

// Get a Submatrix
m.getSubmatrix(1, 1, 3, 3); // => [['E', 'F'], ['H', 'I']]

For more details see description of tests.

Test

$ git clone https://github.com/ahtohbi4/matrix-slicer.git
$ cd matrix-slicer
$ npm install
$ npm test

License

MIT © Alexander Antonov [email protected]