@@ -7,66 +7,70 @@ import { make_reactive } from './utils.js';
7
7
* we could check all of these edge-cases but I think that might become complicated very soon and introduce more bugs
8
8
* also there is the possibility of these behaviors to change as well,
9
9
* so creating a new date and applying the change is a better idea I guess
10
- * @param {Date } current_datetime
11
- * @param {Date } new_datetime
12
- * @param {import("./utils.js").InterceptorOptions<Date, (keyof Date)[], (keyof Date)[]>["notify_read_properties"] } notify_read_properties
10
+ * @param {import("./utils.js").InterceptorOptions<Date, (keyof Date)[], (keyof Date)[]> } options
11
+ * @param {unknown[] } params
13
12
* @return {boolean } - returns true if any changes happened
14
13
*/
15
- const notify_datetime_changes = ( current_datetime , new_datetime , notify_read_properties ) => {
16
- let had_time_changes = false ;
17
- let had_date_changes = false ;
14
+ const notify_datetime_changes = ( options , ... params ) => {
15
+ let is_time_changed = false ;
16
+ let is_date_changed = false ;
18
17
19
- if ( current_datetime . getFullYear ( ) !== new_datetime . getFullYear ( ) ) {
20
- notify_read_properties ( [ 'getFullYear' , 'getUTCFullYear' ] ) ;
21
- had_date_changes = true ;
18
+ const new_datetime = new Date ( options . value ) ;
19
+
20
+ // @ts -ignore
21
+ new_datetime [ options . property ] ( ...params ) ;
22
+
23
+ if ( options . value . getFullYear ( ) !== new_datetime . getFullYear ( ) ) {
24
+ options . notify_read_properties ( [ 'getFullYear' , 'getUTCFullYear' ] ) ;
25
+ is_date_changed = true ;
22
26
}
23
27
24
28
// @ts -expect-error
25
- if ( current_datetime . getYear && current_datetime . getYear ( ) !== new_datetime . getYear ( ) ) {
29
+ if ( options . value . getYear && options . value . getYear ( ) !== new_datetime . getYear ( ) ) {
26
30
// @ts -expect-error
27
- notify_read_properties ( [ 'getYear' ] ) ;
28
- had_date_changes = true ;
31
+ options . notify_read_properties ( [ 'getYear' ] ) ;
32
+ is_date_changed = true ;
29
33
}
30
34
31
- if ( current_datetime . getMonth ( ) !== new_datetime . getMonth ( ) ) {
32
- notify_read_properties ( [ 'getMonth' , 'getUTCMonth' ] ) ;
33
- had_date_changes = true ;
35
+ if ( options . value . getMonth ( ) !== new_datetime . getMonth ( ) ) {
36
+ options . notify_read_properties ( [ 'getMonth' , 'getUTCMonth' ] ) ;
37
+ is_date_changed = true ;
34
38
}
35
39
36
- if ( current_datetime . getDay ( ) !== new_datetime . getDay ( ) ) {
37
- notify_read_properties ( [ 'getDay' , 'getUTCDay' ] ) ;
38
- had_date_changes = true ;
40
+ if ( options . value . getDay ( ) !== new_datetime . getDay ( ) ) {
41
+ options . notify_read_properties ( [ 'getDay' , 'getUTCDay' ] ) ;
42
+ is_date_changed = true ;
39
43
}
40
44
41
- if ( current_datetime . getHours ( ) !== new_datetime . getHours ( ) ) {
42
- notify_read_properties ( [ 'getHours' , 'getUTCHours' ] ) ;
43
- had_time_changes = true ;
45
+ if ( options . value . getHours ( ) !== new_datetime . getHours ( ) ) {
46
+ options . notify_read_properties ( [ 'getHours' , 'getUTCHours' ] ) ;
47
+ is_time_changed = true ;
44
48
}
45
49
46
- if ( current_datetime . getMinutes ( ) !== new_datetime . getMinutes ( ) ) {
47
- notify_read_properties ( [ 'getMinutes' , 'getUTCMinutes' ] ) ;
48
- had_time_changes = true ;
50
+ if ( options . value . getMinutes ( ) !== new_datetime . getMinutes ( ) ) {
51
+ options . notify_read_properties ( [ 'getMinutes' , 'getUTCMinutes' ] ) ;
52
+ is_time_changed = true ;
49
53
}
50
54
51
- if ( current_datetime . getSeconds ( ) !== new_datetime . getSeconds ( ) ) {
52
- notify_read_properties ( [ 'getSeconds' , 'getUTCSeconds' ] ) ;
53
- had_time_changes = true ;
55
+ if ( options . value . getSeconds ( ) !== new_datetime . getSeconds ( ) ) {
56
+ options . notify_read_properties ( [ 'getSeconds' , 'getUTCSeconds' ] ) ;
57
+ is_time_changed = true ;
54
58
}
55
59
56
- if ( current_datetime . getMilliseconds ( ) !== new_datetime . getMilliseconds ( ) ) {
57
- notify_read_properties ( [ 'getMilliseconds' , 'getUTCMilliseconds' ] ) ;
58
- had_time_changes = true ;
60
+ if ( options . value . getMilliseconds ( ) !== new_datetime . getMilliseconds ( ) ) {
61
+ options . notify_read_properties ( [ 'getMilliseconds' , 'getUTCMilliseconds' ] ) ;
62
+ is_time_changed = true ;
59
63
}
60
64
61
- if ( had_time_changes ) {
62
- notify_read_properties ( [ 'toTimeString' , 'toLocaleTimeString' ] ) ;
65
+ if ( is_time_changed ) {
66
+ options . notify_read_properties ( [ 'toTimeString' , 'toLocaleTimeString' ] ) ;
63
67
}
64
68
65
- if ( had_date_changes ) {
66
- notify_read_properties ( [ 'toDateString' , 'toLocaleDateString' ] ) ;
69
+ if ( is_date_changed ) {
70
+ options . notify_read_properties ( [ 'toDateString' , 'toLocaleDateString' ] ) ;
67
71
}
68
72
69
- return had_date_changes || had_time_changes ;
73
+ return is_date_changed || is_time_changed ;
70
74
} ;
71
75
72
76
export const ReactiveDate = make_reactive ( Date , {
@@ -116,119 +120,57 @@ export const ReactiveDate = make_reactive(Date, {
116
120
] ,
117
121
interceptors : {
118
122
setDate : ( options , ...params ) => {
119
- const tmp = new Date ( options . value ) ;
120
- tmp . setDate ( /**@type {number }*/ ( params [ 0 ] ) ) ;
121
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
123
+ return notify_datetime_changes ( options , ...params ) ;
122
124
} ,
123
125
setFullYear : ( options , ...params ) => {
124
- const tmp = new Date ( options . value ) ;
125
- tmp . setFullYear (
126
- /**@type {number }*/ ( params [ 0 ] ) ,
127
- /**@type {number | undefined }*/ ( params [ 1 ] ) ,
128
- /**@type {number | undefined }*/ ( params [ 2 ] )
129
- ) ;
130
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
126
+ return notify_datetime_changes ( options , ...params ) ;
131
127
} ,
132
128
setHours : ( options , ...params ) => {
133
- const tmp = new Date ( options . value ) ;
134
- tmp . setHours (
135
- /**@type {number }*/ ( params [ 0 ] ) ,
136
- /**@type {number | undefined }*/ ( params [ 1 ] ) ,
137
- /**@type {number | undefined }*/ ( params [ 2 ] ) ,
138
- /**@type {number | undefined }*/ ( params [ 3 ] )
139
- ) ;
140
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
129
+ return notify_datetime_changes ( options , ...params ) ;
141
130
} ,
142
131
setMilliseconds : ( options , ...params ) => {
143
- const tmp = new Date ( options . value ) ;
144
- tmp . setMilliseconds ( /**@type {number }*/ ( params [ 0 ] ) ) ;
145
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
132
+ return notify_datetime_changes ( options , ...params ) ;
146
133
} ,
147
134
setMinutes : ( options , ...params ) => {
148
- const tmp = new Date ( options . value ) ;
149
- tmp . setMinutes (
150
- /**@type {number }*/ ( params [ 0 ] ) ,
151
- /**@type {number | undefined }*/ ( params [ 1 ] ) ,
152
- /**@type {number | undefined }*/ ( params [ 2 ] )
153
- ) ;
154
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
135
+ return notify_datetime_changes ( options , ...params ) ;
155
136
} ,
156
137
setMonth : ( options , ...params ) => {
157
- const tmp = new Date ( options . value ) ;
158
- tmp . setMonth ( /**@type {number }*/ ( params [ 0 ] ) , /**@type {number | undefined }*/ ( params [ 1 ] ) ) ;
159
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
138
+ return notify_datetime_changes ( options , ...params ) ;
160
139
} ,
161
140
setSeconds : ( options , ...params ) => {
162
- const tmp = new Date ( options . value ) ;
163
- tmp . setSeconds ( /**@type {number }*/ ( params [ 0 ] ) , /**@type {number | undefined }*/ ( params [ 1 ] ) ) ;
164
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
141
+ return notify_datetime_changes ( options , ...params ) ;
165
142
} ,
166
143
setTime : ( options , ...params ) => {
167
- const tmp = new Date ( options . value ) ;
168
- tmp . setTime ( /**@type {number }*/ ( params [ 0 ] ) ) ;
169
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
144
+ return notify_datetime_changes ( options , ...params ) ;
170
145
} ,
171
146
setUTCDate : ( options , ...params ) => {
172
- const tmp = new Date ( options . value ) ;
173
- tmp . setUTCDate ( /**@type {number }*/ ( params [ 0 ] ) ) ;
174
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
147
+ return notify_datetime_changes ( options , ...params ) ;
175
148
} ,
176
149
setUTCFullYear : ( options , ...params ) => {
177
- const tmp = new Date ( options . value ) ;
178
- tmp . setUTCFullYear (
179
- /**@type {number }*/ ( params [ 0 ] ) ,
180
- /**@type {number | undefined }*/ ( params [ 1 ] ) ,
181
- /**@type {number | undefined }*/ ( params [ 2 ] )
182
- ) ;
183
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
150
+ return notify_datetime_changes ( options , ...params ) ;
184
151
} ,
185
152
setUTCHours : ( options , ...params ) => {
186
- const tmp = new Date ( options . value ) ;
187
- tmp . setUTCHours (
188
- /**@type {number }*/ ( params [ 0 ] ) ,
189
- /**@type {number | undefined }*/ ( params [ 1 ] ) ,
190
- /**@type {number | undefined }*/ ( params [ 2 ] ) ,
191
- /**@type {number | undefined }*/ ( params [ 3 ] )
192
- ) ;
193
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
153
+ return notify_datetime_changes ( options , ...params ) ;
194
154
} ,
195
155
setUTCMilliseconds : ( options , ...params ) => {
196
- const tmp = new Date ( options . value ) ;
197
- tmp . setUTCMilliseconds ( /**@type {number }*/ ( params [ 0 ] ) ) ;
198
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
156
+ return notify_datetime_changes ( options , ...params ) ;
199
157
} ,
200
158
setUTCMinutes : ( options , ...params ) => {
201
- const tmp = new Date ( options . value ) ;
202
- tmp . setUTCMinutes (
203
- /**@type {number }*/ ( params [ 0 ] ) ,
204
- /**@type {number | undefined }*/ ( params [ 1 ] ) ,
205
- /**@type {number | undefined }*/ ( params [ 2 ] )
206
- ) ;
207
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
159
+ return notify_datetime_changes ( options , ...params ) ;
208
160
} ,
209
161
setUTCMonth : ( options , ...params ) => {
210
- const tmp = new Date ( options . value ) ;
211
- tmp . setUTCMonth ( /**@type {number }*/ ( params [ 0 ] ) , /**@type {number | undefined }*/ ( params [ 1 ] ) ) ;
212
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
162
+ return notify_datetime_changes ( options , ...params ) ;
213
163
} ,
214
164
setUTCSeconds : ( options , ...params ) => {
215
- const tmp = new Date ( options . value ) ;
216
- tmp . setUTCSeconds (
217
- /**@type {number }*/ ( params [ 0 ] ) ,
218
- /**@type {number | undefined }*/ ( params [ 1 ] )
219
- ) ;
220
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
165
+ return notify_datetime_changes ( options , ...params ) ;
221
166
} ,
222
167
// @ts -expect-error - deprecated method
223
168
setYear : ( options , ...params ) => {
224
169
// it might be removed from browsers
225
170
if ( ! options . value . getYear ) {
226
171
return false ;
227
172
}
228
- const tmp = new Date ( options . value ) ;
229
- // @ts -expect-error
230
- tmp . setYear ( /**@type {number }*/ ( params [ 0 ] ) ) ;
231
- return notify_datetime_changes ( options . value , tmp , options . notify_read_properties ) ;
173
+ return notify_datetime_changes ( options , ...params ) ;
232
174
}
233
175
}
234
176
} ) ;
0 commit comments