Skip to content

Commit 55ee4a6

Browse files
committed
feat: add flipColumn and flipRow views
1 parent f0f8375 commit 55ee4a6

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

src/abstractMatrix.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var util = require('./util');
77
var MatrixTransposeView = require('./views/transpose');
88
var MatrixRowView = require('./views/row');
99
var MatrixColumnView = require('./views/column');
10+
var MatrixFlipRowView = require('./views/flipRow');
11+
var MatrixFlipColumnView = require('./views/flipColumn');
1012

1113
function abstractMatrix(superCtor) {
1214
if (superCtor === undefined) superCtor = Object;
@@ -1192,6 +1194,14 @@ function abstractMatrix(superCtor) {
11921194
util.checkColumnIndex(this, column);
11931195
return new MatrixColumnView(this, column);
11941196
}
1197+
1198+
flipRowView() {
1199+
return new MatrixFlipRowView(this);
1200+
}
1201+
1202+
flipColumnView() {
1203+
return new MatrixFlipColumnView(this);
1204+
}
11951205
}
11961206

11971207
Matrix.prototype.klass = 'Matrix';

src/views/flipColumn.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var BaseView = require('./base');
4+
5+
class MatrixFlipColumnView extends BaseView {
6+
constructor(matrix) {
7+
super(matrix, matrix.rows, matrix.columns);
8+
}
9+
10+
set(rowIndex, columnIndex, value) {
11+
this.matrix.set(rowIndex, this.columns - columnIndex - 1, value);
12+
return this;
13+
}
14+
15+
get(rowIndex, columnIndex) {
16+
return this.matrix.get(rowIndex, this.columns - columnIndex - 1);
17+
}
18+
}
19+
20+
module.exports = MatrixFlipColumnView;

src/views/flipRow.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
var BaseView = require('./base');
4+
5+
class MatrixFlipRowView extends BaseView {
6+
constructor(matrix) {
7+
super(matrix, matrix.rows, matrix.columns);
8+
}
9+
10+
set(rowIndex, columnIndex, value) {
11+
this.matrix.set(this.rows - rowIndex - 1, columnIndex, value);
12+
return this;
13+
}
14+
15+
get(rowIndex, columnIndex) {
16+
return this.matrix.get(this.rows - rowIndex - 1, columnIndex);
17+
}
18+
}
19+
20+
module.exports = MatrixFlipRowView;

test/views/flipColumn.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var Matrix = require('../..');
4+
5+
describe('Flip column view', function () {
6+
it('should set and get values', function () {
7+
var m = Matrix.ones(5, 8);
8+
var view = m.flipColumnView();
9+
10+
m.set(0, 3, 5);
11+
view.get(0, 4).should.equal(5);
12+
13+
view.set(0, 0, 10);
14+
m.get(0, 7).should.equal(10);
15+
16+
view.rows.should.equal(5);
17+
view.columns.should.equal(8);
18+
});
19+
});

test/views/flipRow.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
var Matrix = require('../..');
4+
5+
describe('Flip row view', function () {
6+
it('should set and get values', function () {
7+
var m = Matrix.ones(5, 8);
8+
var view = m.flipRowView();
9+
10+
m.set(0, 3, 5);
11+
view.get(4, 3).should.equal(5);
12+
13+
view.set(0, 0, 10);
14+
m.get(4, 0).should.equal(10);
15+
16+
view.rows.should.equal(5);
17+
view.columns.should.equal(8);
18+
});
19+
});

0 commit comments

Comments
 (0)