66import type { DayOfWeek , WeekDay } from './_consts.ts' ;
77import type { WeeklyDate } from './_types.ts' ;
88import { sort } from 'fast-sort' ;
9- import * as v from 'valibot' ;
109import { DEFAULT_LOCALE } from './_consts.ts' ;
11- import { createWeeklyDate , dailyDateSchema } from './_types.ts' ;
10+ import { createWeeklyDate } from './_types.ts' ;
1211import { unreachable } from './_utils.ts' ;
1312
13+ // Re-export formatDateCompact from shared package
14+ export { formatDateCompact } from '@ccusage/terminal/table' ;
15+
1416/**
1517 * Sort order for date-based sorting
1618 */
@@ -31,24 +33,6 @@ function createDateFormatter(timezone: string | undefined, locale: string): Intl
3133 } ) ;
3234}
3335
34- /**
35- * Creates a date parts formatter with the specified timezone and locale
36- * @param timezone - Timezone to use
37- * @param locale - Locale to use for formatting
38- * @returns Intl.DateTimeFormat instance
39- */
40- function createDatePartsFormatter (
41- timezone : string | undefined ,
42- locale : string ,
43- ) : Intl . DateTimeFormat {
44- return new Intl . DateTimeFormat ( locale , {
45- year : 'numeric' ,
46- month : '2-digit' ,
47- day : '2-digit' ,
48- timeZone : timezone ,
49- } ) ;
50- }
51-
5236/**
5337 * Formats a date string to YYYY-MM-DD format
5438 * @param dateStr - Input date string
@@ -63,34 +47,6 @@ export function formatDate(dateStr: string, timezone?: string, locale?: string):
6347 return formatter . format ( date ) ;
6448}
6549
66- /**
67- * Formats a date string to compact format with year on first line and month-day on second
68- * @param dateStr - Input date string
69- * @param timezone - Timezone to use for formatting (pass undefined to use system timezone)
70- * @param locale - Locale to use for formatting
71- * @returns Formatted date string with newline separator (YYYY\nMM-DD)
72- */
73- export function formatDateCompact (
74- dateStr : string ,
75- timezone : string | undefined ,
76- locale : string ,
77- ) : string {
78- // For YYYY-MM-DD format, append T00:00:00 to parse as local date
79- // Without this, new Date('YYYY-MM-DD') interprets as UTC midnight
80- const parseResult = v . safeParse ( dailyDateSchema , dateStr ) ;
81- const date = parseResult . success
82- ? timezone != null
83- ? new Date ( `${ dateStr } T00:00:00Z` )
84- : new Date ( `${ dateStr } T00:00:00` )
85- : new Date ( dateStr ) ;
86- const formatter = createDatePartsFormatter ( timezone , locale ) ;
87- const parts = formatter . formatToParts ( date ) ;
88- const year = parts . find ( ( p ) => p . type === 'year' ) ?. value ?? '' ;
89- const month = parts . find ( ( p ) => p . type === 'month' ) ?. value ?? '' ;
90- const day = parts . find ( ( p ) => p . type === 'day' ) ?. value ?? '' ;
91- return `${ year } \n${ month } -${ day } ` ;
92- }
93-
9450/**
9551 * Generic function to sort items by date based on sort order
9652 * @param items - Array of items to sort
@@ -200,27 +156,7 @@ if (import.meta.vitest != null) {
200156 } ) ;
201157 } ) ;
202158
203- describe ( 'formatDateCompact' , ( ) => {
204- it ( 'should format date to compact format with newline' , ( ) => {
205- const result = formatDateCompact ( '2024-08-04' , undefined , 'en-US' ) ;
206- expect ( result ) . toBe ( '2024\n08-04' ) ;
207- } ) ;
208-
209- it ( 'should handle timezone parameter' , ( ) => {
210- const result = formatDateCompact ( '2024-08-04T12:00:00Z' , 'UTC' , 'en-US' ) ;
211- expect ( result ) . toBe ( '2024\n08-04' ) ;
212- } ) ;
213-
214- it ( 'should handle YYYY-MM-DD format dates' , ( ) => {
215- const result = formatDateCompact ( '2024-08-04' , undefined , 'en-US' ) ;
216- expect ( result ) . toBe ( '2024\n08-04' ) ;
217- } ) ;
218-
219- it ( 'should handle timezone with YYYY-MM-DD format' , ( ) => {
220- const result = formatDateCompact ( '2024-08-04' , 'UTC' , 'en-US' ) ;
221- expect ( result ) . toBe ( '2024\n08-04' ) ;
222- } ) ;
223- } ) ;
159+ // formatDateCompact tests are in @ccusage /terminal/table.ts
224160
225161 describe ( 'sortByDate' , ( ) => {
226162 const testData = [
0 commit comments