Skip to content

Commit

Permalink
- empty values now are parsed as unset in range (#4465)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-tate authored Nov 29, 2024
1 parent 33c8da5 commit d04c38b
Show file tree
Hide file tree
Showing 32 changed files with 867 additions and 469 deletions.
21 changes: 14 additions & 7 deletions packages/date-adapters/src/date-fns/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,24 @@ export class AdapterDateFns implements SaltDateAdapter<Date, Locale> {

/**
* Formats a Date object using the specified format string.
* Returns an empty string when null or undefined date is given.
* @param date - The Date object to format.
* @param format - The format string to use.
* @param locale - The locale to use for formatting.
* @returns The formatted date string.
*/
public format(
date: Date,
date: Date | null | undefined,
format: RecommendedFormats = "dd MMM yyyy",
locale?: Locale,
): string {
const dateFnsFormat = this.mapToDateFnsFormat(format);
return formatDateFns(date, dateFnsFormat, {
locale: locale ?? this.locale,
});
if (this.isValid(date)) {
const dateFnsFormat = this.mapToDateFnsFormat(format);
return formatDateFns(date, dateFnsFormat, {
locale: locale ?? this.locale,
});
}
return "";
}

/**
Expand Down Expand Up @@ -201,13 +205,16 @@ export class AdapterDateFns implements SaltDateAdapter<Date, Locale> {
value,
};
}
const isDateDefined = !!value?.trim().length;
return {
date: parsedDate,
value,
errors: [
{
message: "not a valid date",
type: DateDetailErrorEnum.INVALID_DATE,
message: isDateDefined ? "not a valid date" : "no date defined",
type: isDateDefined
? DateDetailErrorEnum.INVALID_DATE
: DateDetailErrorEnum.UNSET,
},
],
};
Expand Down
16 changes: 11 additions & 5 deletions packages/date-adapters/src/dayjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,21 @@ export class AdapterDayjs implements SaltDateAdapter<Dayjs, string> {

/**
* Formats a Day.js date object using the specified format string.
* Returns an empty string when null or undefined date is given.
* @param date - The Day.js date object to format.
* @param format - The format string to use.
* @param locale - The locale to use for formatting.
* @returns The formatted date string.
*/
public format(
date: Dayjs,
date: Dayjs | null | undefined,
format: RecommendedFormats = "DD MMM YYYY",
locale?: string,
): string {
return date.locale(locale ?? this.locale).format(format);
if (this.isValid(date)) {
return date.locale(locale ?? this.locale).format(format);
}
return "";
}

/**
Expand Down Expand Up @@ -211,14 +215,16 @@ export class AdapterDayjs implements SaltDateAdapter<Dayjs, string> {
value,
};
}

const isDateDefined = !!value?.trim().length;
return {
date: this.dayjs("Invalid Date"),
value,
errors: [
{
message: "not a valid date",
type: DateDetailErrorEnum.INVALID_DATE,
message: isDateDefined ? "not a valid date" : "no date defined",
type: isDateDefined
? DateDetailErrorEnum.INVALID_DATE
: DateDetailErrorEnum.UNSET,
},
],
};
Expand Down
17 changes: 12 additions & 5 deletions packages/date-adapters/src/luxon/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,22 @@ export class AdapterLuxon implements SaltDateAdapter<DateTime, string> {

/**
* Formats a Luxon DateTime object using the specified format string.
* Returns an empty string when null or undefined date is given.
* @param date - The Luxon DateTime object to format.
* @param format - The format string to use.
* @param locale - The locale to use for formatting.
* @returns The formatted date string.
*/
public format(
date: DateTime,
date: DateTime | null | undefined,
format: RecommendedFormats = "dd MMM yyyy",
locale?: string,
): string {
const luxonFormat = this.mapToLuxonFormat(format);
return date.setLocale(locale ?? this.locale).toFormat(luxonFormat);
if (this.isValid(date)) {
const luxonFormat = this.mapToLuxonFormat(format);
return date.setLocale(locale ?? this.locale).toFormat(luxonFormat);
}
return "";
}

/**
Expand Down Expand Up @@ -222,13 +226,16 @@ export class AdapterLuxon implements SaltDateAdapter<DateTime, string> {
value,
};
}
const isDateDefined = !!value?.trim().length;
return {
date: parsedDate,
value,
errors: [
{
message: "not a valid date",
type: DateDetailErrorEnum.INVALID_DATE,
message: isDateDefined ? "not a valid date" : "no date defined",
type: isDateDefined
? DateDetailErrorEnum.INVALID_DATE
: DateDetailErrorEnum.UNSET,
},
],
};
Expand Down
21 changes: 14 additions & 7 deletions packages/date-adapters/src/moment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,24 @@ export class AdapterMoment implements SaltDateAdapter<Moment, string> {

/**
* Formats a Moment.js date object using the specified format string.
* Returns an empty string when null or undefined date is given.
* @param date - The Moment.js date object to format.
* @param format - The format string to use.
* @param locale - The locale to use for formatting.
* @returns The formatted date string.
*/
public format(
date: Moment,
date: Moment | null | undefined,
format: RecommendedFormats = "DD MMM YYYY",
locale?: string,
): string {
return date
.clone()
.locale(locale ?? this.locale)
.format(format);
if (this.isValid(date)) {
return date
.clone()
.locale(locale ?? this.locale)
.format(format);
}
return "";
}

/**
Expand Down Expand Up @@ -203,13 +207,16 @@ export class AdapterMoment implements SaltDateAdapter<Moment, string> {
value,
};
}
const isDateDefined = !!value?.trim().length;
return {
date: parsedDate,
value,
errors: [
{
message: "not a valid date",
type: DateDetailErrorEnum.INVALID_DATE,
message: isDateDefined ? "not a valid date" : "no date defined",
type: isDateDefined
? DateDetailErrorEnum.INVALID_DATE
: DateDetailErrorEnum.UNSET,
},
],
};
Expand Down
8 changes: 6 additions & 2 deletions packages/date-adapters/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ export interface SaltDateAdapter<
* @param date - The date object to format.
* @param format - The format string to use.
* @param locale - The locale to use for formatting.
* @returns The formatted date string.
* Returns an empty string when null or undefined date is given.
*/
format(date: TDate, format?: RecommendedFormats, locale?: TLocale): string;
format(
date: TDate | null | undefined,
format?: RecommendedFormats,
locale?: TLocale,
): string;

/**
* Compares two date objects.
Expand Down
Loading

0 comments on commit d04c38b

Please sign in to comment.