Comparing version 2.0.1 to 2.1.0
@@ -249,2 +249,47 @@ /** | ||
/** | ||
* Get the data for this matrix as a formatted string, which is useful for | ||
* logging and debugging. It will be formatted with line breaks to visualize | ||
* the rows and columns. | ||
* | ||
* @param {number|string=} opt_indentation Optional argument to control | ||
* indentation in the output string. If set to a number, the indentation | ||
* will be that many spaces wide. If it is a string, the indentation will be | ||
* this string. It will default to two spaces. | ||
* @param {string=} opt_separator Optional argument to control what separates | ||
* the values in the output string. It will default to two spaces. | ||
* @param {string=} opt_start String to start the output with. Default is '['. | ||
* @param {string=} opt_end String to end the output with. Default is ']'. | ||
* | ||
* @return {string} A string representation of the data. | ||
*/ | ||
Matrix.prototype.toLogString = function(opt_indentation, opt_separator, opt_start, opt_end) { | ||
var array = this.toArray(); | ||
var beginning; | ||
var sep; | ||
var separator = typeof opt_separator === 'string' ? opt_separator : ' '; | ||
var indentation = ' '; | ||
if (typeof opt_indentation === 'number') { | ||
indentation = (new Array(Math.max(0, opt_indentation) + 1)).join(' '); | ||
} else if (typeof opt_indentation === 'string') { | ||
indentation = opt_indentation; | ||
} | ||
var start = typeof opt_start === 'string' ? opt_start : '['; | ||
var end = typeof opt_end === 'string' ? opt_end : ']'; | ||
var string = start; | ||
for (var i = 0, l = array.length; i < l; i++) { | ||
beginning = i % this.cols === 0 ? '\n' + indentation : ''; | ||
sep = i % this.cols === this.cols - 1 ? '' : separator; | ||
string += beginning + array[i] + sep; | ||
} | ||
string += '\n' + end; | ||
return string; | ||
}; | ||
/** | ||
* Clone this matrix to a new instance. | ||
@@ -251,0 +296,0 @@ * |
{ | ||
"name": "matrixmath", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"author": "Johannes Koggdal <johannes@koggdal.com>", | ||
@@ -5,0 +5,0 @@ "description": "Library for working with mathematical matrices.", |
@@ -49,2 +49,3 @@ # Matrix Math | ||
* toArray () | ||
* toLogString () | ||
* clone () | ||
@@ -295,2 +296,48 @@ * add (matrix[,…matrixN]) | ||
#### matrix.toLogString([opt_indentation[, opt_separator[, opt_start[, opt_end]]]]) | ||
Get the data for this matrix as a formatted string, which is useful for logging and debugging. It will be formatted with line breaks to visualize the rows and columns. | ||
``` | ||
var matrix = new Matrix(3, 3); | ||
``` | ||
``` | ||
> console.log(matrix.toLogString()); | ||
[ | ||
1 0 0 | ||
0 1 0 | ||
0 0 1 | ||
] | ||
``` | ||
``` | ||
> console.log(matrix.toLogString(5)); | ||
[ | ||
1 0 0 | ||
0 1 0 | ||
0 0 1 | ||
] | ||
``` | ||
``` | ||
> console.log(matrix.toLogString(' ', ' | ')); | ||
[ | ||
1 | 0 | 0 | ||
0 | 1 | 0 | ||
0 | 0 | 1 | ||
] | ||
``` | ||
``` | ||
> console.log(matrix.toLogString(0, ' | ', '-- start --', '-- end --')); | ||
-- start -- | ||
1 | 0 | 0 | ||
0 | 1 | 0 | ||
0 | 0 | 1 | ||
-- end -- | ||
``` | ||
#### matrix.clone() | ||
@@ -297,0 +344,0 @@ |
@@ -376,2 +376,145 @@ var expect = require('expect.js'); | ||
describe('#toLogString()', function() { | ||
var matrix1 = new Matrix(3, 3); | ||
it('should format all the values as a string with columns and rows', function() { | ||
var string = matrix1.toLogString(); | ||
var expectedString = '[\n'; | ||
expectedString += ' 1 0 0\n'; | ||
expectedString += ' 0 1 0\n'; | ||
expectedString += ' 0 0 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should format all the values as a string with the provided number of spaces as indentation', function() { | ||
var string = matrix1.toLogString(5); | ||
var expectedString = '[\n'; | ||
expectedString += ' 1 0 0\n'; | ||
expectedString += ' 0 1 0\n'; | ||
expectedString += ' 0 0 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should convert negative space count to 0', function() { | ||
var string = matrix1.toLogString(-5); | ||
var expectedString = '[\n'; | ||
expectedString += '1 0 0\n'; | ||
expectedString += '0 1 0\n'; | ||
expectedString += '0 0 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should format all the values as a string with the provided string used for indentation', function() { | ||
var string = matrix1.toLogString('\t'); | ||
var expectedString = '[\n'; | ||
expectedString += '\t1 0 0\n'; | ||
expectedString += '\t0 1 0\n'; | ||
expectedString += '\t0 0 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should format all the values as a string with the provided string used as separator', function() { | ||
var string = matrix1.toLogString(' ', ' | '); | ||
var expectedString = '[\n'; | ||
expectedString += ' 1 | 0 | 0\n'; | ||
expectedString += ' 0 | 1 | 0\n'; | ||
expectedString += ' 0 | 0 | 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should format all the values as a string with the provided string used as start', function() { | ||
var string = matrix1.toLogString(' ', ' | ', '---'); | ||
var expectedString = '---\n'; | ||
expectedString += ' 1 | 0 | 0\n'; | ||
expectedString += ' 0 | 1 | 0\n'; | ||
expectedString += ' 0 | 0 | 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should format all the values as a string with the provided string used as end', function() { | ||
var string = matrix1.toLogString(' ', ' | ', '---', '-=-'); | ||
var expectedString = '---\n'; | ||
expectedString += ' 1 | 0 | 0\n'; | ||
expectedString += ' 0 | 1 | 0\n'; | ||
expectedString += ' 0 | 0 | 1\n'; | ||
expectedString += '-=-'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should work for large matrices', function() { | ||
var matrix1 = new Matrix(5, 5); | ||
var string = matrix1.toLogString(); | ||
var expectedString = '[\n'; | ||
expectedString += ' 1 0 0 0 0\n'; | ||
expectedString += ' 0 1 0 0 0\n'; | ||
expectedString += ' 0 0 1 0 0\n'; | ||
expectedString += ' 0 0 0 1 0\n'; | ||
expectedString += ' 0 0 0 0 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should work for small matrices', function() { | ||
var matrix1 = new Matrix(2, 2); | ||
var string = matrix1.toLogString(); | ||
var expectedString = '[\n'; | ||
expectedString += ' 1 0\n'; | ||
expectedString += ' 0 1\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should work for tall matrices', function() { | ||
var matrix1 = new Matrix(4, 2); | ||
var string = matrix1.toLogString(); | ||
var expectedString = '[\n'; | ||
expectedString += ' 0 0\n'; | ||
expectedString += ' 0 0\n'; | ||
expectedString += ' 0 0\n'; | ||
expectedString += ' 0 0\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
it('should work for wide matrices', function() { | ||
var matrix1 = new Matrix(2, 4); | ||
var string = matrix1.toLogString(); | ||
var expectedString = '[\n'; | ||
expectedString += ' 0 0 0 0\n'; | ||
expectedString += ' 0 0 0 0\n'; | ||
expectedString += ']'; | ||
expect(string).to.equal(expectedString); | ||
}); | ||
}); | ||
describe('#clone()', function() { | ||
@@ -378,0 +521,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60440
1256
528