forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
119 lines (107 loc) · 3.13 KB
/
Copy pathstring.js
File metadata and controls
119 lines (107 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
var formatNumber = require('./number').format;
var formatBigNumber = require('./bignumber/formatter').format;
/**
* Test whether value is a string
* @param {*} value
* @return {boolean} isString
*/
exports.isString = function(value) {
return typeof value === 'string';
};
/**
* Check if a text ends with a certain string.
* @param {string} text
* @param {string} search
*/
exports.endsWith = function(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/utils/number:format for a
* description of the available
* options.
* @return {string} str
*/
exports.format = function(value, options) {
if (typeof value === 'number') {
return formatNumber(value, options);
}
if (value && value.isBigNumber === true) {
return formatBigNumber(value, options);
}
if (value && value.isFraction === true) {
if (!options || options.fraction !== 'decimal') {
// output as ratio, like '1/3'
return (value.s * value.n) + '/' + value.d;
}
else {
// output as decimal, like '0.(3)'
return value.toString();
}
}
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 (typeof value === '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/utils/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);
}
}