Deduplicate duration and number formatting internals
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RaRBJFZKSTA7naHGuQHfnQ
This commit is contained in:
@@ -6,6 +6,8 @@
|
||||
// cindy.chang@dsp.im (Cindy Chang), 2024/5/30
|
||||
/** @module abbreviateNumber Duration abbreviation formatting. */
|
||||
|
||||
import formatDuration from "./formatDuration.js";
|
||||
|
||||
/**
|
||||
* Converts a total number of seconds into a human-readable abbreviated
|
||||
* duration string using days, hours, minutes, and seconds.
|
||||
@@ -15,27 +17,7 @@
|
||||
* or "0" if totalSeconds is zero.
|
||||
*/
|
||||
export default function abbreviateNumber(totalSeconds) {
|
||||
let seconds = 0;
|
||||
let minutes = 0;
|
||||
let hours = 0;
|
||||
let days = 0;
|
||||
let result = "";
|
||||
let symbols = ["d", "h", "m", "s"];
|
||||
|
||||
totalSeconds = Number.parseInt(totalSeconds);
|
||||
if (!Number.isNaN(totalSeconds)) {
|
||||
seconds = totalSeconds % 60;
|
||||
minutes = (Math.floor(totalSeconds - seconds) / 60) % 60;
|
||||
hours = Math.floor(totalSeconds / 3600) % 24;
|
||||
days = Math.floor(totalSeconds / (3600 * 24));
|
||||
}
|
||||
|
||||
const units = [days, hours, minutes, seconds];
|
||||
for (let i = 0; i < units.length; i++) {
|
||||
if (units[i] > 0) result += units[i] + symbols[i] + " ";
|
||||
}
|
||||
result = result.trim();
|
||||
if (totalSeconds === 0) result = "0";
|
||||
|
||||
return result;
|
||||
const parsed = Number.parseInt(totalSeconds);
|
||||
if (parsed === 0) return "0";
|
||||
return formatDuration(parsed, { separator: " " });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user