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 | export function format(value, options) {
|
52 | if (typeof value === 'number') {
|
53 | return formatNumber(value, options);
|
54 | }
|
55 |
|
56 | if (isBigNumber(value)) {
|
57 | return formatBigNumber(value, options);
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 | if (looksLikeFraction(value)) {
|
63 | if (!options || options.fraction !== 'decimal') {
|
64 |
|
65 | return value.s * value.n + '/' + value.d;
|
66 | } else {
|
67 |
|
68 | return value.toString();
|
69 | }
|
70 | }
|
71 |
|
72 | if (Array.isArray(value)) {
|
73 | return formatArray(value, options);
|
74 | }
|
75 |
|
76 | if (isString(value)) {
|
77 | return '"' + value + '"';
|
78 | }
|
79 |
|
80 | if (typeof value === 'function') {
|
81 | return value.syntax ? String(value.syntax) : 'function';
|
82 | }
|
83 |
|
84 | if (value && typeof value === 'object') {
|
85 | if (typeof value.format === 'function') {
|
86 | return value.format(options);
|
87 | } else if (value && value.toString(options) !== {}.toString()) {
|
88 |
|
89 | return value.toString(options);
|
90 | } else {
|
91 | var entries = Object.keys(value).map(key => {
|
92 | return '"' + key + '": ' + format(value[key], options);
|
93 | });
|
94 | return '{' + entries.join(', ') + '}';
|
95 | }
|
96 | }
|
97 |
|
98 | return String(value);
|
99 | }
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | export function stringify(value) {
|
108 | var text = String(value);
|
109 | var escaped = '';
|
110 | var i = 0;
|
111 |
|
112 | while (i < text.length) {
|
113 | var c = text.charAt(i);
|
114 |
|
115 | if (c === '\\') {
|
116 | escaped += c;
|
117 | i++;
|
118 | c = text.charAt(i);
|
119 |
|
120 | if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) {
|
121 | escaped += '\\';
|
122 | }
|
123 |
|
124 | escaped += c;
|
125 | } else if (c === '"') {
|
126 | escaped += '\\"';
|
127 | } else {
|
128 | escaped += c;
|
129 | }
|
130 |
|
131 | i++;
|
132 | }
|
133 |
|
134 | return '"' + escaped + '"';
|
135 | }
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | export function escape(value) {
|
143 | var text = String(value);
|
144 | text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
145 | return text;
|
146 | }
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | function formatArray(array, options) {
|
159 | if (Array.isArray(array)) {
|
160 | var str = '[';
|
161 | var len = array.length;
|
162 |
|
163 | for (var i = 0; i < len; i++) {
|
164 | if (i !== 0) {
|
165 | str += ', ';
|
166 | }
|
167 |
|
168 | str += formatArray(array[i], options);
|
169 | }
|
170 |
|
171 | str += ']';
|
172 | return str;
|
173 | } else {
|
174 | return format(array, options);
|
175 | }
|
176 | }
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | function looksLikeFraction(value) {
|
185 | return value && typeof value === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false;
|
186 | }
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | export function compareText(x, y) {
|
196 |
|
197 | if (!isString(x)) {
|
198 | throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + typeOf(x) + ', index: 0)');
|
199 | }
|
200 |
|
201 | if (!isString(y)) {
|
202 | throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + typeOf(y) + ', index: 1)');
|
203 | }
|
204 |
|
205 | return x === y ? 0 : x > y ? 1 : -1;
|
206 | } |
\ | No newline at end of file |