Added function format to Selector

This commit is contained in:
josdejong 2013-10-26 14:07:47 +02:00
parent ced03dd1fb
commit e1ead2f7a6
6 changed files with 34 additions and 13 deletions

View File

@ -13,10 +13,14 @@ module.exports = function (math) {
* the final value.
*
* The Selector has a number of special functions:
* - done() Finalize the chained operation and return the selectors value.
* - valueOf() The same as done()
* - toString() Executes math.format() onto the selectors value, returning
* a string representation of the value.
* - done() Finalize the chained operation and return the
* selectors value.
* - valueOf() The same as done()
* - toString() Returns a string representation of the selectors value.
* - format([options]) Returns a string representation of the selectors value,
* and allows formatting the string in different ways.
* See lib/util/number:format for a description of the
* available options.
*
* @param {*} [value]
*/
@ -72,11 +76,24 @@ module.exports = function (math) {
},
/**
* Get the string representation of the value in the selector
* Get a string representation of the value in the selector
* @returns {String}
*/
toString: function () {
return string.format(this.value);
},
/**
* Get a string representation of the selectors value, with optional
* formatting options.
* @param {Object | Number | Function} [options] Formatting options. See
* lib/util/number:format for a
* description of the available
* options.
* @return {String} str
*/
format: function format (options) {
return string.format(this.value, options);
}
};

View File

@ -298,7 +298,8 @@ Complex.prototype.equals = function equals (other) {
};
/**
* Format the complex number as a string..
* Get a string representation of the complex number,
* with optional formatting options.
* @param {Object | Number | Function} [options] Formatting options. See
* lib/util/number:format for a
* description of the available
@ -350,9 +351,7 @@ Complex.prototype.format = function format (options) {
};
/**
* Get string representation of the Complex value.
* See also Complex.format, which allows to specify number of digits for
* the output.
* Get a string representation of the complex number.
* @return {String} str
*/
Complex.prototype.toString = function toString () {

View File

@ -692,7 +692,7 @@ Matrix.prototype.valueOf = function valueOf() {
};
/**
* Get a string representation of the matrix.
* Get a string representation of the matrix, with optional formatting options.
* @param {Object | Number | Function} [options] Formatting options. See
* lib/util/number:format for a
* description of the available

View File

@ -230,7 +230,7 @@ Range.prototype.valueOf = function valueOf() {
};
/**
* Get the string representation of the range.
* Get a string representation of the range, with optional formatting options.
* Output is formatted as 'start:step:end', for example '2:6' or '0:0.2:11'
* @param {Object | Number | Function} [options] Formatting options. See
* lib/util/number:format for a
@ -248,6 +248,10 @@ Range.prototype.format = function format(options) {
return str;
};
/**
* Get a string representation of the range.
* @returns {String}
*/
Range.prototype.toString = function toString() {
return this.format();
};

View File

@ -387,7 +387,7 @@ Unit.prototype.toNumber = function (plainUnit) {
/**
* Get string representation
* Get a string representation of the unit.
* @return {String}
*/
Unit.prototype.toString = function toString() {
@ -395,7 +395,7 @@ Unit.prototype.toString = function toString() {
};
/**
* Get string representation of the Unit.
* Get a string representation of the Unit, with optional formatting options.
* @param {Object | Number | Function} [options] Formatting options. See
* lib/util/number:format for a
* description of the available

View File

@ -31,6 +31,7 @@ exports.endsWith = function endsWith(text, search) {
* math.format(2/7); // '0.2857142857142857'
* math.format(math.pi, 3); // '3.14'
* math.format(new Complex(2, 3)); // '2 + 3i'
* math.format('hello'); // '"hello"'
*
* @param {*} value Value to be stringified
* @param {Object | Number | Function} [options] Formatting options. See