1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.endsWith = endsWith;
|
7 | exports.format = format;
|
8 | exports.stringify = stringify;
|
9 | exports.escape = escape;
|
10 | exports.compareText = compareText;
|
11 |
|
12 | var _is = require("./is");
|
13 |
|
14 | var _number = require("./number");
|
15 |
|
16 | var _formatter = require("./bignumber/formatter");
|
17 |
|
18 | 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); }
|
19 |
|
20 | function endsWith(text, search) {
|
21 | var start = text.length - search.length;
|
22 | var end = text.length;
|
23 | return text.substring(start, end) === search;
|
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 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function format(value, options) {
|
63 | if (typeof value === 'number') {
|
64 | return (0, _number.format)(value, options);
|
65 | }
|
66 |
|
67 | if ((0, _is.isBigNumber)(value)) {
|
68 | return (0, _formatter.format)(value, options);
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | if (looksLikeFraction(value)) {
|
74 | if (!options || options.fraction !== 'decimal') {
|
75 |
|
76 | return value.s * value.n + '/' + value.d;
|
77 | } else {
|
78 |
|
79 | return value.toString();
|
80 | }
|
81 | }
|
82 |
|
83 | if (Array.isArray(value)) {
|
84 | return formatArray(value, options);
|
85 | }
|
86 |
|
87 | if ((0, _is.isString)(value)) {
|
88 | return '"' + value + '"';
|
89 | }
|
90 |
|
91 | if (typeof value === 'function') {
|
92 | return value.syntax ? String(value.syntax) : 'function';
|
93 | }
|
94 |
|
95 | if (value && _typeof(value) === 'object') {
|
96 | if (typeof value.format === 'function') {
|
97 | return value.format(options);
|
98 | } else if (value && value.toString(options) !== {}.toString()) {
|
99 |
|
100 | return value.toString(options);
|
101 | } else {
|
102 | var entries = Object.keys(value).map(function (key) {
|
103 | return '"' + key + '": ' + format(value[key], options);
|
104 | });
|
105 | return '{' + entries.join(', ') + '}';
|
106 | }
|
107 | }
|
108 |
|
109 | return String(value);
|
110 | }
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | function stringify(value) {
|
120 | var text = String(value);
|
121 | var escaped = '';
|
122 | var i = 0;
|
123 |
|
124 | while (i < text.length) {
|
125 | var c = text.charAt(i);
|
126 |
|
127 | if (c === '\\') {
|
128 | escaped += c;
|
129 | i++;
|
130 | c = text.charAt(i);
|
131 |
|
132 | if (c === '' || '"\\/bfnrtu'.indexOf(c) === -1) {
|
133 | escaped += '\\';
|
134 | }
|
135 |
|
136 | escaped += c;
|
137 | } else if (c === '"') {
|
138 | escaped += '\\"';
|
139 | } else {
|
140 | escaped += c;
|
141 | }
|
142 |
|
143 | i++;
|
144 | }
|
145 |
|
146 | return '"' + escaped + '"';
|
147 | }
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 | function escape(value) {
|
156 | var text = String(value);
|
157 | text = text.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>');
|
158 | return text;
|
159 | }
|
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 | function compareText(x, y) {
|
210 |
|
211 | if (!(0, _is.isString)(x)) {
|
212 | throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + (0, _is.typeOf)(x) + ', index: 0)');
|
213 | }
|
214 |
|
215 | if (!(0, _is.isString)(y)) {
|
216 | throw new TypeError('Unexpected type of argument in function compareText ' + '(expected: string or Array or Matrix, actual: ' + (0, _is.typeOf)(y) + ', index: 1)');
|
217 | }
|
218 |
|
219 | return x === y ? 0 : x > y ? 1 : -1;
|
220 | } |
\ | No newline at end of file |