1 | 'use strict';
|
2 |
|
3 | function _typeof(obj) { 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); }
|
4 |
|
5 | var formatNumber = require('./number').format;
|
6 |
|
7 | var formatBigNumber = require('./bignumber/formatter').format;
|
8 |
|
9 | var isBigNumber = require('./bignumber/isBigNumber');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | exports.isString = function (value) {
|
18 | return typeof value === 'string';
|
19 | };
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | exports.endsWith = function (text, search) {
|
28 | var start = text.length - search.length;
|
29 | var end = text.length;
|
30 | return text.substring(start, end) === search;
|
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 |
|
68 |
|
69 | exports.format = function (value, options) {
|
70 | if (typeof value === 'number') {
|
71 | return formatNumber(value, options);
|
72 | }
|
73 |
|
74 | if (isBigNumber(value)) {
|
75 | return formatBigNumber(value, options);
|
76 | }
|
77 |
|
78 |
|
79 |
|
80 | if (looksLikeFraction(value)) {
|
81 | if (!options || options.fraction !== 'decimal') {
|
82 |
|
83 | return value.s * value.n + '/' + value.d;
|
84 | } else {
|
85 |
|
86 | return value.toString();
|
87 | }
|
88 | }
|
89 |
|
90 | if (Array.isArray(value)) {
|
91 | return formatArray(value, options);
|
92 | }
|
93 |
|
94 | if (exports.isString(value)) {
|
95 | return '"' + value + '"';
|
96 | }
|
97 |
|
98 | if (typeof value === 'function') {
|
99 | return value.syntax ? String(value.syntax) : 'function';
|
100 | }
|
101 |
|
102 | if (value && _typeof(value) === 'object') {
|
103 | if (typeof value.format === 'function') {
|
104 | return value.format(options);
|
105 | } else if (value && value.toString() !== {}.toString()) {
|
106 |
|
107 | return value.toString();
|
108 | } else {
|
109 | var entries = [];
|
110 |
|
111 | for (var key in value) {
|
112 | if (value.hasOwnProperty(key)) {
|
113 | entries.push('"' + key + '": ' + exports.format(value[key], options));
|
114 | }
|
115 | }
|
116 |
|
117 | return '{' + entries.join(', ') + '}';
|
118 | }
|
119 | }
|
120 |
|
121 | return String(value);
|
122 | };
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | exports.stringify = function (value) {
|
132 | var text = String(value);
|
133 | var escaped = '';
|
134 | var i = 0;
|
135 |
|
136 | while (i < text.length) {
|
137 | var c = text.charAt(i);
|
138 |
|
139 | if (c === '\\') {
|
140 | escaped += c;
|
141 | i++;
|
142 | c = text.charAt(i);
|
143 |
|
144 | if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) {
|
145 | escaped += '\\';
|
146 | }
|
147 |
|
148 | escaped += c;
|
149 | } else if (c === '"') {
|
150 | escaped += '\\"';
|
151 | } else {
|
152 | escaped += c;
|
153 | }
|
154 |
|
155 | i++;
|
156 | }
|
157 |
|
158 | return '"' + escaped + '"';
|
159 | };
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 | exports.escape = function (value) {
|
168 | var text = String(value);
|
169 | text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
170 | return text;
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | function formatArray(array, options) {
|
185 | if (Array.isArray(array)) {
|
186 | var str = '[';
|
187 | var len = array.length;
|
188 |
|
189 | for (var i = 0; i < len; i++) {
|
190 | if (i !== 0) {
|
191 | str += ', ';
|
192 | }
|
193 |
|
194 | str += formatArray(array[i], options);
|
195 | }
|
196 |
|
197 | str += ']';
|
198 | return str;
|
199 | } else {
|
200 | return exports.format(array, options);
|
201 | }
|
202 | }
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | function looksLikeFraction(value) {
|
211 | return value && _typeof(value) === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false;
|
212 | } |
\ | No newline at end of file |