1 | 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); }
|
2 |
|
3 | import { isBigNumber, isString, typeOf } from './is';
|
4 | import { format as formatNumber } from './number';
|
5 | import { format as formatBigNumber } from './bignumber/formatter';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | export 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 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
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 | export 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 | }
|
61 |
|
62 |
|
63 |
|
64 | if (looksLikeFraction(value)) {
|
65 | if (!options || options.fraction !== 'decimal') {
|
66 |
|
67 | return value.s * value.n + '/' + value.d;
|
68 | } else {
|
69 |
|
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 |
|
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 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | export 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 += '\\';
|
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 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | export function escape(value) {
|
145 | var text = String(value);
|
146 | text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
147 | return text;
|
148 | }
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | function 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 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | function looksLikeFraction(value) {
|
187 | return value && _typeof(value) === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false;
|
188 | }
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 | export function compareText(x, y) {
|
198 |
|
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 |