From 368365aa477fc38f2f964527fd6af40d48b760a7 Mon Sep 17 00:00:00 2001 From: Jiri Lojda Date: Tue, 7 Jan 2025 13:54:55 +0100 Subject: [PATCH] Refactor serializeDateForFileName --- src/utils/files.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/utils/files.ts b/src/utils/files.ts index 29dd20e3..97f3f517 100644 --- a/src/utils/files.ts +++ b/src/utils/files.ts @@ -8,11 +8,15 @@ export enum DateLevel { } export const serializeDateForFileName = (date: Date, level: DateLevel) => - [ - date.getUTCFullYear(), - ...level >= DateLevel.Month ? [("0" + (date.getUTCMonth() + 1)).slice(-2)] : [], - ...level >= DateLevel.Day ? [("0" + date.getUTCDate()).slice(-2)] : [], - ...level >= DateLevel.Hour ? [("0" + date.getUTCHours()).slice(-2)] : [], - ...level >= DateLevel.Minute ? [("0" + date.getUTCMinutes()).slice(-2)] : [], - ...level >= DateLevel.Second ? [("0" + date.getUTCSeconds()).slice(-2)] : [], - ].join("-"); + createDateParts(date) + .slice(0, level + 1) + .join("-"); + +const createDateParts = (date: Date) => Object.values({ + [DateLevel.Year]: date.getUTCFullYear().toString(), + [DateLevel.Month]: ("0" + (date.getUTCMonth() + 1)).slice(-2), + [DateLevel.Day]: ("0" + date.getUTCDate()).slice(-2), + [DateLevel.Hour]: ("0" + date.getUTCHours()).slice(-2), + [DateLevel.Minute]: ("0" + date.getUTCMinutes()).slice(-2), + [DateLevel.Second]: ("0" + date.getUTCSeconds()).slice(-2), +} as const satisfies Record); \ No newline at end of file