UNPKG

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