-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allows to select only rows or columns as view (#51)
* feat: allows to select only rows or columns as view * test(selectionView): handle typed arrays * test: remove should * test: explicit message on throws
- Loading branch information
1 parent
7519323
commit 46eb916
Showing
11 changed files
with
155 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
node_modules | ||
/matrix.js | ||
coverage/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,35 @@ | ||
import 'should'; | ||
|
||
import Matrix from '../../src'; | ||
|
||
describe('Selection view', function () { | ||
it('should correctly remap coordinates', function () { | ||
describe('Selection view', () => { | ||
it('should correctly remap coordinates', () => { | ||
const m = Matrix.ones(5, 8); | ||
const msv = m.selectionView([1, 2], [2, 1]); | ||
|
||
m.get(1, 2).should.equal(1); | ||
expect(m.get(1, 2)).toBe(1); | ||
msv.set(0, 0, 5); | ||
m.get(1, 2).should.equal(5); | ||
expect(m.get(1, 2)).toBe(5); | ||
|
||
m.get(2, 1).should.equal(1); | ||
expect(m.get(2, 1)).toBe(1); | ||
m.set(2, 1, 10); | ||
msv.get(1, 1).should.equal(10); | ||
expect(msv.get(1, 1)).toBe(10); | ||
}); | ||
|
||
it('should throw when wrong arguments or range', function () { | ||
const m = Matrix.ones(2, 2); | ||
(function () { | ||
m.selectionView([1, 1, 2], [0, 2]); | ||
}).should.throw(RangeError); | ||
it('should handle typed arrays', () => { | ||
const m = Matrix.ones(5, 8); | ||
const msv = m.selectionView(Int8Array.from([1, 2]), Int8Array.from([2, 1])); | ||
|
||
expect(m.get(1, 2)).toBe(1); | ||
msv.set(0, 0, 5); | ||
expect(m.get(1, 2)).toBe(5); | ||
|
||
(function () { | ||
m.selectionView([1, 1, 2]); | ||
}).should.throw(TypeError); | ||
expect(m.get(2, 1)).toBe(1); | ||
m.set(2, 1, 10); | ||
expect(msv.get(1, 1)).toBe(10); | ||
}); | ||
|
||
it('should throw when wrong arguments or range', () => { | ||
const m = Matrix.ones(2, 2); | ||
expect(() => m.selectionView([1, 1, 2], [0, 2])).toThrow(RangeError); | ||
expect(() => m.selectionView([1, 1])).toThrow(TypeError); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Matrix from '../../src'; | ||
|
||
describe('Selection column view', () => { | ||
it('should correctly remap coordinates', () => { | ||
const m = Matrix.ones(5, 8); | ||
const msv = m.columnSelectionView([1, 2]); | ||
|
||
expect(m.get(0, 2)).toBe(1); | ||
msv.set(0, 1, 5); | ||
expect(m.get(0, 2)).toBe(5); | ||
}); | ||
|
||
it('should throw when wrong arguments or range', () => { | ||
const m = Matrix.ones(2, 2); | ||
expect(() => m.columnSelectionView([1, 1, 2])).toThrow('column indices are out of range'); | ||
expect(() => m.columnSelectionView(1)).toThrow('unexpected type for column indices'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Matrix from '../../src'; | ||
|
||
describe('Selection view', () => { | ||
it('should correctly remap coordinates', () => { | ||
const m = Matrix.ones(5, 8); | ||
const msv = m.rowSelectionView([1, 2]); | ||
|
||
expect(m.get(1, 0)).toBe(1); | ||
msv.set(0, 0, 5); | ||
expect(m.get(1, 0)).toBe(5); | ||
}); | ||
|
||
it('should throw when wrong arguments or range', () => { | ||
const m = Matrix.ones(2, 2); | ||
expect(() => m.rowSelectionView([1, 1, 2])).toThrow('row indices are out of range'); | ||
expect(() => m.rowSelectionView(1)).toThrow('unexpected type for row indices'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import BaseView from './base'; | ||
import {checkColumnIndices} from '../util'; | ||
|
||
export default class MatrixColumnSelectionView extends BaseView { | ||
constructor(matrix, columnIndices) { | ||
columnIndices = checkColumnIndices(matrix, columnIndices); | ||
super(matrix, matrix.rows, columnIndices.length); | ||
this.columnIndices = columnIndices; | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(rowIndex, this.columnIndices[columnIndex], value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(rowIndex, this.columnIndices[columnIndex]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import BaseView from './base'; | ||
import {checkRowIndices} from '../util'; | ||
|
||
export default class MatrixRowSelectionView extends BaseView { | ||
constructor(matrix, rowIndices) { | ||
rowIndices = checkRowIndices(matrix, rowIndices); | ||
super(matrix, rowIndices.length, matrix.columns); | ||
this.rowIndices = rowIndices; | ||
} | ||
|
||
set(rowIndex, columnIndex, value) { | ||
this.matrix.set(this.rowIndices[rowIndex], columnIndex, value); | ||
return this; | ||
} | ||
|
||
get(rowIndex, columnIndex) { | ||
return this.matrix.get(this.rowIndices[rowIndex], columnIndex); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import Matrix from '../src'; | ||
import Matrix from './src/index'; | ||
|
||
export function getSquareArray() { | ||
return [ | ||
|