Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve readibility of format normalization
  • Loading branch information
oandregal committed Nov 13, 2025
commit 14a756bee13ef6cff2f42674f436fd5722638911
18 changes: 10 additions & 8 deletions packages/dataviews/src/types/field-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,17 @@ export type Field< Item > = {
*/
type FormatDate = {
date?: string;
weekStartsOn?:
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';
weekStartsOn?: DayString;
};
export type DayNumber = 0 | 1 | 2 | 3 | 4 | 5 | 6;
export type DayString =
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';

export type NormalizedField< Item > = Omit< Field< Item >, 'Edit' > & {
label: string;
Expand Down
34 changes: 17 additions & 17 deletions packages/dataviews/src/utils/normalize-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getSettings } from '@wordpress/date';
*/
import getFieldTypeDefinition from '../field-types';
import type {
DayString,
DataViewRenderFieldProps,
Field,
FieldTypeDefinition,
Expand All @@ -26,7 +27,7 @@ import {
SINGLE_SELECTION_OPERATORS,
} from '../constants';
import hasElements from './has-elements';
import { numberToWeekStartsOn } from './week-starts-on';
import { numberToWeekStartsOn, DAYS_OF_WEEK } from './week-starts-on';

const getValueFromId =
( id: string ) =>
Expand Down Expand Up @@ -188,22 +189,21 @@ export default function normalizeFields< Item >(

const filterBy = getFilterBy( field, fieldTypeDefinition );

const format =
field.type === 'date'
? {
date:
field.format?.date !== undefined &&
typeof field.format.date === 'string'
? field.format.date
: getSettings().formats.date,
weekStartsOn:
field.format?.weekStartsOn !== undefined
? field.format.weekStartsOn
: numberToWeekStartsOn(
getSettings().l10n.startOfWeek
),
}
: {};
let format = {};
if ( field.type === 'date' ) {
format = {
date:
field.format?.date !== undefined &&
typeof field.format.date === 'string'
? field.format.date
: getSettings().formats.date,
weekStartsOn: DAYS_OF_WEEK.includes(
field.format?.weekStartsOn as DayString
)
? field?.format?.weekStartsOn
: numberToWeekStartsOn( getSettings().l10n.startOfWeek ),
};
}

return {
...field,
Expand Down
20 changes: 8 additions & 12 deletions packages/dataviews/src/utils/week-starts-on.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
type DayNumber = 0 | 1 | 2 | 3 | 4 | 5 | 6;
type DayString =
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday';
const days: DayString[] = [
/**
* Internal dependencies
*/
import type { DayNumber, DayString } from '../types/field-api';

export const DAYS_OF_WEEK: DayString[] = [
'sunday',
'monday',
'tuesday',
Expand All @@ -26,7 +22,7 @@ const DEFAULT_DAY_NUMBER = 0;
* @return The corresponding number (0 for Sunday, 1 for Monday, etc.)
*/
export function weekStartsOnToNumber( day: DayString ): DayNumber {
const index = days.indexOf( day );
const index = DAYS_OF_WEEK.indexOf( day );
if ( index === -1 ) {
return DEFAULT_DAY_NUMBER;
}
Expand All @@ -41,7 +37,7 @@ export function weekStartsOnToNumber( day: DayString ): DayNumber {
* @return The corresponding day name ('sunday', 'monday', etc.)
*/
export function numberToWeekStartsOn( day: DayNumber ): DayString {
const result = days[ day ];
const result = DAYS_OF_WEEK[ day ];
if ( result === undefined ) {
return DEFAULT_DAY_STRING;
}
Expand Down
Loading