Skip to content

Commit

Permalink
feat(mirrorlist): new sizeString shim
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqVictor committed Nov 7, 2023
1 parent 0075c5e commit b50c06b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/components/MirrorList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
:name="item.alias"
:last-update="item.lastUpdateTimeString"
:status="item.status"
:size="item.size"
:size="item.sizeString"
:url="item.url"
:description="item.desc"
:is-shrinked="isShrinked" />
Expand All @@ -42,7 +42,7 @@
:name="item.alias"
:last-update="item.lastUpdateTimeString"
:status="item.status"
:size="item.size"
:size="item.sizeString"
:url="item.url"
:description="item.desc"
:is-shrinked="isShrinked" />
Expand Down
6 changes: 4 additions & 2 deletions src/types/MirrorItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export type MirrorItem = {
lastEnded?: number,
nextSchedule?: number,
upstream?: string,
size?: string,
size?: number,
sizeStr?: string,
url?: string,
type: 'mirror' | 'proxy' | 'git',
errorMsg?: string,
Expand All @@ -23,5 +24,6 @@ export type MirrorViewItem = MirrorItem & {
lastEndedTimeString?: string,
nextScheduleTimeString?: string,
lastOnlineTimeString?: string,
lastRegisterTimeString?: string
lastRegisterTimeString?: string,
sizeString?: string
}
9 changes: 8 additions & 1 deletion src/utils/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@ const dateStringFromMirrorItem = (d?: number): string | undefined => {
return d && d > 0 ? dateFormat(new Date(d * 1000)) : undefined
}

const convertSizeString = (bytes: number) => {
const exp = Math.floor(Math.log(bytes) / Math.log(1024))
const result = (bytes / Math.pow(1024, exp)).toFixed(2)
return result + ' ' + (exp === 0 ? 'B' : 'KMGTPEZY'[exp - 1] + 'B')
}

const mirrorViewShim = (mirrorItem: MirrorItem): MirrorViewItem => {
return { ...mirrorItem,
lastUpdateTimeString: dateStringFromMirrorItem(mirrorItem.lastUpdate),
lastStartedTimeString: dateStringFromMirrorItem(mirrorItem.lastStarted),
lastEndedTimeString: dateStringFromMirrorItem(mirrorItem.lastEnded),
nextScheduleTimeString: dateStringFromMirrorItem(mirrorItem.nextSchedule),
lastOnlineTimeString: dateStringFromMirrorItem(mirrorItem.lastOnline),
lastRegisterTimeString: dateStringFromMirrorItem(mirrorItem.lastRegister)
lastRegisterTimeString: dateStringFromMirrorItem(mirrorItem.lastRegister),
sizeString: mirrorItem.sizeStr || convertSizeString(mirrorItem.size ?? 0)
}
}

Expand Down

0 comments on commit b50c06b

Please sign in to comment.