@jsenv/humanize 1.6.0 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/jsenv_humanize_browser.js +539 -482
- package/dist/browser/jsenv_humanize_browser.js.map +76 -0
- package/dist/node/jsenv_humanize_node.js +36 -37
- package/dist/node/jsenv_humanize_node.js.map +97 -0
- package/dist/node/jsenv_humanize_node_modules.js +23 -15
- package/dist/node/jsenv_humanize_node_modules.js.map +21 -0
- package/package.json +18 -19
- package/src/_tests/byte/file_size.test.js +0 -18
- package/src/_tests/byte/memory.test.js +0 -11
- package/src/_tests/content_frame/content_frame.test.js +0 -155
- package/src/_tests/content_frame/snapshots/10_line_and_column_are_zero.txt +0 -2
- package/src/_tests/content_frame/snapshots/11_line_is_last.txt +0 -3
- package/src/_tests/content_frame/snapshots/1_basic.txt +0 -4
- package/src/_tests/content_frame/snapshots/2_basic_2.txt +0 -6
- package/src/_tests/content_frame/snapshots/3_empty_last_line.txt +0 -7
- package/src/_tests/content_frame/snapshots/4_no_column.txt +0 -3
- package/src/_tests/content_frame/snapshots/5_line_1_and_2_digits.txt +0 -3
- package/src/_tests/content_frame/snapshots/6_line_too_long_column_undefined.txt +0 -3
- package/src/_tests/content_frame/snapshots/7_line_too_long_column_near_start.txt +0 -4
- package/src/_tests/content_frame/snapshots/8_line_too_long_column_near_middle.txt +0 -4
- package/src/_tests/content_frame/snapshots/9_line_too_long_column_near_end.txt +0 -4
- package/src/_tests/js_value/_inspect.test.html +0 -22
- package/src/_tests/js_value/array.test.js +0 -72
- package/src/_tests/js_value/biginit.test.js +0 -35
- package/src/_tests/js_value/boolean.test.js +0 -45
- package/src/_tests/js_value/date.test.js +0 -15
- package/src/_tests/js_value/error.test.js +0 -33
- package/src/_tests/js_value/function.test.js +0 -51
- package/src/_tests/js_value/instance.test.js +0 -12
- package/src/_tests/js_value/null.test.js +0 -6
- package/src/_tests/js_value/number.test.js +0 -182
- package/src/_tests/js_value/object.test.js +0 -173
- package/src/_tests/js_value/regexp.test.js +0 -15
- package/src/_tests/js_value/string.test.js +0 -106
- package/src/_tests/js_value/symbol.test.js +0 -20
- package/src/_tests/js_value/undefined.test.js +0 -6
- package/src/_tests/log/log_long.test_manual.mjs +0 -39
- package/src/_tests/log/log_modified.test_manual.mjs +0 -7
- package/src/_tests/log/logger.test.mjs +0 -90
- package/src/_tests/log/spinner.duration.test_manual.mjs +0 -25
- package/src/_tests/log/spinner.test.mjs +0 -33
- package/src/_tests/log/spinner.test_manual.mjs +0 -15
- package/src/_tests/percentage/percentage_distribution.test.mjs +0 -89
- package/src/_tests/time/decimals.test.js +0 -119
- package/src/_tests/time/duration.test.js +0 -20
- package/src/_tests/time/ellapsed.test.js +0 -10
- package/src/ansi/ansi_browser.js +0 -3
- package/src/ansi/ansi_node.js +0 -13
- package/src/ansi/ansi_runtime_agnostic.js +0 -107
- package/src/byte/byte.js +0 -44
- package/src/content_frame/content_frame.js +0 -157
- package/src/error/error_to_html.js +0 -22
- package/src/error/error_to_markdown.js +0 -14
- package/src/error/format_error.js +0 -53
- package/src/js_value/array.js +0 -58
- package/src/js_value/bigint.js +0 -3
- package/src/js_value/bigint_object.js +0 -5
- package/src/js_value/boolean.js +0 -1
- package/src/js_value/boolean_object.js +0 -12
- package/src/js_value/constructor.js +0 -10
- package/src/js_value/date.js +0 -8
- package/src/js_value/error.js +0 -34
- package/src/js_value/function.js +0 -20
- package/src/js_value/humanize.js +0 -142
- package/src/js_value/null.js +0 -1
- package/src/js_value/number.js +0 -101
- package/src/js_value/number_object.js +0 -9
- package/src/js_value/object.js +0 -78
- package/src/js_value/regexp.js +0 -1
- package/src/js_value/string.js +0 -141
- package/src/js_value/string_object.js +0 -10
- package/src/js_value/symbol.js +0 -24
- package/src/js_value/undefined.js +0 -1
- package/src/log/LOG_LEVELS.js +0 -9
- package/src/log/call_orderer.js +0 -39
- package/src/log/detailed_message.js +0 -29
- package/src/log/details.js +0 -26
- package/src/log/dynamic_log.js +0 -186
- package/src/log/logger.js +0 -85
- package/src/log/section.js +0 -50
- package/src/log/spinner.js +0 -72
- package/src/log/task_log.js +0 -49
- package/src/main_browser.js +0 -13
- package/src/main_node.js +0 -25
- package/src/percentage/distribute_percentages.js +0 -53
- package/src/time/time.js +0 -151
- package/src/unicode/unicode_browser.js +0 -7
- package/src/unicode/unicode_node.js +0 -8
- package/src/unicode/unicode_runtime_agnostic.js +0 -56
- package/src/utils/decimals.js +0 -95
- package/src/utils/indentation.js +0 -38
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
const createDetailedMessage = (message, details = {}) => {
|
|
2
|
-
let text =
|
|
2
|
+
let text = `${message}`;
|
|
3
3
|
const namedSectionsText = renderNamedSections(details);
|
|
4
4
|
if (namedSectionsText) {
|
|
5
|
-
text +=
|
|
5
|
+
text += `
|
|
6
|
+
${namedSectionsText}`;
|
|
6
7
|
}
|
|
7
8
|
return text;
|
|
8
9
|
};
|
|
9
|
-
|
|
10
|
+
|
|
11
|
+
const renderNamedSections = (namedSections) => {
|
|
10
12
|
let text = "";
|
|
11
13
|
let keys = Object.keys(namedSections);
|
|
12
14
|
for (const key of keys) {
|
|
13
15
|
const isLastKey = key === keys[keys.length - 1];
|
|
14
16
|
const value = namedSections[key];
|
|
15
|
-
text +=
|
|
17
|
+
text += `--- ${key} ---
|
|
18
|
+
${
|
|
19
|
+
Array.isArray(value)
|
|
20
|
+
? value.join(`
|
|
21
|
+
`)
|
|
22
|
+
: value
|
|
23
|
+
}`;
|
|
16
24
|
if (!isLastKey) {
|
|
17
25
|
text += "\n";
|
|
18
26
|
}
|
|
@@ -23,6 +31,7 @@ const renderNamedSections = namedSections => {
|
|
|
23
31
|
// https://github.com/Marak/colors.js/blob/master/lib/styles.js
|
|
24
32
|
// https://stackoverflow.com/a/75985833/2634179
|
|
25
33
|
const RESET = "\x1b[0m";
|
|
34
|
+
|
|
26
35
|
const RED = "red";
|
|
27
36
|
const GREEN = "green";
|
|
28
37
|
const YELLOW = "yellow";
|
|
@@ -32,6 +41,7 @@ const CYAN = "cyan";
|
|
|
32
41
|
const GREY = "grey";
|
|
33
42
|
const WHITE = "white";
|
|
34
43
|
const BLACK = "black";
|
|
44
|
+
|
|
35
45
|
const TEXT_COLOR_ANSI_CODES = {
|
|
36
46
|
[RED]: "\x1b[31m",
|
|
37
47
|
[GREEN]: "\x1b[32m",
|
|
@@ -41,7 +51,7 @@ const TEXT_COLOR_ANSI_CODES = {
|
|
|
41
51
|
[CYAN]: "\x1b[36m",
|
|
42
52
|
[GREY]: "\x1b[90m",
|
|
43
53
|
[WHITE]: "\x1b[37m",
|
|
44
|
-
[BLACK]: "\x1b[30m"
|
|
54
|
+
[BLACK]: "\x1b[30m",
|
|
45
55
|
};
|
|
46
56
|
const BACKGROUND_COLOR_ANSI_CODES = {
|
|
47
57
|
[RED]: "\x1b[41m",
|
|
@@ -52,13 +62,13 @@ const BACKGROUND_COLOR_ANSI_CODES = {
|
|
|
52
62
|
[CYAN]: "\x1b[46m",
|
|
53
63
|
[GREY]: "\x1b[100m",
|
|
54
64
|
[WHITE]: "\x1b[47m",
|
|
55
|
-
[BLACK]: "\x1b[40m"
|
|
65
|
+
[BLACK]: "\x1b[40m",
|
|
56
66
|
};
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}) => {
|
|
67
|
+
|
|
68
|
+
const createAnsi = ({ supported }) => {
|
|
60
69
|
const ANSI = {
|
|
61
70
|
supported,
|
|
71
|
+
|
|
62
72
|
RED,
|
|
63
73
|
GREEN,
|
|
64
74
|
YELLOW,
|
|
@@ -83,7 +93,7 @@ const createAnsi = ({
|
|
|
83
93
|
if (!ansiEscapeCodeForTextColor) {
|
|
84
94
|
return text;
|
|
85
95
|
}
|
|
86
|
-
return
|
|
96
|
+
return `${ansiEscapeCodeForTextColor}${text}${RESET}`;
|
|
87
97
|
},
|
|
88
98
|
backgroundColor: (text, color) => {
|
|
89
99
|
if (!ANSI.supported) {
|
|
@@ -96,12 +106,14 @@ const createAnsi = ({
|
|
|
96
106
|
// cannot set background color of blank chars
|
|
97
107
|
return text;
|
|
98
108
|
}
|
|
99
|
-
const ansiEscapeCodeForBackgroundColor =
|
|
109
|
+
const ansiEscapeCodeForBackgroundColor =
|
|
110
|
+
BACKGROUND_COLOR_ANSI_CODES[color];
|
|
100
111
|
if (!ansiEscapeCodeForBackgroundColor) {
|
|
101
112
|
return text;
|
|
102
113
|
}
|
|
103
|
-
return
|
|
114
|
+
return `${ansiEscapeCodeForBackgroundColor}${text}${RESET}`;
|
|
104
115
|
},
|
|
116
|
+
|
|
105
117
|
BOLD: "\x1b[1m",
|
|
106
118
|
UNDERLINE: "\x1b[4m",
|
|
107
119
|
STRIKE: "\x1b[9m",
|
|
@@ -117,47 +129,43 @@ const createAnsi = ({
|
|
|
117
129
|
return text;
|
|
118
130
|
}
|
|
119
131
|
const ansiEscapeCodeForEffect = effect;
|
|
120
|
-
return
|
|
121
|
-
}
|
|
132
|
+
return `${ansiEscapeCodeForEffect}${text}${RESET}`;
|
|
133
|
+
},
|
|
122
134
|
};
|
|
135
|
+
|
|
123
136
|
return ANSI;
|
|
124
137
|
};
|
|
125
138
|
|
|
126
|
-
const ANSI = createAnsi({
|
|
127
|
-
supported: true
|
|
128
|
-
});
|
|
139
|
+
const ANSI = createAnsi({ supported: true });
|
|
129
140
|
|
|
130
141
|
// see also https://github.com/sindresorhus/figures
|
|
131
142
|
|
|
132
|
-
const createUnicode = ({
|
|
133
|
-
supported,
|
|
134
|
-
ANSI
|
|
135
|
-
}) => {
|
|
143
|
+
const createUnicode = ({ supported, ANSI }) => {
|
|
136
144
|
const UNICODE = {
|
|
137
145
|
supported,
|
|
138
146
|
get COMMAND_RAW() {
|
|
139
|
-
return UNICODE.supported ?
|
|
147
|
+
return UNICODE.supported ? `❯` : `>`;
|
|
140
148
|
},
|
|
141
149
|
get OK_RAW() {
|
|
142
|
-
return UNICODE.supported ?
|
|
150
|
+
return UNICODE.supported ? `✔` : `√`;
|
|
143
151
|
},
|
|
144
152
|
get FAILURE_RAW() {
|
|
145
|
-
return UNICODE.supported ?
|
|
153
|
+
return UNICODE.supported ? `✖` : `×`;
|
|
146
154
|
},
|
|
147
155
|
get DEBUG_RAW() {
|
|
148
|
-
return UNICODE.supported ?
|
|
156
|
+
return UNICODE.supported ? `◆` : `♦`;
|
|
149
157
|
},
|
|
150
158
|
get INFO_RAW() {
|
|
151
|
-
return UNICODE.supported ?
|
|
159
|
+
return UNICODE.supported ? `ℹ` : `i`;
|
|
152
160
|
},
|
|
153
161
|
get WARNING_RAW() {
|
|
154
|
-
return UNICODE.supported ?
|
|
162
|
+
return UNICODE.supported ? `⚠` : `‼`;
|
|
155
163
|
},
|
|
156
164
|
get CIRCLE_CROSS_RAW() {
|
|
157
|
-
return UNICODE.supported ?
|
|
165
|
+
return UNICODE.supported ? `ⓧ` : `(×)`;
|
|
158
166
|
},
|
|
159
167
|
get CIRCLE_DOTTED_RAW() {
|
|
160
|
-
return UNICODE.supported ?
|
|
168
|
+
return UNICODE.supported ? `◌` : `*`;
|
|
161
169
|
},
|
|
162
170
|
get COMMAND() {
|
|
163
171
|
return ANSI.color(UNICODE.COMMAND_RAW, ANSI.GREY); // ANSI_MAGENTA)
|
|
@@ -181,26 +189,24 @@ const createUnicode = ({
|
|
|
181
189
|
return ANSI.color(UNICODE.CIRCLE_CROSS_RAW, ANSI.RED);
|
|
182
190
|
},
|
|
183
191
|
get ELLIPSIS() {
|
|
184
|
-
return UNICODE.supported ?
|
|
185
|
-
}
|
|
192
|
+
return UNICODE.supported ? `…` : `...`;
|
|
193
|
+
},
|
|
186
194
|
};
|
|
187
195
|
return UNICODE;
|
|
188
196
|
};
|
|
189
197
|
|
|
190
198
|
const UNICODE = createUnicode({
|
|
191
199
|
supported: true,
|
|
192
|
-
ANSI
|
|
200
|
+
ANSI,
|
|
193
201
|
});
|
|
194
202
|
|
|
195
|
-
const inspectBoolean = value => value.toString();
|
|
203
|
+
const inspectBoolean = (value) => value.toString();
|
|
196
204
|
|
|
197
205
|
const inspectNull = () => "null";
|
|
198
206
|
|
|
199
207
|
// https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/rules/numeric-separators-style.js
|
|
200
208
|
|
|
201
|
-
const inspectNumber = (value, {
|
|
202
|
-
numericSeparator
|
|
203
|
-
}) => {
|
|
209
|
+
const inspectNumber = (value, { numericSeparator }) => {
|
|
204
210
|
if (isNegativeZero(value)) {
|
|
205
211
|
return "-0";
|
|
206
212
|
}
|
|
@@ -223,66 +229,65 @@ const inspectNumber = (value, {
|
|
|
223
229
|
number,
|
|
224
230
|
mark = "",
|
|
225
231
|
sign = "",
|
|
226
|
-
power = ""
|
|
227
|
-
} = numberString.match(
|
|
232
|
+
power = "",
|
|
233
|
+
} = numberString.match(
|
|
234
|
+
/^(?<number>.*?)(?:(?<mark>e)(?<sign>[+-])?(?<power>\d+))?$/i,
|
|
235
|
+
).groups;
|
|
228
236
|
const numberWithSeparators = formatNumber(number);
|
|
229
237
|
const powerWithSeparators = addSeparator(power, {
|
|
230
238
|
minimumDigits: 5,
|
|
231
|
-
groupLength: 3
|
|
239
|
+
groupLength: 3,
|
|
232
240
|
});
|
|
233
|
-
return
|
|
241
|
+
return `${numberWithSeparators}${mark}${sign}${powerWithSeparators}`;
|
|
234
242
|
};
|
|
235
243
|
|
|
236
244
|
// Use this and instead of Object.is(value, -0)
|
|
237
245
|
// because in some corner cases firefox returns false
|
|
238
246
|
// for Object.is(-0, -0)
|
|
239
|
-
const isNegativeZero = value => {
|
|
247
|
+
const isNegativeZero = (value) => {
|
|
240
248
|
return value === 0 && 1 / value === -Infinity;
|
|
241
249
|
};
|
|
242
|
-
|
|
250
|
+
|
|
251
|
+
const formatNumber = (numberString) => {
|
|
243
252
|
const parts = numberString.split(".");
|
|
244
253
|
const [integer, fractional] = parts;
|
|
254
|
+
|
|
245
255
|
if (parts.length === 2) {
|
|
246
256
|
const integerWithSeparators = addSeparator(integer, {
|
|
247
257
|
minimumDigits: 5,
|
|
248
|
-
groupLength: 3
|
|
258
|
+
groupLength: 3,
|
|
249
259
|
});
|
|
250
|
-
return
|
|
260
|
+
return `${integerWithSeparators}.${fractional}`;
|
|
251
261
|
}
|
|
262
|
+
|
|
252
263
|
return addSeparator(integer, {
|
|
253
264
|
minimumDigits: 5,
|
|
254
|
-
groupLength: 3
|
|
265
|
+
groupLength: 3,
|
|
255
266
|
});
|
|
256
267
|
};
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
groupLength
|
|
260
|
-
}) => {
|
|
268
|
+
|
|
269
|
+
const addSeparator = (numberString, { minimumDigits, groupLength }) => {
|
|
261
270
|
if (numberString[0] === "-") {
|
|
262
|
-
return
|
|
271
|
+
return `-${groupDigits(numberString.slice(1), {
|
|
263
272
|
minimumDigits,
|
|
264
|
-
groupLength
|
|
265
|
-
})
|
|
273
|
+
groupLength,
|
|
274
|
+
})}`;
|
|
266
275
|
}
|
|
267
|
-
return groupDigits(numberString, {
|
|
268
|
-
minimumDigits,
|
|
269
|
-
groupLength
|
|
270
|
-
});
|
|
276
|
+
return groupDigits(numberString, { minimumDigits, groupLength });
|
|
271
277
|
};
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
groupLength
|
|
275
|
-
}) => {
|
|
278
|
+
|
|
279
|
+
const groupDigits = (digits, { minimumDigits, groupLength }) => {
|
|
276
280
|
const digitCount = digits.length;
|
|
277
281
|
if (digitCount < minimumDigits) {
|
|
278
282
|
return digits;
|
|
279
283
|
}
|
|
280
|
-
|
|
281
|
-
let
|
|
284
|
+
|
|
285
|
+
let digitsWithSeparator = digits.slice(-groupLength);
|
|
286
|
+
let remainingDigits = digits.slice(0, -groupLength);
|
|
282
287
|
while (remainingDigits.length) {
|
|
283
|
-
const group = remainingDigits.slice(-
|
|
284
|
-
remainingDigits = remainingDigits.slice(0, -
|
|
285
|
-
digitsWithSeparator =
|
|
288
|
+
const group = remainingDigits.slice(-groupLength);
|
|
289
|
+
remainingDigits = remainingDigits.slice(0, -groupLength);
|
|
290
|
+
digitsWithSeparator = `${group}_${digitsWithSeparator}`;
|
|
286
291
|
}
|
|
287
292
|
return digitsWithSeparator;
|
|
288
293
|
};
|
|
@@ -301,46 +306,50 @@ const groupDigits = (digits, {
|
|
|
301
306
|
// return parts.join("_");
|
|
302
307
|
// };
|
|
303
308
|
|
|
304
|
-
const DOUBLE_QUOTE = "
|
|
305
|
-
const SINGLE_QUOTE =
|
|
309
|
+
const DOUBLE_QUOTE = `"`;
|
|
310
|
+
const SINGLE_QUOTE = `'`;
|
|
306
311
|
const BACKTICK = "`";
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
312
|
+
|
|
313
|
+
const inspectString = (
|
|
314
|
+
value,
|
|
315
|
+
{
|
|
316
|
+
quote = "auto",
|
|
317
|
+
canUseTemplateString = false,
|
|
318
|
+
preserveLineBreaks = false,
|
|
319
|
+
quoteDefault = DOUBLE_QUOTE,
|
|
320
|
+
} = {},
|
|
321
|
+
) => {
|
|
322
|
+
quote =
|
|
323
|
+
quote === "auto"
|
|
324
|
+
? determineQuote(value, { canUseTemplateString, quoteDefault })
|
|
325
|
+
: quote;
|
|
317
326
|
if (quote === BACKTICK) {
|
|
318
|
-
return
|
|
327
|
+
return `\`${escapeTemplateStringSpecialCharacters(value)}\``;
|
|
319
328
|
}
|
|
320
|
-
return surroundStringWith(value, {
|
|
321
|
-
quote,
|
|
322
|
-
preserveLineBreaks
|
|
323
|
-
});
|
|
329
|
+
return surroundStringWith(value, { quote, preserveLineBreaks });
|
|
324
330
|
};
|
|
325
331
|
|
|
326
332
|
// https://github.com/mgenware/string-to-template-literal/blob/main/src/main.ts#L1
|
|
327
|
-
const escapeTemplateStringSpecialCharacters = string => {
|
|
333
|
+
const escapeTemplateStringSpecialCharacters = (string) => {
|
|
328
334
|
string = String(string);
|
|
329
335
|
let i = 0;
|
|
330
336
|
let escapedString = "";
|
|
331
337
|
while (i < string.length) {
|
|
332
338
|
const char = string[i];
|
|
333
339
|
i++;
|
|
334
|
-
escapedString += isTemplateStringSpecialChar(char) ?
|
|
340
|
+
escapedString += isTemplateStringSpecialChar(char) ? `\\${char}` : char;
|
|
335
341
|
}
|
|
336
342
|
return escapedString;
|
|
337
343
|
};
|
|
338
|
-
|
|
344
|
+
|
|
345
|
+
const isTemplateStringSpecialChar = (char) =>
|
|
346
|
+
templateStringSpecialChars.indexOf(char) > -1;
|
|
339
347
|
const templateStringSpecialChars = ["\\", "`", "$"];
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
348
|
+
|
|
349
|
+
const determineQuote = (
|
|
350
|
+
string,
|
|
351
|
+
{ canUseTemplateString, quoteDefault = DOUBLE_QUOTE } = {},
|
|
352
|
+
) => {
|
|
344
353
|
const containsDoubleQuote = string.includes(DOUBLE_QUOTE);
|
|
345
354
|
if (!containsDoubleQuote) {
|
|
346
355
|
return DOUBLE_QUOTE;
|
|
@@ -365,18 +374,29 @@ const determineQuote = (string, {
|
|
|
365
374
|
}
|
|
366
375
|
return quoteDefault;
|
|
367
376
|
};
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
preserveLineBreaks
|
|
371
|
-
}) => {
|
|
377
|
+
|
|
378
|
+
const inspectChar = (char, { quote, preserveLineBreaks }) => {
|
|
372
379
|
const point = char.charCodeAt(0);
|
|
373
380
|
if (preserveLineBreaks && (char === "\n" || char === "\r")) {
|
|
374
381
|
return char;
|
|
375
382
|
}
|
|
376
|
-
if (
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
383
|
+
if (
|
|
384
|
+
char === quote ||
|
|
385
|
+
point === 92 ||
|
|
386
|
+
point < 32 ||
|
|
387
|
+
(point > 126 && point < 160) ||
|
|
388
|
+
// line separators
|
|
389
|
+
point === 8232 ||
|
|
390
|
+
point === 8233
|
|
391
|
+
) {
|
|
392
|
+
const replacement =
|
|
393
|
+
char === quote
|
|
394
|
+
? `\\${quote}`
|
|
395
|
+
: point === 8232
|
|
396
|
+
? "\\u2028"
|
|
397
|
+
: point === 8233
|
|
398
|
+
? "\\u2029"
|
|
399
|
+
: meta[point];
|
|
380
400
|
return replacement;
|
|
381
401
|
}
|
|
382
402
|
return char;
|
|
@@ -386,25 +406,19 @@ const inspectChar = (char, {
|
|
|
386
406
|
// https://github.com/jsenv/jsenv-inspect/blob/bb11de3adf262b68f71ed82b0a37d4528dd42229/src/internal/string.js#L3
|
|
387
407
|
// https://github.com/joliss/js-string-escape/blob/master/index.js
|
|
388
408
|
// http://javascript.crockford.com/remedial.html
|
|
389
|
-
const surroundStringWith = (string, {
|
|
390
|
-
quote,
|
|
391
|
-
preserveLineBreaks
|
|
392
|
-
}) => {
|
|
409
|
+
const surroundStringWith = (string, { quote, preserveLineBreaks }) => {
|
|
393
410
|
let result = "";
|
|
394
411
|
let last = 0;
|
|
395
412
|
const lastIndex = string.length;
|
|
396
413
|
let i = 0;
|
|
397
414
|
while (i < lastIndex) {
|
|
398
415
|
const char = string[i];
|
|
399
|
-
const replacement = inspectChar(char, {
|
|
400
|
-
quote,
|
|
401
|
-
preserveLineBreaks
|
|
402
|
-
});
|
|
416
|
+
const replacement = inspectChar(char, { quote, preserveLineBreaks });
|
|
403
417
|
if (char !== replacement) {
|
|
404
418
|
if (last === i) {
|
|
405
419
|
result += replacement;
|
|
406
420
|
} else {
|
|
407
|
-
result +=
|
|
421
|
+
result += `${string.slice(last, i)}${replacement}`;
|
|
408
422
|
}
|
|
409
423
|
last = i + 1;
|
|
410
424
|
}
|
|
@@ -413,90 +427,105 @@ const surroundStringWith = (string, {
|
|
|
413
427
|
if (last !== lastIndex) {
|
|
414
428
|
result += string.slice(last);
|
|
415
429
|
}
|
|
416
|
-
return
|
|
430
|
+
return `${quote}${result}${quote}`;
|
|
417
431
|
};
|
|
418
432
|
|
|
419
433
|
// prettier-ignore
|
|
420
|
-
const meta = [
|
|
421
|
-
// x07
|
|
422
|
-
'\\b', '\\t', '\\n', '\\x0B', '\\f', '\\r', '\\x0E', '\\x0F',
|
|
423
|
-
//
|
|
424
|
-
'\\
|
|
425
|
-
//
|
|
426
|
-
'
|
|
427
|
-
//
|
|
428
|
-
'', '', '', '', '', '', '',
|
|
429
|
-
//
|
|
430
|
-
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
|
|
431
|
-
//
|
|
432
|
-
'', '', '', '', '', '', '', '',
|
|
433
|
-
//
|
|
434
|
-
'', '', '', '', '', '', '', '',
|
|
435
|
-
// x5F
|
|
436
|
-
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
|
|
437
|
-
// x6F
|
|
438
|
-
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '\\x7F',
|
|
439
|
-
// x7F
|
|
440
|
-
'\\x80', '\\x81', '\\x82', '\\x83', '\\x84', '\\x85', '\\x86', '\\x87',
|
|
441
|
-
// x87
|
|
442
|
-
'\\x88', '\\x89', '\\x8A', '\\x8B', '\\x8C', '\\x8D', '\\x8E', '\\x8F',
|
|
443
|
-
// x8F
|
|
444
|
-
'\\x90', '\\x91', '\\x92', '\\x93', '\\x94', '\\x95', '\\x96', '\\x97',
|
|
445
|
-
// x97
|
|
446
|
-
'\\x98', '\\x99', '\\x9A', '\\x9B', '\\x9C', '\\x9D', '\\x9E', '\\x9F' // x9F
|
|
434
|
+
const meta = [
|
|
435
|
+
'\\x00', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\x07', // x07
|
|
436
|
+
'\\b', '\\t', '\\n', '\\x0B', '\\f', '\\r', '\\x0E', '\\x0F', // x0F
|
|
437
|
+
'\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17', // x17
|
|
438
|
+
'\\x18', '\\x19', '\\x1A', '\\x1B', '\\x1C', '\\x1D', '\\x1E', '\\x1F', // x1F
|
|
439
|
+
'', '', '', '', '', '', '', "\\'", '', '', '', '', '', '', '', '', // x2F
|
|
440
|
+
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x3F
|
|
441
|
+
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x4F
|
|
442
|
+
'', '', '', '', '', '', '', '', '', '', '', '', '\\\\', '', '', '', // x5F
|
|
443
|
+
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', // x6F
|
|
444
|
+
'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '\\x7F', // x7F
|
|
445
|
+
'\\x80', '\\x81', '\\x82', '\\x83', '\\x84', '\\x85', '\\x86', '\\x87', // x87
|
|
446
|
+
'\\x88', '\\x89', '\\x8A', '\\x8B', '\\x8C', '\\x8D', '\\x8E', '\\x8F', // x8F
|
|
447
|
+
'\\x90', '\\x91', '\\x92', '\\x93', '\\x94', '\\x95', '\\x96', '\\x97', // x97
|
|
448
|
+
'\\x98', '\\x99', '\\x9A', '\\x9B', '\\x9C', '\\x9D', '\\x9E', '\\x9F', // x9F
|
|
447
449
|
];
|
|
448
450
|
|
|
449
|
-
const inspectSymbol = (value, {
|
|
450
|
-
nestedHumanize,
|
|
451
|
-
parenthesis
|
|
452
|
-
}) => {
|
|
451
|
+
const inspectSymbol = (value, { nestedHumanize, parenthesis }) => {
|
|
453
452
|
const symbolDescription = symbolToDescription(value);
|
|
454
|
-
const symbolDescriptionSource = symbolDescription
|
|
455
|
-
|
|
456
|
-
|
|
453
|
+
const symbolDescriptionSource = symbolDescription
|
|
454
|
+
? nestedHumanize(symbolDescription)
|
|
455
|
+
: "";
|
|
456
|
+
const symbolSource = `Symbol(${symbolDescriptionSource})`;
|
|
457
|
+
|
|
458
|
+
if (parenthesis) return `${symbolSource}`;
|
|
457
459
|
return symbolSource;
|
|
458
460
|
};
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
461
|
+
|
|
462
|
+
const symbolToDescription =
|
|
463
|
+
"description" in Symbol.prototype
|
|
464
|
+
? (symbol) => symbol.description
|
|
465
|
+
: (symbol) => {
|
|
466
|
+
const toStringResult = symbol.toString();
|
|
467
|
+
const openingParenthesisIndex = toStringResult.indexOf("(");
|
|
468
|
+
const closingParenthesisIndex = toStringResult.indexOf(")");
|
|
469
|
+
const symbolDescription = toStringResult.slice(
|
|
470
|
+
openingParenthesisIndex + 1,
|
|
471
|
+
closingParenthesisIndex,
|
|
472
|
+
);
|
|
473
|
+
return symbolDescription;
|
|
474
|
+
};
|
|
466
475
|
|
|
467
476
|
const inspectUndefined = () => "undefined";
|
|
468
477
|
|
|
469
|
-
const inspectBigInt = value => {
|
|
470
|
-
return
|
|
478
|
+
const inspectBigInt = (value) => {
|
|
479
|
+
return `${value}n`;
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
const prefixFirstAndIndentRemainingLines = (
|
|
483
|
+
text,
|
|
484
|
+
{ prefix, indentation, trimLines, trimLastLine },
|
|
485
|
+
) => {
|
|
486
|
+
const lines = text.split(/\r?\n/);
|
|
487
|
+
const firstLine = lines.shift();
|
|
488
|
+
if (indentation === undefined) {
|
|
489
|
+
if (prefix) {
|
|
490
|
+
indentation = " "; // prefix + space
|
|
491
|
+
} else {
|
|
492
|
+
indentation = "";
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
let result = prefix ? `${prefix} ${firstLine}` : firstLine;
|
|
496
|
+
let i = 0;
|
|
497
|
+
while (i < lines.length) {
|
|
498
|
+
const line = trimLines ? lines[i].trim() : lines[i];
|
|
499
|
+
i++;
|
|
500
|
+
result += line.length
|
|
501
|
+
? `\n${indentation}${line}`
|
|
502
|
+
: trimLastLine && i === lines.length
|
|
503
|
+
? ""
|
|
504
|
+
: `\n`;
|
|
505
|
+
}
|
|
506
|
+
return result;
|
|
471
507
|
};
|
|
472
508
|
|
|
473
|
-
const preNewLineAndIndentation = (
|
|
474
|
-
|
|
475
|
-
indentUsingTab,
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
return "".concat(newLineAndIndent({
|
|
509
|
+
const preNewLineAndIndentation = (
|
|
510
|
+
value,
|
|
511
|
+
{ depth = 0, indentUsingTab, indentSize },
|
|
512
|
+
) => {
|
|
513
|
+
return `${newLineAndIndent({
|
|
479
514
|
count: depth + 1,
|
|
480
515
|
useTabs: indentUsingTab,
|
|
481
|
-
size: indentSize
|
|
482
|
-
})
|
|
516
|
+
size: indentSize,
|
|
517
|
+
})}${value}`;
|
|
483
518
|
};
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
indentUsingTab,
|
|
487
|
-
indentSize
|
|
488
|
-
}) => {
|
|
519
|
+
|
|
520
|
+
const postNewLineAndIndentation = ({ depth, indentUsingTab, indentSize }) => {
|
|
489
521
|
return newLineAndIndent({
|
|
490
522
|
count: depth,
|
|
491
523
|
useTabs: indentUsingTab,
|
|
492
|
-
size: indentSize
|
|
524
|
+
size: indentSize,
|
|
493
525
|
});
|
|
494
526
|
};
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
useTabs,
|
|
498
|
-
size
|
|
499
|
-
}) => {
|
|
527
|
+
|
|
528
|
+
const newLineAndIndent = ({ count, useTabs, size }) => {
|
|
500
529
|
if (useTabs) {
|
|
501
530
|
// eslint-disable-next-line prefer-template
|
|
502
531
|
return "\n" + "\t".repeat(count);
|
|
@@ -504,118 +533,116 @@ const newLineAndIndent = ({
|
|
|
504
533
|
// eslint-disable-next-line prefer-template
|
|
505
534
|
return "\n" + " ".repeat(count * size);
|
|
506
535
|
};
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
indentSize
|
|
511
|
-
|
|
512
|
-
return
|
|
513
|
-
depth,
|
|
514
|
-
indentUsingTab,
|
|
515
|
-
indentSize
|
|
516
|
-
})).concat(postNewLineAndIndentation({
|
|
536
|
+
|
|
537
|
+
const wrapNewLineAndIndentation = (
|
|
538
|
+
value,
|
|
539
|
+
{ depth = 0, indentUsingTab, indentSize },
|
|
540
|
+
) => {
|
|
541
|
+
return `${preNewLineAndIndentation(value, {
|
|
517
542
|
depth,
|
|
518
543
|
indentUsingTab,
|
|
519
|
-
indentSize
|
|
520
|
-
}))
|
|
544
|
+
indentSize,
|
|
545
|
+
})}${postNewLineAndIndentation({ depth, indentUsingTab, indentSize })}`;
|
|
521
546
|
};
|
|
522
547
|
|
|
523
|
-
const inspectConstructor = (value, {
|
|
524
|
-
parenthesis,
|
|
525
|
-
useNew
|
|
526
|
-
}) => {
|
|
548
|
+
const inspectConstructor = (value, { parenthesis, useNew }) => {
|
|
527
549
|
let formattedString = value;
|
|
528
550
|
if (parenthesis) {
|
|
529
|
-
formattedString =
|
|
551
|
+
formattedString = `(${value})`;
|
|
530
552
|
}
|
|
531
553
|
if (useNew) {
|
|
532
|
-
formattedString =
|
|
554
|
+
formattedString = `new ${formattedString}`;
|
|
533
555
|
}
|
|
534
556
|
return formattedString;
|
|
535
557
|
};
|
|
536
558
|
|
|
537
|
-
const inspectArray = (
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
559
|
+
const inspectArray = (
|
|
560
|
+
value,
|
|
561
|
+
{
|
|
562
|
+
seen = [],
|
|
563
|
+
nestedHumanize,
|
|
564
|
+
depth,
|
|
565
|
+
indentUsingTab,
|
|
566
|
+
indentSize,
|
|
567
|
+
parenthesis,
|
|
568
|
+
useNew,
|
|
569
|
+
},
|
|
570
|
+
) => {
|
|
546
571
|
if (seen.indexOf(value) > -1) {
|
|
547
572
|
return "Symbol.for('circular')";
|
|
548
573
|
}
|
|
549
574
|
seen.push(value);
|
|
575
|
+
|
|
550
576
|
let valuesSource = "";
|
|
551
577
|
let i = 0;
|
|
552
578
|
const j = value.length;
|
|
579
|
+
|
|
553
580
|
while (i < j) {
|
|
554
|
-
const valueSource = value.hasOwnProperty(i)
|
|
555
|
-
seen
|
|
556
|
-
|
|
581
|
+
const valueSource = value.hasOwnProperty(i)
|
|
582
|
+
? nestedHumanize(value[i], { seen })
|
|
583
|
+
: "";
|
|
557
584
|
if (i === 0) {
|
|
558
585
|
valuesSource += valueSource;
|
|
559
586
|
} else {
|
|
560
|
-
valuesSource +=
|
|
587
|
+
valuesSource += `,${preNewLineAndIndentation(valueSource, {
|
|
561
588
|
depth,
|
|
562
589
|
indentUsingTab,
|
|
563
|
-
indentSize
|
|
564
|
-
})
|
|
590
|
+
indentSize,
|
|
591
|
+
})}`;
|
|
565
592
|
}
|
|
566
593
|
i++;
|
|
567
594
|
}
|
|
595
|
+
|
|
568
596
|
let arraySource;
|
|
569
597
|
if (valuesSource.length) {
|
|
570
598
|
arraySource = wrapNewLineAndIndentation(valuesSource, {
|
|
571
599
|
depth,
|
|
572
600
|
indentUsingTab,
|
|
573
|
-
indentSize
|
|
601
|
+
indentSize,
|
|
574
602
|
});
|
|
575
603
|
} else {
|
|
576
604
|
arraySource = "";
|
|
577
605
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
});
|
|
606
|
+
|
|
607
|
+
arraySource = `[${arraySource}]`;
|
|
608
|
+
|
|
609
|
+
return inspectConstructor(arraySource, { parenthesis, useNew });
|
|
583
610
|
};
|
|
584
611
|
|
|
585
|
-
const inspectBigIntObject = (value, {
|
|
586
|
-
nestedHumanize
|
|
587
|
-
}) => {
|
|
612
|
+
const inspectBigIntObject = (value, { nestedHumanize }) => {
|
|
588
613
|
const bigIntSource = nestedHumanize(value.valueOf());
|
|
589
|
-
|
|
614
|
+
|
|
615
|
+
return `BigInt(${bigIntSource})`;
|
|
590
616
|
};
|
|
591
617
|
|
|
592
|
-
const inspectBooleanObject = (
|
|
593
|
-
|
|
594
|
-
useNew,
|
|
595
|
-
|
|
596
|
-
}) => {
|
|
618
|
+
const inspectBooleanObject = (
|
|
619
|
+
value,
|
|
620
|
+
{ nestedHumanize, useNew, parenthesis },
|
|
621
|
+
) => {
|
|
597
622
|
const booleanSource = nestedHumanize(value.valueOf());
|
|
598
|
-
return inspectConstructor(
|
|
623
|
+
return inspectConstructor(`Boolean(${booleanSource})`, {
|
|
599
624
|
useNew,
|
|
600
|
-
parenthesis
|
|
625
|
+
parenthesis,
|
|
601
626
|
});
|
|
602
627
|
};
|
|
603
628
|
|
|
604
|
-
const inspectError = (
|
|
605
|
-
|
|
606
|
-
useNew,
|
|
607
|
-
|
|
608
|
-
}) => {
|
|
629
|
+
const inspectError = (
|
|
630
|
+
error,
|
|
631
|
+
{ nestedHumanize, useNew, parenthesis },
|
|
632
|
+
) => {
|
|
609
633
|
const messageSource = nestedHumanize(error.message);
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
634
|
+
|
|
635
|
+
const errorSource = inspectConstructor(
|
|
636
|
+
`${errorToConstructorName(error)}(${messageSource})`,
|
|
637
|
+
{
|
|
638
|
+
useNew,
|
|
639
|
+
parenthesis,
|
|
640
|
+
},
|
|
641
|
+
);
|
|
614
642
|
return errorSource;
|
|
615
643
|
};
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
}) => {
|
|
644
|
+
|
|
645
|
+
const errorToConstructorName = ({ name }) => {
|
|
619
646
|
if (derivedErrorNameArray.includes(name)) {
|
|
620
647
|
return name;
|
|
621
648
|
}
|
|
@@ -623,148 +650,153 @@ const errorToConstructorName = ({
|
|
|
623
650
|
};
|
|
624
651
|
|
|
625
652
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types
|
|
626
|
-
const derivedErrorNameArray = [
|
|
653
|
+
const derivedErrorNameArray = [
|
|
654
|
+
"EvalError",
|
|
655
|
+
"RangeError",
|
|
656
|
+
"ReferenceError",
|
|
657
|
+
"SyntaxError",
|
|
658
|
+
"TypeError",
|
|
659
|
+
"URIError",
|
|
660
|
+
];
|
|
627
661
|
|
|
628
|
-
const inspectDate = (value, {
|
|
629
|
-
nestedHumanize,
|
|
630
|
-
useNew,
|
|
631
|
-
parenthesis
|
|
632
|
-
}) => {
|
|
662
|
+
const inspectDate = (value, { nestedHumanize, useNew, parenthesis }) => {
|
|
633
663
|
const dateSource = nestedHumanize(value.valueOf(), {
|
|
634
|
-
numericSeparator: false
|
|
635
|
-
});
|
|
636
|
-
return inspectConstructor("Date(".concat(dateSource, ")"), {
|
|
637
|
-
useNew,
|
|
638
|
-
parenthesis
|
|
664
|
+
numericSeparator: false,
|
|
639
665
|
});
|
|
666
|
+
return inspectConstructor(`Date(${dateSource})`, { useNew, parenthesis });
|
|
640
667
|
};
|
|
641
668
|
|
|
642
|
-
const inspectFunction = (
|
|
643
|
-
|
|
644
|
-
parenthesis,
|
|
645
|
-
|
|
646
|
-
}) => {
|
|
669
|
+
const inspectFunction = (
|
|
670
|
+
value,
|
|
671
|
+
{ showFunctionBody, parenthesis, depth },
|
|
672
|
+
) => {
|
|
647
673
|
let functionSource;
|
|
648
674
|
if (showFunctionBody) {
|
|
649
675
|
functionSource = value.toString();
|
|
650
676
|
} else {
|
|
651
677
|
const isArrowFunction = value.prototype === undefined;
|
|
652
|
-
const head = isArrowFunction
|
|
653
|
-
|
|
678
|
+
const head = isArrowFunction
|
|
679
|
+
? "() =>"
|
|
680
|
+
: `function ${depth === 0 ? value.name : ""}()`;
|
|
681
|
+
functionSource = `${head} {/* hidden */}`;
|
|
654
682
|
}
|
|
683
|
+
|
|
655
684
|
if (parenthesis) {
|
|
656
|
-
return
|
|
685
|
+
return `(${functionSource})`;
|
|
657
686
|
}
|
|
658
687
|
return functionSource;
|
|
659
688
|
};
|
|
660
689
|
|
|
661
|
-
const inspectNumberObject = (
|
|
662
|
-
|
|
663
|
-
useNew,
|
|
664
|
-
|
|
665
|
-
}) => {
|
|
690
|
+
const inspectNumberObject = (
|
|
691
|
+
value,
|
|
692
|
+
{ nestedHumanize, useNew, parenthesis },
|
|
693
|
+
) => {
|
|
666
694
|
const numberSource = nestedHumanize(value.valueOf());
|
|
667
|
-
return inspectConstructor(
|
|
668
|
-
useNew,
|
|
669
|
-
parenthesis
|
|
670
|
-
});
|
|
695
|
+
return inspectConstructor(`Number(${numberSource})`, { useNew, parenthesis });
|
|
671
696
|
};
|
|
672
697
|
|
|
673
|
-
const inspectObject = (
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
698
|
+
const inspectObject = (
|
|
699
|
+
value,
|
|
700
|
+
{
|
|
701
|
+
nestedHumanize,
|
|
702
|
+
seen = [],
|
|
703
|
+
depth,
|
|
704
|
+
indentUsingTab,
|
|
705
|
+
indentSize,
|
|
706
|
+
objectConstructor,
|
|
707
|
+
parenthesis,
|
|
708
|
+
useNew,
|
|
709
|
+
},
|
|
710
|
+
) => {
|
|
683
711
|
if (seen.indexOf(value) > -1) return "Symbol.for('circular')";
|
|
712
|
+
|
|
684
713
|
seen.push(value);
|
|
714
|
+
|
|
685
715
|
const propertySourceArray = [];
|
|
686
|
-
Object.getOwnPropertyNames(value).forEach(propertyName => {
|
|
716
|
+
Object.getOwnPropertyNames(value).forEach((propertyName) => {
|
|
687
717
|
const propertyNameAsNumber = parseInt(propertyName, 10);
|
|
688
|
-
const propertyNameSource = nestedHumanize(
|
|
718
|
+
const propertyNameSource = nestedHumanize(
|
|
719
|
+
Number.isInteger(propertyNameAsNumber)
|
|
720
|
+
? propertyNameAsNumber
|
|
721
|
+
: propertyName,
|
|
722
|
+
);
|
|
689
723
|
propertySourceArray.push({
|
|
690
724
|
nameOrSymbolSource: propertyNameSource,
|
|
691
|
-
valueSource: nestedHumanize(value[propertyName], {
|
|
692
|
-
seen
|
|
693
|
-
})
|
|
725
|
+
valueSource: nestedHumanize(value[propertyName], { seen }),
|
|
694
726
|
});
|
|
695
727
|
});
|
|
696
|
-
Object.getOwnPropertySymbols(value).forEach(symbol => {
|
|
728
|
+
Object.getOwnPropertySymbols(value).forEach((symbol) => {
|
|
697
729
|
propertySourceArray.push({
|
|
698
|
-
nameOrSymbolSource:
|
|
699
|
-
valueSource: nestedHumanize(value[symbol], {
|
|
700
|
-
seen
|
|
701
|
-
})
|
|
730
|
+
nameOrSymbolSource: `[${nestedHumanize(symbol)}]`,
|
|
731
|
+
valueSource: nestedHumanize(value[symbol], { seen }),
|
|
702
732
|
});
|
|
703
733
|
});
|
|
734
|
+
|
|
704
735
|
let propertiesSource = "";
|
|
705
|
-
propertySourceArray.forEach(({
|
|
706
|
-
nameOrSymbolSource,
|
|
707
|
-
valueSource
|
|
708
|
-
}, index) => {
|
|
736
|
+
propertySourceArray.forEach(({ nameOrSymbolSource, valueSource }, index) => {
|
|
709
737
|
if (index === 0) {
|
|
710
|
-
propertiesSource +=
|
|
738
|
+
propertiesSource += `${nameOrSymbolSource}: ${valueSource}`;
|
|
711
739
|
} else {
|
|
712
|
-
propertiesSource +=
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
740
|
+
propertiesSource += `,${preNewLineAndIndentation(
|
|
741
|
+
`${nameOrSymbolSource}: ${valueSource}`,
|
|
742
|
+
{
|
|
743
|
+
depth,
|
|
744
|
+
indentUsingTab,
|
|
745
|
+
indentSize,
|
|
746
|
+
},
|
|
747
|
+
)}`;
|
|
717
748
|
}
|
|
718
749
|
});
|
|
750
|
+
|
|
719
751
|
let objectSource;
|
|
720
752
|
if (propertiesSource.length) {
|
|
721
|
-
objectSource =
|
|
753
|
+
objectSource = `${wrapNewLineAndIndentation(propertiesSource, {
|
|
722
754
|
depth,
|
|
723
755
|
indentUsingTab,
|
|
724
|
-
indentSize
|
|
725
|
-
})
|
|
756
|
+
indentSize,
|
|
757
|
+
})}`;
|
|
726
758
|
} else {
|
|
727
759
|
objectSource = "";
|
|
728
760
|
}
|
|
761
|
+
|
|
729
762
|
if (objectConstructor) {
|
|
730
|
-
objectSource =
|
|
763
|
+
objectSource = `Object({${objectSource}})`;
|
|
731
764
|
} else {
|
|
732
|
-
objectSource =
|
|
765
|
+
objectSource = `{${objectSource}}`;
|
|
733
766
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
useNew
|
|
737
|
-
});
|
|
767
|
+
|
|
768
|
+
return inspectConstructor(objectSource, { parenthesis, useNew });
|
|
738
769
|
};
|
|
739
770
|
|
|
740
|
-
const inspectRegExp = value => value.toString();
|
|
771
|
+
const inspectRegExp = (value) => value.toString();
|
|
741
772
|
|
|
742
|
-
const inspectStringObject = (
|
|
743
|
-
|
|
744
|
-
useNew,
|
|
745
|
-
|
|
746
|
-
}) => {
|
|
773
|
+
const inspectStringObject = (
|
|
774
|
+
value,
|
|
775
|
+
{ nestedHumanize, useNew, parenthesis },
|
|
776
|
+
) => {
|
|
747
777
|
const stringSource = nestedHumanize(value.valueOf());
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
parenthesis
|
|
751
|
-
});
|
|
778
|
+
|
|
779
|
+
return inspectConstructor(`String(${stringSource})`, { useNew, parenthesis });
|
|
752
780
|
};
|
|
753
781
|
|
|
754
782
|
// tslint:disable:ordered-imports
|
|
755
783
|
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
784
|
+
|
|
785
|
+
const humanize = (
|
|
786
|
+
value,
|
|
787
|
+
{
|
|
788
|
+
parenthesis = false,
|
|
789
|
+
quote = "auto",
|
|
790
|
+
canUseTemplateString = true,
|
|
791
|
+
useNew = false,
|
|
792
|
+
objectConstructor = false,
|
|
793
|
+
showFunctionBody = false,
|
|
794
|
+
indentUsingTab = false,
|
|
795
|
+
indentSize = 2,
|
|
796
|
+
numericSeparator = true,
|
|
797
|
+
preserveLineBreaks = false,
|
|
798
|
+
} = {},
|
|
799
|
+
) => {
|
|
768
800
|
const scopedHumanize = (scopedValue, scopedOptions) => {
|
|
769
801
|
const options = {
|
|
770
802
|
...scopedOptions,
|
|
@@ -772,12 +804,13 @@ const humanize = (value, {
|
|
|
772
804
|
return scopedHumanize(nestedValue, {
|
|
773
805
|
...scopedOptions,
|
|
774
806
|
depth: scopedOptions.depth + 1,
|
|
775
|
-
...nestedOptions
|
|
807
|
+
...nestedOptions,
|
|
776
808
|
});
|
|
777
|
-
}
|
|
809
|
+
},
|
|
778
810
|
};
|
|
779
811
|
return humanizeValue(scopedValue, options);
|
|
780
812
|
};
|
|
813
|
+
|
|
781
814
|
return scopedHumanize(value, {
|
|
782
815
|
parenthesis,
|
|
783
816
|
quote,
|
|
@@ -789,10 +822,12 @@ const humanize = (value, {
|
|
|
789
822
|
indentSize,
|
|
790
823
|
numericSeparator,
|
|
791
824
|
preserveLineBreaks,
|
|
792
|
-
depth: 0
|
|
825
|
+
depth: 0,
|
|
793
826
|
});
|
|
794
827
|
};
|
|
828
|
+
|
|
795
829
|
const humanizeMethodSymbol = Symbol.for("inspect");
|
|
830
|
+
|
|
796
831
|
const humanizeValue = (value, options) => {
|
|
797
832
|
const customHumanize = value && value[humanizeMethodSymbol];
|
|
798
833
|
if (customHumanize) {
|
|
@@ -808,12 +843,16 @@ const humanizeValue = (value, options) => {
|
|
|
808
843
|
if (compositeStringifier) {
|
|
809
844
|
return compositeStringifier(value, options);
|
|
810
845
|
}
|
|
811
|
-
return inspectConstructor(
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
846
|
+
return inspectConstructor(
|
|
847
|
+
`${compositeType}(${inspectObject(value, options)})`,
|
|
848
|
+
{
|
|
849
|
+
...options,
|
|
850
|
+
parenthesis: false,
|
|
851
|
+
},
|
|
852
|
+
);
|
|
815
853
|
};
|
|
816
|
-
|
|
854
|
+
|
|
855
|
+
const primitiveTypeFromValue = (value) => {
|
|
817
856
|
if (value === null) {
|
|
818
857
|
return "null";
|
|
819
858
|
}
|
|
@@ -830,9 +869,9 @@ const primitiveStringifiers = {
|
|
|
830
869
|
string: inspectString,
|
|
831
870
|
symbol: inspectSymbol,
|
|
832
871
|
undefined: inspectUndefined,
|
|
833
|
-
bigint: inspectBigInt
|
|
872
|
+
bigint: inspectBigInt,
|
|
834
873
|
};
|
|
835
|
-
const compositeTypeFromObject = object => {
|
|
874
|
+
const compositeTypeFromObject = (object) => {
|
|
836
875
|
if (typeof object === "object" && Object.getPrototypeOf(object) === null) {
|
|
837
876
|
return "Object";
|
|
838
877
|
}
|
|
@@ -848,9 +887,8 @@ const compositeTypeFromObject = object => {
|
|
|
848
887
|
}
|
|
849
888
|
return tagName;
|
|
850
889
|
};
|
|
851
|
-
const {
|
|
852
|
-
|
|
853
|
-
} = Object.prototype;
|
|
890
|
+
const { toString } = Object.prototype;
|
|
891
|
+
|
|
854
892
|
const compositeStringifiers = {
|
|
855
893
|
Array: inspectArray,
|
|
856
894
|
BigInt: inspectBigIntObject,
|
|
@@ -861,40 +899,45 @@ const compositeStringifiers = {
|
|
|
861
899
|
Number: inspectNumberObject,
|
|
862
900
|
Object: inspectObject,
|
|
863
901
|
RegExp: inspectRegExp,
|
|
864
|
-
String: inspectStringObject
|
|
902
|
+
String: inspectStringObject,
|
|
865
903
|
};
|
|
866
904
|
|
|
867
|
-
const getPrecision = number => {
|
|
905
|
+
const getPrecision = (number) => {
|
|
868
906
|
if (Math.floor(number) === number) return 0;
|
|
869
907
|
const [, decimals] = number.toString().split(".");
|
|
870
908
|
return decimals.length || 0;
|
|
871
909
|
};
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
910
|
+
|
|
911
|
+
const setRoundedPrecision = (
|
|
912
|
+
number,
|
|
913
|
+
{ decimals = 1, decimalsWhenSmall = decimals } = {},
|
|
914
|
+
) => {
|
|
876
915
|
return setDecimalsPrecision(number, {
|
|
877
916
|
decimals,
|
|
878
917
|
decimalsWhenSmall,
|
|
879
|
-
transform: Math.round
|
|
918
|
+
transform: Math.round,
|
|
880
919
|
});
|
|
881
920
|
};
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
921
|
+
|
|
922
|
+
const setPrecision = (
|
|
923
|
+
number,
|
|
924
|
+
{ decimals = 1, decimalsWhenSmall = decimals } = {},
|
|
925
|
+
) => {
|
|
886
926
|
return setDecimalsPrecision(number, {
|
|
887
927
|
decimals,
|
|
888
928
|
decimalsWhenSmall,
|
|
889
|
-
transform: parseInt
|
|
929
|
+
transform: parseInt,
|
|
890
930
|
});
|
|
891
931
|
};
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
932
|
+
|
|
933
|
+
const setDecimalsPrecision = (
|
|
934
|
+
number,
|
|
935
|
+
{
|
|
936
|
+
transform,
|
|
937
|
+
decimals, // max decimals for number in [-Infinity, -1[]1, Infinity]
|
|
938
|
+
decimalsWhenSmall, // max decimals for number in [-1,1]
|
|
939
|
+
} = {},
|
|
940
|
+
) => {
|
|
898
941
|
if (number === 0) {
|
|
899
942
|
return 0;
|
|
900
943
|
}
|
|
@@ -933,35 +976,34 @@ const setDecimalsPrecision = (number, {
|
|
|
933
976
|
// return numberTruncated
|
|
934
977
|
// }
|
|
935
978
|
|
|
936
|
-
const humanizeEllapsedTime = (ms, {
|
|
937
|
-
short
|
|
938
|
-
} = {}) => {
|
|
979
|
+
const humanizeEllapsedTime = (ms, { short } = {}) => {
|
|
939
980
|
if (ms < 1000) {
|
|
940
981
|
return short ? "0s" : "0 second";
|
|
941
982
|
}
|
|
942
|
-
const {
|
|
943
|
-
primary,
|
|
944
|
-
remaining
|
|
945
|
-
} = parseMs(ms);
|
|
983
|
+
const { primary, remaining } = parseMs(ms);
|
|
946
984
|
if (!remaining) {
|
|
947
985
|
return inspectEllapsedUnit(primary, short);
|
|
948
986
|
}
|
|
949
|
-
return
|
|
987
|
+
return `${inspectEllapsedUnit(primary, short)} and ${inspectEllapsedUnit(
|
|
988
|
+
remaining,
|
|
989
|
+
short,
|
|
990
|
+
)}`;
|
|
950
991
|
};
|
|
951
992
|
const inspectEllapsedUnit = (unit, short) => {
|
|
952
|
-
const count =
|
|
993
|
+
const count =
|
|
994
|
+
unit.name === "second" ? Math.floor(unit.count) : Math.round(unit.count);
|
|
953
995
|
let name = unit.name;
|
|
954
996
|
if (short) {
|
|
955
997
|
name = unitShort[name];
|
|
956
998
|
if (count <= 1) {
|
|
957
|
-
return
|
|
999
|
+
return `${count}${name}`;
|
|
958
1000
|
}
|
|
959
|
-
return
|
|
1001
|
+
return `${count}${name}s`;
|
|
960
1002
|
}
|
|
961
1003
|
if (count <= 1) {
|
|
962
|
-
return
|
|
1004
|
+
return `${count} ${name}`;
|
|
963
1005
|
}
|
|
964
|
-
return
|
|
1006
|
+
return `${count} ${name}s`;
|
|
965
1007
|
};
|
|
966
1008
|
const unitShort = {
|
|
967
1009
|
year: "y",
|
|
@@ -970,13 +1012,13 @@ const unitShort = {
|
|
|
970
1012
|
day: "d",
|
|
971
1013
|
hour: "h",
|
|
972
1014
|
minute: "m",
|
|
973
|
-
second: "s"
|
|
1015
|
+
second: "s",
|
|
974
1016
|
};
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
decimals
|
|
979
|
-
|
|
1017
|
+
|
|
1018
|
+
const humanizeDuration = (
|
|
1019
|
+
ms,
|
|
1020
|
+
{ short, rounded = true, decimals } = {},
|
|
1021
|
+
) => {
|
|
980
1022
|
// ignore ms below meaningfulMs so that:
|
|
981
1023
|
// humanizeDuration(0.5) -> "0 second"
|
|
982
1024
|
// humanizeDuration(1.1) -> "0.001 second" (and not "0.0011 second")
|
|
@@ -987,62 +1029,55 @@ const humanizeDuration = (ms, {
|
|
|
987
1029
|
if (ms < 1) {
|
|
988
1030
|
return short ? "0s" : "0 second";
|
|
989
1031
|
}
|
|
990
|
-
const {
|
|
991
|
-
primary,
|
|
992
|
-
remaining
|
|
993
|
-
} = parseMs(ms);
|
|
1032
|
+
const { primary, remaining } = parseMs(ms);
|
|
994
1033
|
if (!remaining) {
|
|
995
1034
|
return humanizeDurationUnit(primary, {
|
|
996
|
-
decimals:
|
|
1035
|
+
decimals:
|
|
1036
|
+
decimals === undefined ? (primary.name === "second" ? 1 : 0) : decimals,
|
|
997
1037
|
short,
|
|
998
|
-
rounded
|
|
1038
|
+
rounded,
|
|
999
1039
|
});
|
|
1000
1040
|
}
|
|
1001
|
-
return
|
|
1041
|
+
return `${humanizeDurationUnit(primary, {
|
|
1002
1042
|
decimals: decimals === undefined ? 0 : decimals,
|
|
1003
1043
|
short,
|
|
1004
|
-
rounded
|
|
1005
|
-
})
|
|
1044
|
+
rounded,
|
|
1045
|
+
})} and ${humanizeDurationUnit(remaining, {
|
|
1006
1046
|
decimals: decimals === undefined ? 0 : decimals,
|
|
1007
1047
|
short,
|
|
1008
|
-
rounded
|
|
1009
|
-
})
|
|
1048
|
+
rounded,
|
|
1049
|
+
})}`;
|
|
1010
1050
|
};
|
|
1011
|
-
const humanizeDurationUnit = (unit, {
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
}) => {
|
|
1016
|
-
const count = rounded ? setRoundedPrecision(unit.count, {
|
|
1017
|
-
decimals
|
|
1018
|
-
}) : setPrecision(unit.count, {
|
|
1019
|
-
decimals
|
|
1020
|
-
});
|
|
1051
|
+
const humanizeDurationUnit = (unit, { decimals, short, rounded }) => {
|
|
1052
|
+
const count = rounded
|
|
1053
|
+
? setRoundedPrecision(unit.count, { decimals })
|
|
1054
|
+
: setPrecision(unit.count, { decimals });
|
|
1021
1055
|
let name = unit.name;
|
|
1022
1056
|
if (short) {
|
|
1023
1057
|
name = unitShort[name];
|
|
1024
|
-
return
|
|
1058
|
+
return `${count}${name}`;
|
|
1025
1059
|
}
|
|
1026
1060
|
if (count <= 1) {
|
|
1027
|
-
return
|
|
1061
|
+
return `${count} ${name}`;
|
|
1028
1062
|
}
|
|
1029
|
-
return
|
|
1063
|
+
return `${count} ${name}s`;
|
|
1030
1064
|
};
|
|
1031
1065
|
const MS_PER_UNITS = {
|
|
1032
|
-
year:
|
|
1033
|
-
month:
|
|
1034
|
-
week:
|
|
1035
|
-
day:
|
|
1036
|
-
hour:
|
|
1037
|
-
minute:
|
|
1038
|
-
second: 1000
|
|
1066
|
+
year: 31_557_600_000,
|
|
1067
|
+
month: 2_629_000_000,
|
|
1068
|
+
week: 604_800_000,
|
|
1069
|
+
day: 86_400_000,
|
|
1070
|
+
hour: 3_600_000,
|
|
1071
|
+
minute: 60_000,
|
|
1072
|
+
second: 1000,
|
|
1039
1073
|
};
|
|
1040
|
-
|
|
1074
|
+
|
|
1075
|
+
const parseMs = (ms) => {
|
|
1041
1076
|
const unitNames = Object.keys(MS_PER_UNITS);
|
|
1042
1077
|
const smallestUnitName = unitNames[unitNames.length - 1];
|
|
1043
1078
|
let firstUnitName = smallestUnitName;
|
|
1044
1079
|
let firstUnitCount = ms / MS_PER_UNITS[smallestUnitName];
|
|
1045
|
-
const firstUnitIndex = unitNames.findIndex(unitName => {
|
|
1080
|
+
const firstUnitIndex = unitNames.findIndex((unitName) => {
|
|
1046
1081
|
if (unitName === smallestUnitName) {
|
|
1047
1082
|
return false;
|
|
1048
1083
|
}
|
|
@@ -1059,8 +1094,8 @@ const parseMs = ms => {
|
|
|
1059
1094
|
return {
|
|
1060
1095
|
primary: {
|
|
1061
1096
|
name: firstUnitName,
|
|
1062
|
-
count: firstUnitCount
|
|
1063
|
-
}
|
|
1097
|
+
count: firstUnitCount,
|
|
1098
|
+
},
|
|
1064
1099
|
};
|
|
1065
1100
|
}
|
|
1066
1101
|
const remainingMs = ms - firstUnitCount * MS_PER_UNITS[firstUnitName];
|
|
@@ -1074,51 +1109,42 @@ const parseMs = ms => {
|
|
|
1074
1109
|
return {
|
|
1075
1110
|
primary: {
|
|
1076
1111
|
name: firstUnitName,
|
|
1077
|
-
count: firstUnitCount
|
|
1078
|
-
}
|
|
1112
|
+
count: firstUnitCount,
|
|
1113
|
+
},
|
|
1079
1114
|
};
|
|
1080
1115
|
}
|
|
1081
1116
|
// - 1 year and 1 month is great
|
|
1082
1117
|
return {
|
|
1083
1118
|
primary: {
|
|
1084
1119
|
name: firstUnitName,
|
|
1085
|
-
count: firstUnitCount
|
|
1120
|
+
count: firstUnitCount,
|
|
1086
1121
|
},
|
|
1087
1122
|
remaining: {
|
|
1088
1123
|
name: remainingUnitName,
|
|
1089
|
-
count: remainingUnitCount
|
|
1090
|
-
}
|
|
1124
|
+
count: remainingUnitCount,
|
|
1125
|
+
},
|
|
1091
1126
|
};
|
|
1092
1127
|
};
|
|
1093
1128
|
|
|
1094
|
-
const humanizeFileSize = (numberOfBytes, {
|
|
1095
|
-
decimals,
|
|
1096
|
-
short
|
|
1097
|
-
} = {}) => {
|
|
1098
|
-
return inspectBytes(numberOfBytes, {
|
|
1099
|
-
decimals,
|
|
1100
|
-
short
|
|
1101
|
-
});
|
|
1129
|
+
const humanizeFileSize = (numberOfBytes, { decimals, short } = {}) => {
|
|
1130
|
+
return inspectBytes(numberOfBytes, { decimals, short });
|
|
1102
1131
|
};
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
short
|
|
1106
|
-
} = {}) => {
|
|
1107
|
-
return inspectBytes(metricValue, {
|
|
1108
|
-
decimals,
|
|
1109
|
-
fixedDecimals: true,
|
|
1110
|
-
short
|
|
1111
|
-
});
|
|
1132
|
+
|
|
1133
|
+
const humanizeMemory = (metricValue, { decimals, short } = {}) => {
|
|
1134
|
+
return inspectBytes(metricValue, { decimals, fixedDecimals: true, short });
|
|
1112
1135
|
};
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
short
|
|
1117
|
-
|
|
1136
|
+
|
|
1137
|
+
const inspectBytes = (
|
|
1138
|
+
number,
|
|
1139
|
+
{ fixedDecimals = false, decimals, short } = {},
|
|
1140
|
+
) => {
|
|
1118
1141
|
if (number === 0) {
|
|
1119
|
-
return
|
|
1142
|
+
return `0 B`;
|
|
1120
1143
|
}
|
|
1121
|
-
const exponent = Math.min(
|
|
1144
|
+
const exponent = Math.min(
|
|
1145
|
+
Math.floor(Math.log10(number) / 3),
|
|
1146
|
+
BYTE_UNITS.length - 1,
|
|
1147
|
+
);
|
|
1122
1148
|
const unitNumber = number / Math.pow(1000, exponent);
|
|
1123
1149
|
const unitName = BYTE_UNITS[exponent];
|
|
1124
1150
|
if (decimals === undefined) {
|
|
@@ -1130,41 +1156,43 @@ const inspectBytes = (number, {
|
|
|
1130
1156
|
}
|
|
1131
1157
|
const unitNumberRounded = setRoundedPrecision(unitNumber, {
|
|
1132
1158
|
decimals,
|
|
1133
|
-
decimalsWhenSmall: 1
|
|
1159
|
+
decimalsWhenSmall: 1,
|
|
1134
1160
|
});
|
|
1135
|
-
const value = fixedDecimals
|
|
1161
|
+
const value = fixedDecimals
|
|
1162
|
+
? unitNumberRounded.toFixed(decimals)
|
|
1163
|
+
: unitNumberRounded;
|
|
1136
1164
|
if (short) {
|
|
1137
|
-
return
|
|
1165
|
+
return `${value}${unitName}`;
|
|
1138
1166
|
}
|
|
1139
|
-
return
|
|
1167
|
+
return `${value} ${unitName}`;
|
|
1140
1168
|
};
|
|
1169
|
+
|
|
1141
1170
|
const BYTE_UNITS = ["B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
1142
1171
|
|
|
1143
|
-
const distributePercentages = (
|
|
1144
|
-
|
|
1145
|
-
|
|
1172
|
+
const distributePercentages = (
|
|
1173
|
+
namedNumbers,
|
|
1174
|
+
{ maxPrecisionHint = 2 } = {},
|
|
1175
|
+
) => {
|
|
1146
1176
|
const numberNames = Object.keys(namedNumbers);
|
|
1147
1177
|
if (numberNames.length === 0) {
|
|
1148
1178
|
return {};
|
|
1149
1179
|
}
|
|
1150
1180
|
if (numberNames.length === 1) {
|
|
1151
1181
|
const firstNumberName = numberNames[0];
|
|
1152
|
-
return {
|
|
1153
|
-
[firstNumberName]: "100 %"
|
|
1154
|
-
};
|
|
1182
|
+
return { [firstNumberName]: "100 %" };
|
|
1155
1183
|
}
|
|
1156
|
-
const numbers = numberNames.map(name => namedNumbers[name]);
|
|
1184
|
+
const numbers = numberNames.map((name) => namedNumbers[name]);
|
|
1157
1185
|
const total = numbers.reduce((sum, value) => sum + value, 0);
|
|
1158
|
-
const ratios = numbers.map(number => number / total);
|
|
1186
|
+
const ratios = numbers.map((number) => number / total);
|
|
1159
1187
|
const percentages = {};
|
|
1160
1188
|
ratios.pop();
|
|
1161
1189
|
ratios.forEach((ratio, index) => {
|
|
1162
1190
|
const percentage = ratio * 100;
|
|
1163
1191
|
percentages[numberNames[index]] = percentage;
|
|
1164
1192
|
});
|
|
1165
|
-
const lowestPercentage = 1 / Math.pow(10, maxPrecisionHint) * 100;
|
|
1193
|
+
const lowestPercentage = (1 / Math.pow(10, maxPrecisionHint)) * 100;
|
|
1166
1194
|
let precision = 0;
|
|
1167
|
-
Object.keys(percentages).forEach(name => {
|
|
1195
|
+
Object.keys(percentages).forEach((name) => {
|
|
1168
1196
|
const percentage = percentages[name];
|
|
1169
1197
|
if (percentage < lowestPercentage) {
|
|
1170
1198
|
// check the amout of meaningful decimals
|
|
@@ -1177,33 +1205,36 @@ const distributePercentages = (namedNumbers, {
|
|
|
1177
1205
|
}
|
|
1178
1206
|
});
|
|
1179
1207
|
let remainingPercentage = 100;
|
|
1180
|
-
|
|
1208
|
+
|
|
1209
|
+
Object.keys(percentages).forEach((name) => {
|
|
1181
1210
|
const percentage = percentages[name];
|
|
1182
1211
|
const percentageAllocated = setRoundedPrecision(percentage, {
|
|
1183
|
-
decimals: precision
|
|
1212
|
+
decimals: precision,
|
|
1184
1213
|
});
|
|
1185
1214
|
remainingPercentage -= percentageAllocated;
|
|
1186
1215
|
percentages[name] = percentageAllocated;
|
|
1187
1216
|
});
|
|
1188
1217
|
const lastName = numberNames[numberNames.length - 1];
|
|
1189
1218
|
percentages[lastName] = setRoundedPrecision(remainingPercentage, {
|
|
1190
|
-
decimals: precision
|
|
1219
|
+
decimals: precision,
|
|
1191
1220
|
});
|
|
1192
1221
|
return percentages;
|
|
1193
1222
|
};
|
|
1194
1223
|
|
|
1195
|
-
const formatDefault = v => v;
|
|
1224
|
+
const formatDefault = (v) => v;
|
|
1225
|
+
|
|
1196
1226
|
const generateContentFrame = ({
|
|
1197
1227
|
content,
|
|
1198
1228
|
line,
|
|
1199
1229
|
column,
|
|
1230
|
+
|
|
1200
1231
|
linesAbove = 3,
|
|
1201
1232
|
linesBelow = 0,
|
|
1202
1233
|
lineMaxWidth = 120,
|
|
1203
1234
|
lineNumbersOnTheLeft = true,
|
|
1204
1235
|
lineMarker = true,
|
|
1205
1236
|
columnMarker = true,
|
|
1206
|
-
format = formatDefault
|
|
1237
|
+
format = formatDefault,
|
|
1207
1238
|
} = {}) => {
|
|
1208
1239
|
const lineStrings = content.split(/\r?\n/);
|
|
1209
1240
|
if (line === 0) line = 1;
|
|
@@ -1212,6 +1243,7 @@ const generateContentFrame = ({
|
|
|
1212
1243
|
column = 1;
|
|
1213
1244
|
}
|
|
1214
1245
|
if (column === 0) column = 1;
|
|
1246
|
+
|
|
1215
1247
|
let lineStartIndex = line - 1 - linesAbove;
|
|
1216
1248
|
if (lineStartIndex < 0) {
|
|
1217
1249
|
lineStartIndex = 0;
|
|
@@ -1228,6 +1260,7 @@ const generateContentFrame = ({
|
|
|
1228
1260
|
lineMarker = false; // useless because last line
|
|
1229
1261
|
}
|
|
1230
1262
|
let lineIndex = lineStartIndex;
|
|
1263
|
+
|
|
1231
1264
|
let columnsBefore;
|
|
1232
1265
|
let columnsAfter;
|
|
1233
1266
|
if (column > lineMaxWidth) {
|
|
@@ -1238,6 +1271,7 @@ const generateContentFrame = ({
|
|
|
1238
1271
|
columnsAfter = lineMaxWidth;
|
|
1239
1272
|
}
|
|
1240
1273
|
let columnMarkerIndex = column - 1 - columnsBefore;
|
|
1274
|
+
|
|
1241
1275
|
let source = "";
|
|
1242
1276
|
while (lineIndex <= lineEndIndex) {
|
|
1243
1277
|
const lineString = lineStrings[lineIndex];
|
|
@@ -1245,18 +1279,19 @@ const generateContentFrame = ({
|
|
|
1245
1279
|
const isLastLine = lineIndex === lineEndIndex;
|
|
1246
1280
|
const isMainLine = lineNumber === line;
|
|
1247
1281
|
lineIndex++;
|
|
1282
|
+
|
|
1248
1283
|
{
|
|
1249
1284
|
if (lineMarker) {
|
|
1250
1285
|
if (isMainLine) {
|
|
1251
|
-
source +=
|
|
1286
|
+
source += `${format(">", "marker_line")} `;
|
|
1252
1287
|
} else {
|
|
1253
1288
|
source += " ";
|
|
1254
1289
|
}
|
|
1255
1290
|
}
|
|
1256
1291
|
if (lineNumbersOnTheLeft) {
|
|
1257
1292
|
// fill with spaces to ensure if line moves from 7,8,9 to 10 the display is still great
|
|
1258
|
-
const asideSource =
|
|
1259
|
-
source +=
|
|
1293
|
+
const asideSource = `${fillLeft(lineNumber, lineEndIndex + 1)} |`;
|
|
1294
|
+
source += `${format(asideSource, "line_number_aside")} `;
|
|
1260
1295
|
}
|
|
1261
1296
|
}
|
|
1262
1297
|
{
|
|
@@ -1265,17 +1300,18 @@ const generateContentFrame = ({
|
|
|
1265
1300
|
end: columnsAfter,
|
|
1266
1301
|
prefix: "…",
|
|
1267
1302
|
suffix: "…",
|
|
1268
|
-
format
|
|
1303
|
+
format,
|
|
1269
1304
|
});
|
|
1270
1305
|
}
|
|
1271
1306
|
{
|
|
1272
1307
|
if (columnMarker && isMainLine) {
|
|
1273
|
-
source +=
|
|
1308
|
+
source += `\n`;
|
|
1274
1309
|
if (lineMarker) {
|
|
1275
1310
|
source += " ";
|
|
1276
1311
|
}
|
|
1277
1312
|
if (lineNumbersOnTheLeft) {
|
|
1278
|
-
const asideSpaces =
|
|
1313
|
+
const asideSpaces = `${fillLeft(lineNumber, lineEndIndex + 1)} | `
|
|
1314
|
+
.length;
|
|
1279
1315
|
source += " ".repeat(asideSpaces);
|
|
1280
1316
|
}
|
|
1281
1317
|
source += " ".repeat(columnMarkerIndex);
|
|
@@ -1288,24 +1324,23 @@ const generateContentFrame = ({
|
|
|
1288
1324
|
}
|
|
1289
1325
|
return source;
|
|
1290
1326
|
};
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
end,
|
|
1294
|
-
prefix,
|
|
1295
|
-
suffix,
|
|
1296
|
-
format
|
|
1297
|
-
}) => {
|
|
1327
|
+
|
|
1328
|
+
const truncateLine = (line, { start, end, prefix, suffix, format }) => {
|
|
1298
1329
|
const lastIndex = line.length;
|
|
1330
|
+
|
|
1299
1331
|
if (line.length === 0) {
|
|
1300
1332
|
// don't show any ellipsis if the line is empty
|
|
1301
1333
|
// because it's not truncated in that case
|
|
1302
1334
|
return "";
|
|
1303
1335
|
}
|
|
1336
|
+
|
|
1304
1337
|
const startTruncated = start > 0;
|
|
1305
1338
|
const endTruncated = lastIndex > end;
|
|
1339
|
+
|
|
1306
1340
|
let from = startTruncated ? start + prefix.length : start;
|
|
1307
1341
|
let to = endTruncated ? end - suffix.length : end;
|
|
1308
1342
|
if (to > lastIndex) to = lastIndex;
|
|
1343
|
+
|
|
1309
1344
|
if (start >= lastIndex || from === to) {
|
|
1310
1345
|
return "";
|
|
1311
1346
|
}
|
|
@@ -1318,16 +1353,20 @@ const truncateLine = (line, {
|
|
|
1318
1353
|
return "";
|
|
1319
1354
|
}
|
|
1320
1355
|
if (startTruncated && endTruncated) {
|
|
1321
|
-
return
|
|
1356
|
+
return `${format(prefix, "marker_overflow_left")}${result}${format(
|
|
1357
|
+
suffix,
|
|
1358
|
+
"marker_overflow_right",
|
|
1359
|
+
)}`;
|
|
1322
1360
|
}
|
|
1323
1361
|
if (startTruncated) {
|
|
1324
|
-
return
|
|
1362
|
+
return `${format(prefix, "marker_overflow_left")}${result}`;
|
|
1325
1363
|
}
|
|
1326
1364
|
if (endTruncated) {
|
|
1327
|
-
return
|
|
1365
|
+
return `${result}${format(suffix, "marker_overflow_right")}`;
|
|
1328
1366
|
}
|
|
1329
1367
|
return result;
|
|
1330
1368
|
};
|
|
1369
|
+
|
|
1331
1370
|
const fillLeft = (value, biggestValue, char = " ") => {
|
|
1332
1371
|
const width = String(value).length;
|
|
1333
1372
|
const biggestWidth = String(biggestValue).length;
|
|
@@ -1344,8 +1383,9 @@ const createCallOrderer = () => {
|
|
|
1344
1383
|
const queue = [];
|
|
1345
1384
|
const callWhenPreviousExecutionAreDone = (executionIndex, callback) => {
|
|
1346
1385
|
if (queue[executionIndex]) {
|
|
1347
|
-
throw new Error(
|
|
1386
|
+
throw new Error(`${executionIndex} already used`);
|
|
1348
1387
|
}
|
|
1388
|
+
|
|
1349
1389
|
let allBeforeAreDone = true;
|
|
1350
1390
|
if (executionIndex > 0) {
|
|
1351
1391
|
let beforeIndex = executionIndex - 1;
|
|
@@ -1379,26 +1419,43 @@ const createCallOrderer = () => {
|
|
|
1379
1419
|
return callWhenPreviousExecutionAreDone;
|
|
1380
1420
|
};
|
|
1381
1421
|
|
|
1382
|
-
const errorToMarkdown = error => {
|
|
1383
|
-
const errorIsAPrimitive =
|
|
1422
|
+
const errorToMarkdown = (error) => {
|
|
1423
|
+
const errorIsAPrimitive =
|
|
1424
|
+
error === null ||
|
|
1425
|
+
(typeof error !== "object" && typeof error !== "function");
|
|
1426
|
+
|
|
1384
1427
|
if (errorIsAPrimitive) {
|
|
1385
|
-
return
|
|
1428
|
+
return `\`\`\`js
|
|
1429
|
+
${error}
|
|
1430
|
+
\`\`\``;
|
|
1386
1431
|
}
|
|
1387
|
-
return
|
|
1432
|
+
return `\`\`\`
|
|
1433
|
+
${error.stack}
|
|
1434
|
+
\`\`\``;
|
|
1388
1435
|
};
|
|
1389
1436
|
|
|
1390
|
-
const errorToHTML = error => {
|
|
1391
|
-
const errorIsAPrimitive =
|
|
1437
|
+
const errorToHTML = (error) => {
|
|
1438
|
+
const errorIsAPrimitive =
|
|
1439
|
+
error === null ||
|
|
1440
|
+
(typeof error !== "object" && typeof error !== "function");
|
|
1441
|
+
|
|
1392
1442
|
if (errorIsAPrimitive) {
|
|
1393
1443
|
if (typeof error === "string") {
|
|
1394
|
-
return
|
|
1444
|
+
return `<pre>${escapeHtml(error)}</pre>`;
|
|
1395
1445
|
}
|
|
1396
|
-
return
|
|
1446
|
+
return `<pre>${JSON.stringify(error, null, " ")}</pre>`;
|
|
1397
1447
|
}
|
|
1398
|
-
return
|
|
1448
|
+
return `<pre>${escapeHtml(error.stack)}</pre>`;
|
|
1399
1449
|
};
|
|
1400
|
-
|
|
1401
|
-
|
|
1450
|
+
|
|
1451
|
+
const escapeHtml = (string) => {
|
|
1452
|
+
return string
|
|
1453
|
+
.replace(/&/g, "&")
|
|
1454
|
+
.replace(/</g, "<")
|
|
1455
|
+
.replace(/>/g, ">")
|
|
1456
|
+
.replace(/"/g, """)
|
|
1457
|
+
.replace(/'/g, "'");
|
|
1402
1458
|
};
|
|
1403
1459
|
|
|
1404
|
-
export { ANSI, UNICODE, createCallOrderer, createDetailedMessage, distributePercentages, errorToHTML, errorToMarkdown, generateContentFrame, humanize, humanizeDuration, humanizeEllapsedTime, humanizeFileSize, humanizeMemory, humanizeMethodSymbol };
|
|
1460
|
+
export { ANSI, UNICODE, createCallOrderer, createDetailedMessage, distributePercentages, errorToHTML, errorToMarkdown, generateContentFrame, humanize, humanizeDuration, humanizeEllapsedTime, humanizeFileSize, humanizeMemory, humanizeMethodSymbol, preNewLineAndIndentation, prefixFirstAndIndentRemainingLines, wrapNewLineAndIndentation };
|
|
1461
|
+
//# sourceMappingURL=jsenv_humanize_browser.js.map
|