-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
utils.ts
120 lines (98 loc) 路 2.9 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import dayjs from 'dayjs';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import relativeTime from 'dayjs/plugin/relativeTime';
import i18next from 'i18next';
import isString from 'lodash/isString';
import { UpLinks } from '@verdaccio/types';
import { Time } from '../types/packageMeta';
export const TIMEFORMAT = 'L LTS';
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
/**
* Formats license field for webui.
* @see https://docs.npmjs.com/files/package.json#license
*/
// License should use type License defined above, but conflicts with the unit test that provide array or empty object
/* eslint-disable @typescript-eslint/no-explicit-any */
export function formatLicense(license: any): string | undefined {
if (isString(license)) {
return license;
}
if (license?.type) {
return license.type;
}
return;
}
export interface Repository {
type: string;
url: string;
}
/**
* Formats repository field for webui.
* @see https://docs.npmjs.com/files/package.json#repository
*/
// Repository should use type Repository defined above, but conflicts with the unit test that provide array or empty object
/* eslint-disable @typescript-eslint/no-explicit-any */
export function formatRepository(repository: any): string | null {
if (isString(repository)) {
return repository;
}
if (repository?.url) {
return repository.url;
}
return null;
}
export function formatDate(lastUpdate: string | number): string {
return dayjs(new Date(lastUpdate)).format(TIMEFORMAT);
}
export function formatDateDistance(lastUpdate: Date | string | number): string {
return dayjs(new Date(lastUpdate)).fromNow();
}
/**
* For <LastSync /> component
* @param {array} uplinks
*/
export function getLastUpdatedPackageTime(uplinks: UpLinks = {}): string {
let lastUpdate = 0;
Object.keys(uplinks).forEach(function computeUplink(upLinkName): void {
const status = uplinks[upLinkName];
if (status.fetched > lastUpdate) {
lastUpdate = status.fetched;
}
});
return lastUpdate ? formatDate(lastUpdate) : '';
}
/**
* For <LastSync /> component
* @param {Object} time
* @returns {Array} last 3 releases
*/
export function getRecentReleases(time: Time = {}): Time[] {
const recent = Object.keys(time).map((version) => ({
version,
time: formatDate(time[version]),
}));
return recent.slice(recent.length - 3, recent.length).reverse();
}
export function getAuthorName(authorName?: string): string {
if (!authorName) {
return i18next.t('author-unknown');
}
if (authorName.toLowerCase() === 'anonymous') {
return i18next.t('author-anonymous');
}
return authorName;
}
export function fileSizeSI(
a: number,
b?: typeof Math,
c?: (p: number) => number,
d?: number,
e?: number
): string {
return (
((b = Math), (c = b.log), (d = 1e3), (e = (c(a) / c(d)) | 0), a / b.pow(d, e)).toFixed(2) +
' ' +
(e ? 'kMGTPEZY'[--e] + 'B' : 'Bytes')
);
}