Skip to content

Commit

Permalink
feat(date-time): Added returnEmptyIfNotSet and `returnEmptyIfInvali…
Browse files Browse the repository at this point in the history
…d` options to `formatDate`
  • Loading branch information
sullivanpj committed Oct 18, 2024
1 parent f890df9 commit 79cbf52
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
35 changes: 32 additions & 3 deletions packages/date-time/src/utilities/format-date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,48 @@ import type { Temporal } from "@js-temporal/polyfill";
import { StormDate } from "../storm-date";
import type { StormDateTime } from "../storm-date-time";

export type FormatDateOptions = Partial<Temporal.ShowCalendarOption> & {
/**
* Should an empty string be returned if the date is null or undefined
*
* @defaultValue false
*/
returnEmptyIfNotSet?: boolean;

/**
* Should an empty string be returned if the date is invalid
*
* @defaultValue false
*/
returnEmptyIfInvalid?: boolean;
};

/**
* Format a date field
*
* @param dateTime - The date time to format
* @returns The formatted date
*/
export const formatDate = (
dateTime: StormDateTime = StormDate.current(),
options?: Partial<Temporal.ShowCalendarOption>
dateTime?: StormDateTime | null,
options?: FormatDateOptions
): string => {
let value = dateTime;

const calendarName = options?.calendarName || "never";
if (!dateTime && options?.returnEmptyIfNotSet) {
return "";
}

if ((!dateTime || !dateTime.isValid) && options?.returnEmptyIfInvalid) {
return "";
}

if (!dateTime || !dateTime.isValid) {
value = StormDate.current();
}

return dateTime.zonedDateTime.toPlainDate().toString({
return value!.zonedDateTime.toPlainDate().toString({
calendarName
});
};
5 changes: 4 additions & 1 deletion packages/telemetry/src/logging/create-file-stream-logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-------------------------------------------------------------------*/

import type { StormConfig } from "@storm-software/config";
import { StormDate } from "@storm-stack/date-time/storm-date";
import { StormDateTime } from "@storm-stack/date-time/storm-date-time";
import { formatDate, formatDateTime } from "@storm-stack/date-time/utilities";
import { isSetString } from "@storm-stack/types/type-checks/is-set-string";
Expand Down Expand Up @@ -51,7 +52,9 @@ export const createFileStreamLogs = (
stream: pino.destination({
dest: join(
logPath,
formatDate().replaceAll("/", "-").replaceAll(" ", "-"),
formatDate(StormDate.current())
.replaceAll("/", "-")
.replaceAll(" ", "-"),
`${`${loggingConfig.fileName || "storm"}-`}${formatDateTime(
StormDateTime.current(),
{
Expand Down

0 comments on commit 79cbf52

Please sign in to comment.