Skip to content

Commit 021115b

Browse files
committed
feat: make JSON.stringify always return a 2D array from any matrix
1 parent c573709 commit 021115b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/__tests__/matrix/creation.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Matrix } from '../..';
1+
import { Matrix, wrap } from '../..';
22
import MatrixTransposeView from '../../views/transpose';
33
import * as util from '../../../testUtils';
44

@@ -160,4 +160,15 @@ describe('Matrix creation', () => {
160160
expect(Matrix.subtract(a, b).to2DArray()).toStrictEqual([[-2, 1]]);
161161
expect(MatrixTransposeView.subtract(a, b).to2DArray()).toStrictEqual([[-2, 1]]);
162162
});
163+
164+
it('JSON.stringify should always return a 2D array', () => {
165+
const data = [[0, 1], [2, 3]];
166+
const json = JSON.stringify(data);
167+
const matrix = new Matrix(data);
168+
expect(JSON.stringify(matrix)).toBe(json);
169+
const mat1D = wrap(matrix.to1DArray(), { rows: 2 });
170+
expect(JSON.stringify(mat1D)).toBe(json);
171+
const transposeTwice = matrix.transposeView().transposeView();
172+
expect(JSON.stringify(transposeTwice)).toBe(json);
173+
});
163174
});

src/abstractMatrix.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ export default function AbstractMatrix(superCtor) {
299299
return copy;
300300
}
301301

302+
toJSON() {
303+
return this.to2DArray();
304+
}
305+
302306
/**
303307
* @return {boolean} true if the matrix has one row
304308
*/

0 commit comments

Comments
 (0)