UNPKG

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