From e1ead2f7a6ee28eb99fc1687b0faa387af1bb24e Mon Sep 17 00:00:00 2001 From: josdejong Date: Sat, 26 Oct 2013 14:07:47 +0200 Subject: [PATCH] Added function `format` to Selector --- lib/chaining/Selector.js | 27 ++++++++++++++++++++++----- lib/type/Complex.js | 7 +++---- lib/type/Matrix.js | 2 +- lib/type/Range.js | 6 +++++- lib/type/Unit.js | 4 ++-- lib/util/string.js | 1 + 6 files changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/chaining/Selector.js b/lib/chaining/Selector.js index a7176a3c3..0d4f4e10a 100644 --- a/lib/chaining/Selector.js +++ b/lib/chaining/Selector.js @@ -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); } }; diff --git a/lib/type/Complex.js b/lib/type/Complex.js index f62d798ce..60c910781 100644 --- a/lib/type/Complex.js +++ b/lib/type/Complex.js @@ -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 () { diff --git a/lib/type/Matrix.js b/lib/type/Matrix.js index 5d9fd2d4a..2d9fb9423 100644 --- a/lib/type/Matrix.js +++ b/lib/type/Matrix.js @@ -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 diff --git a/lib/type/Range.js b/lib/type/Range.js index d31fcd047..7cec0f71f 100644 --- a/lib/type/Range.js +++ b/lib/type/Range.js @@ -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(); }; diff --git a/lib/type/Unit.js b/lib/type/Unit.js index 31c507943..8a8d09a29 100644 --- a/lib/type/Unit.js +++ b/lib/type/Unit.js @@ -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 diff --git a/lib/util/string.js b/lib/util/string.js index 2ba9ae511..80dc20440 100644 --- a/lib/util/string.js +++ b/lib/util/string.js @@ -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