mathjs
Version:
Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.
108 lines (97 loc) • 2.95 kB
JavaScript
var number = require('./number'),
bignumber = require('./bignumber'),
BigNumber = require('decimal.js');
/**
* Test whether value is a String
* @param {*} value
* @return {Boolean} isString
*/
exports.isString = function isString(value) {
return (value instanceof String) || (typeof value == 'string');
};
/**
* Check if a text ends with a certain string.
* @param {String} text
* @param {String} search
*/
exports.endsWith = function endsWith(text, search) {
var start = text.length - search.length;
var end = text.length;
return (text.substring(start, end) === search);
};
/**
* Format a value of any type into a string.
*
* Usage:
* math.format(value)
* math.format(value, precision)
*
* If value is a function, the returned string is 'function' unless the function
* has a property `description`, in that case this properties value is returned.
*
* Example usage:
* 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
* lib/util/number:format for a
* description of the available
* options.
* @return {String} str
*/
exports.format = function format(value, options) {
if (number.isNumber(value)) {
return number.format(value, options);
}
if (value instanceof BigNumber) {
return bignumber.format(value, options);
}
if (Array.isArray(value)) {
return formatArray(value, options);
}
if (exports.isString(value)) {
return '"' + value + '"';
}
if (typeof value === 'function') {
return value.syntax ? value.syntax + '' : 'function';
}
if (value instanceof Object) {
if (typeof value.format === 'function') {
return value.format(options);
}
else {
return value.toString();
}
}
return String(value);
};
/**
* Recursively format an n-dimensional matrix
* Example output: "[[1, 2], [3, 4]]"
* @param {Array} array
* @param {Object | Number | Function} [options] Formatting options. See
* lib/util/number:format for a
* description of the available
* options.
* @returns {String} str
*/
function formatArray (array, options) {
if (Array.isArray(array)) {
var str = '[';
var len = array.length;
for (var i = 0; i < len; i++) {
if (i != 0) {
str += ', ';
}
str += formatArray(array[i], options);
}
str += ']';
return str;
}
else {
return exports.format(array, options);
}
}