1 | 'use strict';
|
2 |
|
3 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
4 |
|
5 | var formatNumber = require('./number').format;
|
6 | var formatBigNumber = require('./bignumber/formatter').format;
|
7 | var isBigNumber = require('./bignumber/isBigNumber');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | exports.isString = function (value) {
|
15 | return typeof value === 'string';
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | exports.endsWith = function (text, search) {
|
24 | var start = text.length - search.length;
|
25 | var end = text.length;
|
26 | return text.substring(start, end) === search;
|
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 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | exports.format = function (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 (exports.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 === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
|
98 | if (typeof value.format === 'function') {
|
99 | return value.format(options);
|
100 | } else if (value && value.toString() !== {}.toString()) {
|
101 |
|
102 | return value.toString();
|
103 | } else {
|
104 | var entries = [];
|
105 |
|
106 | for (var key in value) {
|
107 | if (value.hasOwnProperty(key)) {
|
108 | entries.push('"' + key + '": ' + exports.format(value[key], options));
|
109 | }
|
110 | }
|
111 |
|
112 | return '{' + entries.join(', ') + '}';
|
113 | }
|
114 | }
|
115 |
|
116 | return String(value);
|
117 | };
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | exports.stringify = function (value) {
|
126 | var text = String(value);
|
127 | var escaped = '';
|
128 | var i = 0;
|
129 | while (i < text.length) {
|
130 | var c = text.charAt(i);
|
131 |
|
132 | if (c === '\\') {
|
133 | escaped += c;
|
134 | i++;
|
135 |
|
136 | c = text.charAt(i);
|
137 | if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) {
|
138 | escaped += '\\';
|
139 | }
|
140 | escaped += c;
|
141 | } else if (c === '"') {
|
142 | escaped += '\\"';
|
143 | } else {
|
144 | escaped += c;
|
145 | }
|
146 | i++;
|
147 | }
|
148 |
|
149 | return '"' + escaped + '"';
|
150 | };
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 | exports.escape = function (value) {
|
158 | var text = String(value);
|
159 | text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
160 |
|
161 | return text;
|
162 | };
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | function formatArray(array, options) {
|
175 | if (Array.isArray(array)) {
|
176 | var str = '[';
|
177 | var len = array.length;
|
178 | for (var i = 0; i < len; i++) {
|
179 | if (i !== 0) {
|
180 | str += ', ';
|
181 | }
|
182 | str += formatArray(array[i], options);
|
183 | }
|
184 | str += ']';
|
185 | return str;
|
186 | } else {
|
187 | return exports.format(array, options);
|
188 | }
|
189 | }
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 | function looksLikeFraction(value) {
|
197 | return value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object' && typeof value.s === 'number' && typeof value.n === 'number' && typeof value.d === 'number' || false;
|
198 | } |
\ | No newline at end of file |