77 addYears ,
88 differenceInDays ,
99 differenceInHours ,
10+ differenceInMonths ,
1011 differenceInSeconds ,
1112 differenceInYears ,
1213 endOfDay ,
@@ -37,11 +38,11 @@ type DateRangeValue = { start: Date; end: Date };
3738
3839export class DateRange {
3940 #value: RangeName | { start : Date ; end : Date } ;
40- label : string ;
41+ variant ? : string ;
4142
4243 constructor ( value : RangeName | { start : Date ; end : Date } ) {
4344 this . #value = value ;
44- this . label = "" ;
45+ if ( typeof value === "string" ) this . variant = value ;
4546 }
4647
4748 get value ( ) : DateRangeValue {
@@ -52,10 +53,11 @@ export class DateRange {
5253 }
5354
5455 isCustom ( ) : boolean {
55- return typeof this . #value !== "string" ;
56+ return typeof this . #value !== "string" && ! this . variant ;
5657 }
5758
5859 format ( ) : string {
60+ if ( this . variant === "allTime" ) return "All Time" ;
5961 if ( typeof this . #value === "string" ) return wellKnownRanges [ this . #value] ;
6062 return formatDateRange ( this . #value. start , this . #value. end ) ;
6163 }
@@ -66,15 +68,19 @@ export class DateRange {
6668
6769 serialize ( ) : string {
6870 if ( typeof this . #value === "string" ) return this . #value;
69- return `${ Number ( this . #value. start ) } :${ Number ( this . #value. end ) } ` ;
71+ return `${ Number ( this . #value. start ) } :${ Number ( this . #value. end ) } : ${ this . variant } ` ;
7072 }
7173
7274 static deserialize ( range : string ) : DateRange {
7375 if ( ! range . includes ( ":" ) ) {
7476 return new DateRange ( range as RangeName ) ;
7577 }
76- const [ start , end ] = range . split ( ":" ) . map ( ( v ) => new Date ( Number ( v ) ) ) ;
77- return new DateRange ( { start, end } ) ;
78+ const [ start , end , variant ] = range . split ( ":" ) ;
79+ const dr = new DateRange ( { start : new Date ( Number ( start ) ) , end : new Date ( Number ( end ) ) } ) ;
80+ if ( variant ) {
81+ dr . variant = variant ;
82+ }
83+ return dr ;
7884 }
7985
8086 endsToday ( ) : boolean {
@@ -118,6 +124,7 @@ export class DateRange {
118124 }
119125
120126 previous ( ) {
127+ if ( this . variant === "allTime" ) return this ;
121128 if ( this . #value === "today" ) return new DateRange ( "yesterday" ) ;
122129
123130 if (
@@ -150,6 +157,18 @@ export class DateRange {
150157 return new DateRange ( { start, end } ) ;
151158 }
152159
160+ if ( differenceInMonths ( this . value . end , this . value . start ) === 12 ) {
161+ // if (isSameDay(this.value.start, startOfMonth(this.value.start))) {
162+ // const start = startOfMonth(subYears(this.value.start, 1));
163+ // const end = endOfMonth(subYears(this.value.end, 1));
164+ // return new DateRange({ start, end });
165+ // }
166+
167+ const start = subYears ( this . value . start , 1 ) ;
168+ const end = subYears ( this . value . end , 1 ) ;
169+ return new DateRange ( { start, end } ) ;
170+ }
171+
153172 if ( differenceInHours ( this . value . end , this . value . start ) < 23 ) {
154173 const start = subSeconds ( this . value . start , differenceInSeconds ( this . value . end , this . value . start ) ) ;
155174 const end = subSeconds ( this . value . end , differenceInSeconds ( this . value . end , this . value . start ) ) ;
@@ -198,6 +217,18 @@ export class DateRange {
198217 return new DateRange ( { start, end } ) ;
199218 }
200219
220+ if ( differenceInMonths ( this . value . end , this . value . start ) === 12 ) {
221+ // if (isSameDay(this.value.start, startOfMonth(this.value.start))) {
222+ // const start = startOfMonth(addYears(this.value.start, 1));
223+ // const end = endOfMonth(addYears(this.value.end, 1));
224+ // return new DateRange({ start, end });
225+ // }
226+
227+ const start = addYears ( this . value . start , 1 ) ;
228+ const end = addYears ( this . value . end , 1 ) ;
229+ return new DateRange ( { start, end } ) ;
230+ }
231+
201232 if ( differenceInHours ( this . value . end , this . value . start ) < 23 ) {
202233 const start = addSeconds ( this . value . start , differenceInSeconds ( this . value . end , this . value . start ) ) ;
203234 const end = addSeconds ( this . value . end , differenceInSeconds ( this . value . end , this . value . start ) ) ;
@@ -247,8 +278,8 @@ export const ranges: Record<RangeName, () => { range: { start: Date; end: Date }
247278 last7Days : ( ) => ( { range : lastXDays ( 7 ) } ) ,
248279 last30Days : ( ) => ( { range : lastXDays ( 30 ) } ) ,
249280 last12Months : ( ) => {
281+ const start = startOfMonth ( subYears ( new Date ( ) , 1 ) ) ;
250282 const end = endOfMonth ( new Date ( ) ) ;
251- const start = subMonths ( end , 11 ) ;
252283 return { range : { start, end } } ;
253284 } ,
254285 weekToDate : ( ) => {
0 commit comments