1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.endsWith = endsWith;
|
7 | exports.format = format;
|
8 | exports.stringify = stringify;
|
9 | exports.escape = escape;
|
10 | exports.compareText = compareText;
|
11 |
|
12 | var _is = require("./is");
|
13 |
|
14 | var _number = require("./number");
|
15 |
|
16 | var _formatter = require("./bignumber/formatter");
|
17 |
|
18 | function _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 |
|
22 |
|
23 |
|
24 |
|
25 | function 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 |
|
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 | function 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 | }
|
75 |
|
76 |
|
77 |
|
78 | if (looksLikeFraction(value)) {
|
79 | if (!options || options.fraction !== 'decimal') {
|
80 |
|
81 | return value.s * value.n + '/' + value.d;
|
82 | } else {
|
83 |
|
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 |
|
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 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function 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 += '\\';
|
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 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | function escape(value) {
|
161 | var text = String(value);
|
162 | text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
163 | return text;
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | function 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 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 | function looksLikeFraction(value) {
|
204 | return value && _typeof(value) === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false;
|
205 | }
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | function compareText(x, y) {
|
215 |
|
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 |