1
0
Fork 1
mirror of https://github.com/SomboChea/ui synced 2024-05-18 09:21:37 +07:00
verdaccio-ui/src/utils/package.ts
Juan Picado @jotadeveloper 97e8448098
fix: refactoring version page / fix issue not found page #100 (#117)
* chore: refactoring version page

* refactor: migrate version page to hooks

* refactor: Version page better imports

* fix: #100 render not found on click item

* test: add test for version page

* chore: update mocks

* test: add scenario for not found package

* chore: fix wrong mock path

* chore: update mock

* chore: add todo list
2019-08-25 14:34:27 +02:00

92 lines
2.4 KiB
TypeScript

import { UpLinks } from '@verdaccio/types';
import isString from 'lodash/isString';
import format from 'date-fns/format';
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
import { isObject } from 'util';
export const TIMEFORMAT = 'DD.MM.YYYY, HH:mm:ss';
/**
* 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 && isObject(license) && 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 && isObject(repository) && repository.url) {
return repository.url;
}
return null;
}
export function formatDate(lastUpdate): string {
return format(new Date(lastUpdate), TIMEFORMAT);
}
export function formatDateDistance(lastUpdate): string {
return distanceInWordsToNow(new Date(lastUpdate));
}
export function buildScopePackage(scope: string, packageName: string) {
return `@${scope}/${packageName}`;
}
/**
* 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 = {}): unknown {
const recent = Object.keys(time).map((version): unknown => ({
version,
time: formatDate(time[version]),
}));
return recent.slice(recent.length - 3, recent.length).reverse();
}