Skip to content

Commit

Permalink
fixup! utils ts
Browse files Browse the repository at this point in the history
  • Loading branch information
zmc committed Oct 11, 2024
1 parent d0a7c48 commit 5891000
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
import { format } from "date-fns";
import {
type MRT_PaginationState,
type MRT_ColumnFiltersState,
} from 'material-react-table';
import { format, type Duration } from "date-fns";

function formatDate(orig) {
function getUrl(path: string, filters: MRT_ColumnFiltersState, pagination: MRT_PaginationState) {
const newUrl = new URL(path, window.location.origin);
filters.forEach(item => {
if ( ! item.id ) return;
if ( item.value instanceof Function ) return;
if ( item.value instanceof Date ) {
newUrl.searchParams.set("date", formatDay(item.value));
} else {
newUrl.searchParams.set(String(item.id), String(item.value));
}
});
if ( pagination.pageIndex ) newUrl.searchParams.set("page", String(pagination.pageIndex));
newUrl.searchParams.set("pageSize", String(pagination.pageSize));
return newUrl;
}

function formatDate(orig: string | number | Date) {
if (!orig) return "";
return format(new Date(orig), "yyyy-MM-dd HH:mm:ss");
}

function formatDay(orig) {
function formatDay(orig: string | number | Date) {
if (!orig) return "";
return format(new Date(orig), "yyyy-MM-dd");
}

function pad(num) {
function pad(num: number) {
return `${num}`.padStart(2, "0");
}

function getDuration(seconds) {
const result = {};
function getDuration(seconds: number) {
const result: Duration = {};
let seconds_ = seconds;
result.days = Math.floor(seconds_ / (60 * 60 * 24));
seconds_ %= 60 * 60 * 24;
Expand All @@ -27,7 +47,7 @@ function getDuration(seconds) {
return result;
}

function formatDuration(seconds) {
function formatDuration(seconds: number) {
let result = seconds;
const hours = Math.floor(result / 3600);
result %= 3600;
Expand All @@ -36,10 +56,10 @@ function formatDuration(seconds) {
return `${pad(hours)}:${pad(minutes)}:${pad(result)}`;
}

function dirName(path) {
function dirName(path: string) {
const array = path.split("/");
array.splice(-1, 1, "");
return array.join("/");
}

export { formatDate, formatDay, getDuration, formatDuration, dirName };
export { formatDate, formatDay, getDuration, formatDuration, dirName, getUrl };

0 comments on commit 5891000

Please sign in to comment.