1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.wordTrim = exports.indentRow = exports.wordWrap = exports.tableView = exports.ratingStars = exports.repeatString = exports.formatDateTime = exports.formatTime = exports.formatDate = exports.icons = void 0;
|
4 | const fixedLocale = 'en-us';
|
5 | const format = {
|
6 | date: { month: 'long', day: 'numeric', year: 'numeric' },
|
7 | time: { hour: 'numeric', minute: 'numeric', second: 'numeric' },
|
8 | };
|
9 | const columns = process.stdout.columns ? process.stdout.columns : 80;
|
10 |
|
11 |
|
12 | const useFallbackIcons = process.platform === 'win32';
|
13 | exports.icons = useFallbackIcons
|
14 | ? { download: '\u{2193}', star: '\u{2665}', emptyStar: '\u{2022}' }
|
15 | : { download: '\u{2913}', star: '\u{2605}', emptyStar: '\u{2606}' };
|
16 | function formatDate(date) {
|
17 | return date.toLocaleString(fixedLocale, format.date);
|
18 | }
|
19 | exports.formatDate = formatDate;
|
20 | function formatTime(date) {
|
21 | return date.toLocaleString(fixedLocale, format.time);
|
22 | }
|
23 | exports.formatTime = formatTime;
|
24 | function formatDateTime(date) {
|
25 | return date.toLocaleString(fixedLocale, { ...format.date, ...format.time });
|
26 | }
|
27 | exports.formatDateTime = formatDateTime;
|
28 | function repeatString(text, count) {
|
29 | let result = '';
|
30 | for (let i = 0; i < count; i++) {
|
31 | result += text;
|
32 | }
|
33 | return result;
|
34 | }
|
35 | exports.repeatString = repeatString;
|
36 | function ratingStars(rating, total = 5) {
|
37 | const c = Math.min(Math.round(rating), total);
|
38 | return `${repeatString(exports.icons.star + ' ', c)}${repeatString(exports.icons.emptyStar + ' ', total - c)}`;
|
39 | }
|
40 | exports.ratingStars = ratingStars;
|
41 | function tableView(table, spacing = 2) {
|
42 | const maxLen = {};
|
43 | table.forEach(row => row.forEach((cell, i) => (maxLen[i] = Math.max(maxLen[i] || 0, cell.length))));
|
44 | return table.map(row => row.map((cell, i) => `${cell}${repeatString(' ', maxLen[i] - cell.length + spacing)}`).join(''));
|
45 | }
|
46 | exports.tableView = tableView;
|
47 | function wordWrap(text, width = columns) {
|
48 | const [indent = ''] = text.match(/^\s+/) || [];
|
49 | const maxWidth = width - indent.length;
|
50 | return text
|
51 | .replace(/^\s+/, '')
|
52 | .split('')
|
53 | .reduce(([out, buffer, pos], ch) => {
|
54 | const nl = pos === maxWidth ? `\n${indent}` : '';
|
55 | const newPos = nl ? 0 : +pos + 1;
|
56 | return / |-|,|\./.test(ch) ? [`${out}${buffer}${ch}${nl}`, '', newPos] : [`${out}${nl}`, buffer + ch, newPos];
|
57 | }, [indent, '', 0])
|
58 | .slice(0, 2)
|
59 | .join('');
|
60 | }
|
61 | exports.wordWrap = wordWrap;
|
62 | function indentRow(row) {
|
63 | return ` ${row}`;
|
64 | }
|
65 | exports.indentRow = indentRow;
|
66 | function wordTrim(text, width = columns, indicator = '...') {
|
67 | if (text.length > width) {
|
68 | return text.substr(0, width - indicator.length) + indicator;
|
69 | }
|
70 | return text;
|
71 | }
|
72 | exports.wordTrim = wordTrim;
|
73 |
|
\ | No newline at end of file |