UNPKG

2.98 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.wordTrim = exports.indentRow = exports.wordWrap = exports.tableView = exports.ratingStars = exports.repeatString = exports.formatDateTime = exports.formatTime = exports.formatDate = exports.icons = void 0;
4const fixedLocale = 'en-us';
5const format = {
6 date: { month: 'long', day: 'numeric', year: 'numeric' },
7 time: { hour: 'numeric', minute: 'numeric', second: 'numeric' },
8};
9const columns = process.stdout.columns ? process.stdout.columns : 80;
10// xxx: Windows cmd + powershell standard fonts currently don't support the full
11// unicode charset. For now we use fallback icons when on windows.
12const useFallbackIcons = process.platform === 'win32';
13exports.icons = useFallbackIcons
14 ? { download: '\u{2193}', star: '\u{2665}', emptyStar: '\u{2022}' }
15 : { download: '\u{2913}', star: '\u{2605}', emptyStar: '\u{2606}' };
16function formatDate(date) {
17 return date.toLocaleString(fixedLocale, format.date);
18}
19exports.formatDate = formatDate;
20function formatTime(date) {
21 return date.toLocaleString(fixedLocale, format.time);
22}
23exports.formatTime = formatTime;
24function formatDateTime(date) {
25 return date.toLocaleString(fixedLocale, { ...format.date, ...format.time });
26}
27exports.formatDateTime = formatDateTime;
28function repeatString(text, count) {
29 let result = '';
30 for (let i = 0; i < count; i++) {
31 result += text;
32 }
33 return result;
34}
35exports.repeatString = repeatString;
36function 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}
40exports.ratingStars = ratingStars;
41function 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}
46exports.tableView = tableView;
47function 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}
61exports.wordWrap = wordWrap;
62function indentRow(row) {
63 return ` ${row}`;
64}
65exports.indentRow = indentRow;
66function wordTrim(text, width = columns, indicator = '...') {
67 if (text.length > width) {
68 return text.substr(0, width - indicator.length) + indicator;
69 }
70 return text;
71}
72exports.wordTrim = wordTrim;
73//# sourceMappingURL=viewutils.js.map
\No newline at end of file