@@ -105,6 +105,17 @@ test("Duration#shiftTo handles mixed units", () => {
105
105
} ) ;
106
106
} ) ;
107
107
108
+ test ( "Duration#shiftTo does not produce unnecessary fractions in higher order units" , ( ) => {
109
+ const duration = Duration . fromObject (
110
+ { years : 2.5 , weeks : - 1 } ,
111
+ { conversionAccuracy : "longterm" }
112
+ ) ;
113
+ const shifted = duration . shiftTo ( "years" , "weeks" , "minutes" ) . toObject ( ) ;
114
+ expect ( shifted . years ) . toBe ( 2 ) ;
115
+ expect ( shifted . weeks ) . toBe ( 25 ) ;
116
+ expect ( shifted . minutes ) . toBeCloseTo ( 894.6 , 5 ) ;
117
+ } ) ;
118
+
108
119
//------
109
120
// #shiftToAll()
110
121
//-------
@@ -122,6 +133,22 @@ test("Duration#shiftToAll shifts to all available units", () => {
122
133
} ) ;
123
134
} ) ;
124
135
136
+ test ( "Duration#shiftToAll does not produce unnecessary fractions in higher order units" , ( ) => {
137
+ const duration = Duration . fromObject (
138
+ { years : 2.5 , weeks : - 1 , seconds : 0 } ,
139
+ { conversionAccuracy : "longterm" }
140
+ ) ;
141
+ const toAll = duration . shiftToAll ( ) . toObject ( ) ;
142
+ expect ( toAll . years ) . toBe ( 2 ) ;
143
+ expect ( toAll . months ) . toBe ( 5 ) ;
144
+ expect ( toAll . weeks ) . toBe ( 3 ) ;
145
+ expect ( toAll . days ) . toBe ( 2 ) ;
146
+ expect ( toAll . hours ) . toBe ( 10 ) ;
147
+ expect ( toAll . minutes ) . toBe ( 29 ) ;
148
+ expect ( toAll . seconds ) . toBe ( 6 ) ;
149
+ expect ( toAll . milliseconds ) . toBeCloseTo ( 0 , 5 ) ;
150
+ } ) ;
151
+
125
152
test ( "Duration#shiftToAll maintains invalidity" , ( ) => {
126
153
const dur = Duration . invalid ( "because" ) . shiftToAll ( ) ;
127
154
expect ( dur . isValid ) . toBe ( false ) ;
@@ -243,6 +270,40 @@ test("Duration#normalize can convert all unit pairs", () => {
243
270
}
244
271
} ) ;
245
272
273
+ test ( "Duration#normalize moves fractions to lower-order units" , ( ) => {
274
+ expect ( Duration . fromObject ( { years : 2.5 , days : 0 , hours : 0 } ) . normalize ( ) . toObject ( ) ) . toEqual ( {
275
+ years : 2 ,
276
+ days : 182 ,
277
+ hours : 12 ,
278
+ } ) ;
279
+ expect ( Duration . fromObject ( { years : - 2.5 , days : 0 , hours : 0 } ) . normalize ( ) . toObject ( ) ) . toEqual ( {
280
+ years : - 2 ,
281
+ days : - 182 ,
282
+ hours : - 12 ,
283
+ } ) ;
284
+ expect ( Duration . fromObject ( { years : 2.5 , days : 12 , hours : 0 } ) . normalize ( ) . toObject ( ) ) . toEqual ( {
285
+ years : 2 ,
286
+ days : 194 ,
287
+ hours : 12 ,
288
+ } ) ;
289
+ expect ( Duration . fromObject ( { years : 2.5 , days : 12.25 , hours : 0 } ) . normalize ( ) . toObject ( ) ) . toEqual (
290
+ { years : 2 , days : 194 , hours : 18 }
291
+ ) ;
292
+ } ) ;
293
+
294
+ test ( "Duration#normalize does not produce fractions in higher order units when rolling up negative lower order unit values" , ( ) => {
295
+ const normalized = Duration . fromObject (
296
+ { years : 100 , months : 0 , weeks : - 1 , days : 0 } ,
297
+ { conversionAccuracy : "longterm" }
298
+ )
299
+ . normalize ( )
300
+ . toObject ( ) ;
301
+ expect ( normalized . years ) . toBe ( 99 ) ;
302
+ expect ( normalized . months ) . toBe ( 11 ) ;
303
+ expect ( normalized . weeks ) . toBe ( 3 ) ;
304
+ expect ( normalized . days ) . toBeCloseTo ( 2.436875 , 7 ) ;
305
+ } ) ;
306
+
246
307
//------
247
308
// #rescale()
248
309
//-------
0 commit comments