UNPKG

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