1 | import { isBigNumber, isString, typeOf } from './is.js';
|
2 | import { format as formatNumber } from './number.js';
|
3 | import { format as formatBigNumber } from './bignumber/formatter.js';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export function endsWith(text, search) {
|
11 | var start = text.length - search.length;
|
12 | var end = text.length;
|
13 | return text.substring(start, end) === search;
|
14 | }
|
15 |
|
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 |
|
54 | export function format(value, options) {
|
55 | var result = _format(value, options);
|
56 |
|
57 | if (options && typeof options === 'object' && 'truncate' in options && result.length > options.truncate) {
|
58 | return result.substring(0, options.truncate - 3) + '...';
|
59 | }
|
60 |
|
61 | return result;
|
62 | }
|
63 |
|
64 | function _format(value, options) {
|
65 | if (typeof value === 'number') {
|
66 | return formatNumber(value, options);
|
67 | }
|
68 |
|
69 | if (isBigNumber(value)) {
|
70 | return formatBigNumber(value, options);
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 | if (looksLikeFraction(value)) {
|
76 | if (!options || options.fraction !== 'decimal') {
|
77 |
|
78 | return value.s * value.n + '/' + value.d;
|
79 | } else {
|
80 |
|
81 | return value.toString();
|
82 | }
|
83 | }
|
84 |
|
85 | if (Array.isArray(value)) {
|
86 | return formatArray(value, options);
|
87 | }
|
88 |
|
89 | if (isString(value)) {
|
90 | return '"' + value + '"';
|
91 | }
|
92 |
|
93 | if (typeof value === 'function') {
|
94 | return value.syntax ? String(value.syntax) : 'function';
|
95 | }
|
96 |
|
97 | if (value && typeof value === 'object') {
|
98 | if (typeof value.format === 'function') {
|
99 | return value.format(options);
|
100 | } else if (value && value.toString(options) !== {}.toString()) {
|
101 |
|
102 | return value.toString(options);
|
103 | } else {
|
104 | var entries = Object.keys(value).map(key => {
|
105 | return '"' + key + '": ' + format(value[key], options);
|
106 | });
|
107 | return '{' + entries.join(', ') + '}';
|
108 | }
|
109 | }
|
110 |
|
111 | return String(value);
|
112 | }
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | export function stringify(value) {
|
122 | var text = String(value);
|
123 | var escaped = '';
|
124 | var i = 0;
|
125 |
|
126 | while (i < text.length) {
|
127 | var c = text.charAt(i);
|
128 |
|
129 | if (c === '\\') {
|
130 | escaped += c;
|
131 | i++;
|
132 | c = text.charAt(i);
|
133 |
|
134 | if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) {
|
135 | escaped += '\\';
|
136 | }
|
137 |
|
138 | escaped += c;
|
139 | } else if (c === '"') {
|
140 | escaped += '\\"';
|
141 | } else {
|
142 | escaped += c;
|
143 | }
|
144 |
|
145 | i++;
|
146 | }
|
147 |
|
148 | return '"' + escaped + '"';
|
149 | }
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | export function escape(value) {
|
157 | var text = String(value);
|
158 | text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
159 | return text;
|
160 | }
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 | function 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 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 | function looksLikeFraction(value) {
|
199 | return value && typeof value === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false;
|
200 | }
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | export function compareText(x, y) {
|
210 |
|
211 | if (!isString(x)) {
|
212 | throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + typeOf(x) + ', index: 0)');
|
213 | }
|
214 |
|
215 | if (!isString(y)) {
|
216 | throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + typeOf(y) + ', index: 1)');
|
217 | }
|
218 |
|
219 | return x === y ? 0 : x > y ? 1 : -1;
|
220 | } |
\ | No newline at end of file |