1
+ import { setInspectOnClass , setToStringTag } from '@seedcompany/common' ;
2
+ import { markSkipClassTransformation } from '@seedcompany/nest' ;
1
3
import { DateTime , DateTimeUnit , Interval } from 'luxon' ;
2
4
import { Writable as Mutable } from 'type-fest' ;
3
- import { inspect } from 'util' ;
4
5
5
6
/* eslint-disable @typescript-eslint/method-signature-style */
6
7
declare module 'luxon/src/interval' {
7
8
interface Interval {
8
- [ inspect . custom ] ( ) : string ;
9
-
10
9
/**
11
10
* Expand this interval to the full duration unit given
12
11
*/
@@ -28,25 +27,40 @@ declare module 'luxon/src/interval' {
28
27
}
29
28
/* eslint-enable @typescript-eslint/method-signature-style */
30
29
31
- Interval . prototype [ inspect . custom ] = function ( this : Interval ) {
32
- const format = ( dt : DateTime ) =>
33
- dt . toLocaleString ( DateTime . DATETIME_SHORT_WITH_SECONDS ) ;
34
- return `[Interval ${ format ( this . start ) } – ${ format ( this . end ) } )` ;
35
- } ;
30
+ setInspectOnClass ( Interval , ( i : Interval ) => ( { stylize } ) => {
31
+ return (
32
+ stylize ( `[Interval ` , 'special' ) +
33
+ `${ format ( i . start ) } – ${ format ( i . end ) } ` +
34
+ stylize ( `)` , 'special' )
35
+ ) ;
36
+ } ) ;
37
+ const format = ( dt : DateTime ) =>
38
+ dt . toLocaleString ( DateTime . DATETIME_SHORT_WITH_SECONDS ) ;
36
39
37
- Interval . prototype . expandToFull = function (
38
- this : Interval ,
39
- unit : DateTimeUnit ,
40
- ) {
41
- return Interval . fromDateTimes ( this . start . startOf ( unit ) , this . end . endOf ( unit ) ) ;
42
- } ;
40
+ setToStringTag ( Interval , 'Interval' ) ;
41
+ markSkipClassTransformation ( Interval ) ;
42
+
43
+ Object . defineProperty ( Interval . prototype , 'expandToFull' , {
44
+ configurable : true ,
45
+ value : function expandToFull ( this : Interval , unit : DateTimeUnit ) {
46
+ return Interval . fromDateTimes (
47
+ this . start . startOf ( unit ) ,
48
+ this . end . endOf ( unit ) ,
49
+ ) ;
50
+ } ,
51
+ } ) ;
43
52
44
- const IntervalStatic = Interval as Mutable < typeof Interval > ;
53
+ const IntervalStatic = new Proxy ( Interval , {
54
+ set ( target , p , value ) {
55
+ Object . defineProperty ( target , p , { value, configurable : true } ) ;
56
+ return true ;
57
+ } ,
58
+ } ) as Mutable < typeof Interval > ;
45
59
46
- IntervalStatic . compare = (
60
+ IntervalStatic . compare = function compare (
47
61
prev : Interval | null | undefined ,
48
62
now : Interval | null | undefined ,
49
- ) => {
63
+ ) {
50
64
const removals = ! prev ? [ ] : ! now ? [ prev ] : prev . difference ( now ) ;
51
65
const additions = ! now ? [ ] : ! prev ? [ now ] : now . difference ( prev ) ;
52
66
return { removals, additions } ;
0 commit comments