nowjs-core
Version:
NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence
158 lines (157 loc) • 4.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = require("../../exceptions/index");
class Matrix {
constructor(rowSize = 2, colSize = 2, ...rows) {
this.rowSize = rowSize;
this.colSize = colSize;
this.init(rowSize, colSize, ...rows);
}
get IsSquare() {
return this.colSize === this.rowSize;
}
get ColSize() {
return this.colSize;
}
get RowSize() {
return this.rowSize;
}
set(value, row, col) {
this.checkIndexes(row, col);
this.arr[row][col] = value;
}
getElement(row, col) {
this.checkIndexes(row, col);
return this.arr[row][col];
}
getSize() {
return [this.rowSize, this.colSize];
}
get(row, col) {
if (!col && col !== 0) {
return this.getRow(row);
}
else if (row || row === 0) {
return this.getElement(row, col);
}
else {
return this.getColumn(col);
}
}
getColumn(col) {
const arrs = [];
this.checkColumnIndex(col);
for (const row of this.arr) {
arrs.push(row[col]);
}
return arrs;
}
getRow(row) {
this.checkRowIndex(row);
return this.arr[row];
}
forEachRow(fn) {
if (typeof fn === 'function') {
for (let i = 0, len = this.arr.length; i < len; i++) {
fn(i, this.arr[i], this.arr);
}
}
return this;
}
replaceEachRow(fn) {
let newRow;
if (typeof fn === 'function') {
for (let i = 0, len = this.arr.length; i < len; i++) {
newRow = fn(i, this.arr[i], this.arr);
if (newRow && Array.isArray(newRow)) {
this.arr[i] = newRow;
newRow = null;
}
}
}
return this;
}
[Symbol.iterator]() {
const itr = {
[Symbol.iterator]: () => {
let row = 0, col = -1;
return {
next: () => {
if (col > this.colSize) {
row++;
col = -1;
}
col++;
if (row > this.rowSize) {
return { value: this.arr[row][col], done: true };
}
return { value: this.arr[row][col], done: false };
},
};
},
};
return itr;
}
rotateLeft(times) {
return this;
}
rotateRight(times) {
return this;
}
reverse() {
return this;
}
transpose() {
const arr2 = (this.arr &&
this.arr.length &&
this.arr[0].map &&
Object.keys(this.arr[0]).map((_, c) => {
return this.arr.map(r => r[c]);
})) ||
[];
return new Matrix(this.colSize, this.rowSize, ...arr2);
}
toArray() {
const arr = [];
for (let i = 0; i < this.rowSize; i++) {
arr.push([]);
for (let j = 0; j < this.colSize; j++) {
arr[i].push(this.arr[i] ? this.arr[i][j] : undefined);
}
}
return arr;
}
swapValues(firstRow, firstCol, secondRow, secondCol) {
this.checkIndexes(firstRow, firstCol);
this.checkIndexes(secondRow, secondCol);
const temp = this.arr[firstRow][firstCol];
this.arr[firstRow][firstCol] = this.arr[secondRow][secondCol];
this.arr[secondRow][secondCol] = temp;
}
checkColumnIndex(col) {
if (col >= this.colSize || col < 0) {
throw new index_1.Exception('Col index is out of range.');
}
}
checkRowIndex(row) {
if (row >= this.rowSize || row < 0) {
throw new index_1.Exception('Row index is out of range.');
}
}
checkIndexes(row, col) {
this.checkColumnIndex(col);
this.checkRowIndex(row);
}
init(rowSize, colSize, ...rows) {
this.arr = [];
this.rowSize = rowSize;
this.colSize = colSize;
for (let i = 0; i < rowSize; i++) {
this.arr.push([]);
for (let j = 0; j < colSize; j++) {
this.arr[i].push(rows[i] ? rows[i][j] : undefined);
}
}
}
}
exports.Matrix = Matrix;