UNPKG

5.88 kBJavaScriptView Raw
1'use strict';
2
3function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
4
5var formatNumber = require('./number').format;
6
7var formatBigNumber = require('./bignumber/formatter').format;
8
9var isBigNumber = require('./bignumber/isBigNumber');
10/**
11 * Test whether value is a string
12 * @param {*} value
13 * @return {boolean} isString
14 */
15
16
17exports.isString = function (value) {
18 return typeof value === 'string';
19};
20/**
21 * Check if a text ends with a certain string.
22 * @param {string} text
23 * @param {string} search
24 */
25
26
27exports.endsWith = function (text, search) {
28 var start = text.length - search.length;
29 var end = text.length;
30 return text.substring(start, end) === search;
31};
32/**
33 * Format a value of any type into a string.
34 *
35 * Usage:
36 * math.format(value)
37 * math.format(value, precision)
38 *
39 * When value is a function:
40 *
41 * - When the function has a property `syntax`, it returns this
42 * syntax description.
43 * - In other cases, a string `'function'` is returned.
44 *
45 * When `value` is an Object:
46 *
47 * - When the object contains a property `format` being a function, this
48 * function is invoked as `value.format(options)` and the result is returned.
49 * - When the object has its own `toString` method, this method is invoked
50 * and the result is returned.
51 * - In other cases the function will loop over all object properties and
52 * return JSON object notation like '{"a": 2, "b": 3}'.
53 *
54 * Example usage:
55 * math.format(2/7) // '0.2857142857142857'
56 * math.format(math.pi, 3) // '3.14'
57 * math.format(new Complex(2, 3)) // '2 + 3i'
58 * math.format('hello') // '"hello"'
59 *
60 * @param {*} value Value to be stringified
61 * @param {Object | number | Function} [options] Formatting options. See
62 * lib/utils/number:format for a
63 * description of the available
64 * options.
65 * @return {string} str
66 */
67
68
69exports.format = function (value, options) {
70 if (typeof value === 'number') {
71 return formatNumber(value, options);
72 }
73
74 if (isBigNumber(value)) {
75 return formatBigNumber(value, options);
76 } // note: we use unsafe duck-typing here to check for Fractions, this is
77 // ok here since we're only invoking toString or concatenating its values
78
79
80 if (looksLikeFraction(value)) {
81 if (!options || options.fraction !== 'decimal') {
82 // output as ratio, like '1/3'
83 return value.s * value.n + '/' + value.d;
84 } else {
85 // output as decimal, like '0.(3)'
86 return value.toString();
87 }
88 }
89
90 if (Array.isArray(value)) {
91 return formatArray(value, options);
92 }
93
94 if (exports.isString(value)) {
95 return '"' + value + '"';
96 }
97
98 if (typeof value === 'function') {
99 return value.syntax ? String(value.syntax) : 'function';
100 }
101
102 if (value && _typeof(value) === 'object') {
103 if (typeof value.format === 'function') {
104 return value.format(options);
105 } else if (value && value.toString() !== {}.toString()) {
106 // this object has a non-native toString method, use that one
107 return value.toString();
108 } else {
109 var entries = [];
110
111 for (var key in value) {
112 if (value.hasOwnProperty(key)) {
113 entries.push('"' + key + '": ' + exports.format(value[key], options));
114 }
115 }
116
117 return '{' + entries.join(', ') + '}';
118 }
119 }
120
121 return String(value);
122};
123/**
124 * Stringify a value into a string enclosed in double quotes.
125 * Unescaped double quotes and backslashes inside the value are escaped.
126 * @param {*} value
127 * @return {string}
128 */
129
130
131exports.stringify = function (value) {
132 var text = String(value);
133 var escaped = '';
134 var i = 0;
135
136 while (i < text.length) {
137 var c = text.charAt(i);
138
139 if (c === '\\') {
140 escaped += c;
141 i++;
142 c = text.charAt(i);
143
144 if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) {
145 escaped += '\\'; // no valid escape character -> escape it
146 }
147
148 escaped += c;
149 } else if (c === '"') {
150 escaped += '\\"';
151 } else {
152 escaped += c;
153 }
154
155 i++;
156 }
157
158 return '"' + escaped + '"';
159};
160/**
161 * Escape special HTML characters
162 * @param {*} value
163 * @return {string}
164 */
165
166
167exports.escape = function (value) {
168 var text = String(value);
169 text = text.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
170 return text;
171};
172/**
173 * Recursively format an n-dimensional matrix
174 * Example output: "[[1, 2], [3, 4]]"
175 * @param {Array} array
176 * @param {Object | number | Function} [options] Formatting options. See
177 * lib/utils/number:format for a
178 * description of the available
179 * options.
180 * @returns {string} str
181 */
182
183
184function formatArray(array, options) {
185 if (Array.isArray(array)) {
186 var str = '[';
187 var len = array.length;
188
189 for (var i = 0; i < len; i++) {
190 if (i !== 0) {
191 str += ', ';
192 }
193
194 str += formatArray(array[i], options);
195 }
196
197 str += ']';
198 return str;
199 } else {
200 return exports.format(array, options);
201 }
202}
203/**
204 * Check whether a value looks like a Fraction (unsafe duck-type check)
205 * @param {*} value
206 * @return {boolean}
207 */
208
209
210function looksLikeFraction(value) {
211 return value && _typeof(value) === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false;
212}
\No newline at end of file