Skip to content

Commit cb51169

Browse files
committed
feat: add a custom Node.js inspect function
1 parent 22c4f60 commit cb51169

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/__tests__/matrix/utility.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,14 @@ describe('utility methods', () => {
279279
expect(result[2][0]).toBeCloseTo(0.24390244, 5);
280280
expect(result[2][1]).toBeCloseTo(0.07317073, 5);
281281
});
282+
282283
it('isEchelonForm', () => {
283284
var matrix = new Matrix([[1, 0], [0, 1]]);
284285
var matrix2 = new Matrix([[2, 1], [1, 1]]);
285286
expect(matrix.isEchelonForm()).toStrictEqual(true);
286287
expect(matrix2.isEchelonForm()).toStrictEqual(false);
287288
});
289+
288290
it('isReducedEchelonForm', () => {
289291
var matrix = new Matrix([[1, 0], [0, 1]]);
290292
var matrix2 = new Matrix([[1, 1], [0, 1]]);

src/abstractMatrix.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
productAll
1818
} from './stat';
1919

20+
const inspect = Symbol.for('nodejs.util.inspect.custom');
21+
2022
export default class AbstractMatrix {
2123
static get [Symbol.species]() {
2224
return this;
@@ -1673,6 +1675,17 @@ export default class AbstractMatrix {
16731675
}
16741676
return variance;
16751677
}
1678+
1679+
[inspect](_, options) {
1680+
const { indentationLvl } = options;
1681+
const indent = ' '.repeat(indentationLvl + 2);
1682+
const indentData = ' '.repeat(indentationLvl + 4);
1683+
return `${this.constructor.name} {
1684+
${indent}[ ${inspectData(this, indentData)} ]
1685+
${indent}rows: ${this.rows},
1686+
${indent}columns: ${this.columns}
1687+
${' '.repeat(indentationLvl)}}`;
1688+
}
16761689
}
16771690

16781691
AbstractMatrix.prototype.klass = 'Matrix';
@@ -1966,3 +1979,15 @@ function fillTemplateFunction(template, values) {
19661979
}
19671980
return template;
19681981
}
1982+
1983+
function inspectData(matrix, indent) {
1984+
const result = [];
1985+
for (var i = 0; i < matrix.rows; i++) {
1986+
let line = [];
1987+
for (var j = 0; j < matrix.columns; j++) {
1988+
line.push(Math.round(matrix.get(i, j) * 1000) / 1000);
1989+
}
1990+
result.push(`[ ${line.join(', ')} ]`);
1991+
}
1992+
return result.join(`\n${indent}`);
1993+
}

0 commit comments

Comments
 (0)