@@ -108,63 +108,10 @@ describe('Duration', () => {
108
108
} ) ;
109
109
describe ( 'Duration.add()' , ( ) => {
110
110
const duration = Duration . from ( { days : 1 , minutes : 5 } ) ;
111
- it ( 'adds same units' , ( ) => {
112
- equal ( `${ duration . add ( { days : 2 , minutes : 5 } ) } ` , 'P3DT10M' ) ;
113
- } ) ;
114
- it ( 'adds different units' , ( ) => {
115
- equal ( `${ duration . add ( { hours : 12 , seconds : 30 } ) } ` , 'P1DT12H5M30S' ) ;
116
- } ) ;
117
- it ( 'symmetric with regard to negative durations' , ( ) => {
118
- equal ( `${ Duration . from ( 'P3DT10M' ) . add ( { days : - 2 , minutes : - 5 } ) } ` , 'P1DT5M' ) ;
119
- equal ( `${ Duration . from ( 'P1DT12H5M30S' ) . add ( { hours : - 12 , seconds : - 30 } ) } ` , 'P1DT5M' ) ;
120
- } ) ;
121
- it ( 'balances time units even if both operands are positive' , ( ) => {
122
- const d = Duration . from ( 'P50DT50H50M50.500500500S' ) ;
123
- const result = d . add ( d ) ;
124
- equal ( result . days , 104 ) ;
125
- equal ( result . hours , 5 ) ;
126
- equal ( result . minutes , 41 ) ;
127
- equal ( result . seconds , 41 ) ;
128
- equal ( result . milliseconds , 1 ) ;
129
- equal ( result . microseconds , 1 ) ;
130
- equal ( result . nanoseconds , 0 ) ;
131
- } ) ;
132
- it ( 'balances correctly if adding different units flips the overall sign' , ( ) => {
133
- const d1 = Duration . from ( { hours : - 1 , seconds : - 60 } ) ;
134
- equal ( `${ d1 . add ( { minutes : 122 } ) } ` , 'PT1H1M' ) ;
135
- const d2 = Duration . from ( { hours : - 1 , seconds : - 3721 } ) ;
136
- equal ( `${ d2 . add ( { minutes : 61 , nanoseconds : 3722000000001 } ) } ` , 'PT1M1.000000001S' ) ;
137
- } ) ;
138
111
const max = new Duration ( 0 , 0 , 0 , ...Array ( 7 ) . fill ( Number . MAX_VALUE ) ) ;
139
112
it ( 'always throws when addition overflows' , ( ) => {
140
113
throws ( ( ) => max . add ( max ) , RangeError ) ;
141
114
} ) ;
142
- it ( 'mixed positive and negative values always throw' , ( ) => {
143
- throws ( ( ) => duration . add ( { hours : 1 , minutes : - 30 } ) , RangeError ) ;
144
- } ) ;
145
- it ( 'relativeTo required for years, months, and weeks' , ( ) => {
146
- const d = Duration . from ( { hours : 1 } ) ;
147
- const dy = Duration . from ( { years : 1 } ) ;
148
- const dm = Duration . from ( { months : 1 } ) ;
149
- const dw = Duration . from ( { weeks : 1 } ) ;
150
- throws ( ( ) => d . add ( dy ) , RangeError ) ;
151
- throws ( ( ) => d . add ( dm ) , RangeError ) ;
152
- throws ( ( ) => d . add ( dw ) , RangeError ) ;
153
- throws ( ( ) => dy . add ( d ) , RangeError ) ;
154
- throws ( ( ) => dm . add ( d ) , RangeError ) ;
155
- throws ( ( ) => dw . add ( d ) , RangeError ) ;
156
- const relativeTo = Temporal . PlainDate . from ( '2000-01-01' ) ;
157
- equal ( `${ d . add ( dy , { relativeTo } ) } ` , 'P1YT1H' ) ;
158
- equal ( `${ d . add ( dm , { relativeTo } ) } ` , 'P1MT1H' ) ;
159
- equal ( `${ d . add ( dw , { relativeTo } ) } ` , 'P1WT1H' ) ;
160
- equal ( `${ dy . add ( d , { relativeTo } ) } ` , 'P1YT1H' ) ;
161
- equal ( `${ dm . add ( d , { relativeTo } ) } ` , 'P1MT1H' ) ;
162
- equal ( `${ dw . add ( d , { relativeTo } ) } ` , 'P1WT1H' ) ;
163
- } ) ;
164
- it ( 'object must contain at least one correctly-spelled property' , ( ) => {
165
- throws ( ( ) => duration . add ( { } ) , TypeError ) ;
166
- throws ( ( ) => duration . add ( { month : 12 } ) , TypeError ) ;
167
- } ) ;
168
115
it ( 'incorrectly-spelled properties are ignored' , ( ) => {
169
116
equal ( `${ duration . add ( { month : 1 , days : 1 } ) } ` , 'P2DT5M' ) ;
170
117
} ) ;
@@ -311,90 +258,6 @@ describe('Duration', () => {
311
258
} ) ;
312
259
describe ( 'Duration.subtract()' , ( ) => {
313
260
const duration = Duration . from ( { days : 3 , hours : 1 , minutes : 10 } ) ;
314
- it ( 'subtracts same units with positive result' , ( ) => {
315
- equal ( `${ duration . subtract ( { days : 1 , minutes : 5 } ) } ` , 'P2DT1H5M' ) ;
316
- } ) ;
317
- it ( 'subtracts same units with zero result' , ( ) => {
318
- equal ( `${ duration . subtract ( duration ) } ` , 'PT0S' ) ;
319
- equal ( `${ duration . subtract ( { days : 3 } ) } ` , 'PT1H10M' ) ;
320
- equal ( `${ duration . subtract ( { minutes : 10 } ) } ` , 'P3DT1H' ) ;
321
- } ) ;
322
- it ( 'balances when subtracting same units with negative result' , ( ) => {
323
- equal ( `${ duration . subtract ( { minutes : 15 } ) } ` , 'P3DT55M' ) ;
324
- } ) ;
325
- it ( 'balances when subtracting different units' , ( ) => {
326
- equal ( `${ duration . subtract ( { seconds : 30 } ) } ` , 'P3DT1H9M30S' ) ;
327
- } ) ;
328
- it ( 'symmetric with regard to negative durations' , ( ) => {
329
- equal ( `${ Duration . from ( 'P2DT1H5M' ) . subtract ( { days : - 1 , minutes : - 5 } ) } ` , 'P3DT1H10M' ) ;
330
- equal ( `${ new Duration ( ) . subtract ( { days : - 3 , hours : - 1 , minutes : - 10 } ) } ` , 'P3DT1H10M' ) ;
331
- equal ( `${ Duration . from ( 'PT1H10M' ) . subtract ( { days : - 3 } ) } ` , 'P3DT1H10M' ) ;
332
- equal ( `${ Duration . from ( 'P3DT1H' ) . subtract ( { minutes : - 10 } ) } ` , 'P3DT1H10M' ) ;
333
- equal ( `${ Duration . from ( 'P3DT55M' ) . subtract ( { minutes : - 15 } ) } ` , 'P3DT1H10M' ) ;
334
- equal ( `${ Duration . from ( 'P3DT1H9M30S' ) . subtract ( { seconds : - 30 } ) } ` , 'P3DT1H10M' ) ;
335
- } ) ;
336
- it ( 'balances positive units up to the largest nonzero unit' , ( ) => {
337
- const d = Duration . from ( {
338
- minutes : 100 ,
339
- seconds : 100 ,
340
- milliseconds : 2000 ,
341
- microseconds : 2000 ,
342
- nanoseconds : 2000
343
- } ) ;
344
- const less = Duration . from ( {
345
- minutes : 10 ,
346
- seconds : 10 ,
347
- milliseconds : 500 ,
348
- microseconds : 500 ,
349
- nanoseconds : 500
350
- } ) ;
351
- const result = d . subtract ( less ) ;
352
- equal ( result . minutes , 91 ) ;
353
- equal ( result . seconds , 31 ) ;
354
- equal ( result . milliseconds , 501 ) ;
355
- equal ( result . microseconds , 501 ) ;
356
- equal ( result . nanoseconds , 500 ) ;
357
- } ) ;
358
- const tenDays = Duration . from ( 'P10D' ) ;
359
- const tenMinutes = Duration . from ( 'PT10M' ) ;
360
- it ( 'has correct negative result' , ( ) => {
361
- let result = tenDays . subtract ( { days : 15 } ) ;
362
- equal ( result . days , - 5 ) ;
363
- result = tenMinutes . subtract ( { minutes : 15 } ) ;
364
- equal ( result . minutes , - 5 ) ;
365
- } ) ;
366
- it ( 'balances correctly if subtracting different units flips the overall sign' , ( ) => {
367
- const d1 = Duration . from ( { hours : 1 , seconds : 60 } ) ;
368
- equal ( `${ d1 . subtract ( { minutes : 122 } ) } ` , '-PT1H1M' ) ;
369
- const d2 = Duration . from ( { hours : 1 , seconds : 3721 } ) ;
370
- equal ( `${ d2 . subtract ( { minutes : 61 , nanoseconds : 3722000000001 } ) } ` , '-PT1M1.000000001S' ) ;
371
- } ) ;
372
- it ( 'mixed positive and negative values always throw' , ( ) => {
373
- throws ( ( ) => duration . subtract ( { hours : 1 , minutes : - 30 } ) , RangeError ) ;
374
- } ) ;
375
- it ( 'relativeTo required for years, months, and weeks' , ( ) => {
376
- const d = Duration . from ( { hours : 1 } ) ;
377
- const dy = Duration . from ( { years : 1 , hours : 1 } ) ;
378
- const dm = Duration . from ( { months : 1 , hours : 1 } ) ;
379
- const dw = Duration . from ( { weeks : 1 , hours : 1 } ) ;
380
- throws ( ( ) => d . subtract ( dy ) , RangeError ) ;
381
- throws ( ( ) => d . subtract ( dm ) , RangeError ) ;
382
- throws ( ( ) => d . subtract ( dw ) , RangeError ) ;
383
- throws ( ( ) => dy . subtract ( d ) , RangeError ) ;
384
- throws ( ( ) => dm . subtract ( d ) , RangeError ) ;
385
- throws ( ( ) => dw . subtract ( d ) , RangeError ) ;
386
- const relativeTo = Temporal . PlainDate . from ( '2000-01-01' ) ;
387
- equal ( `${ d . subtract ( dy , { relativeTo } ) } ` , '-P1Y' ) ;
388
- equal ( `${ d . subtract ( dm , { relativeTo } ) } ` , '-P1M' ) ;
389
- equal ( `${ d . subtract ( dw , { relativeTo } ) } ` , '-P1W' ) ;
390
- equal ( `${ dy . subtract ( d , { relativeTo } ) } ` , 'P1Y' ) ;
391
- equal ( `${ dm . subtract ( d , { relativeTo } ) } ` , 'P1M' ) ;
392
- equal ( `${ dw . subtract ( d , { relativeTo } ) } ` , 'P1W' ) ;
393
- } ) ;
394
- it ( 'object must contain at least one correctly-spelled property' , ( ) => {
395
- throws ( ( ) => duration . subtract ( { } ) , TypeError ) ;
396
- throws ( ( ) => duration . subtract ( { month : 12 } ) , TypeError ) ;
397
- } ) ;
398
261
it ( 'incorrectly-spelled properties are ignored' , ( ) => {
399
262
equal ( `${ duration . subtract ( { month : 1 , days : 1 } ) } ` , 'P2DT1H10M' ) ;
400
263
} ) ;
0 commit comments