@@ -965,8 +965,8 @@ describe('Duration', () => {
965965 const d = new Duration ( 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 ) ;
966966 const d2 = new Duration ( 0 , 0 , 0 , 5 , 5 , 5 , 5 , 5 , 5 , 5 ) ;
967967 const relativeTo = Temporal . PlainDateTime . from ( '2020-01-01T00:00' ) ;
968- it ( 'options may only be an object' , ( ) => {
969- [ null , 1 , 'hello' , true , Symbol ( 'foo' ) , 1n ] . forEach ( ( badOptions ) => throws ( ( ) => d . round ( badOptions ) , TypeError ) ) ;
968+ it ( 'parameter may only be an object or string ' , ( ) => {
969+ [ null , 1 , true , Symbol ( 'foo' ) , 1n ] . forEach ( ( badOptions ) => throws ( ( ) => d . round ( badOptions ) , TypeError ) ) ;
970970 } ) ;
971971 it ( 'throws without parameter' , ( ) => {
972972 throws ( ( ) => d . round ( ) , TypeError ) ;
@@ -977,11 +977,16 @@ describe('Duration', () => {
977977 it ( "succeeds with largestUnit: 'auto'" , ( ) => {
978978 equal ( `${ Duration . from ( { hours : 25 } ) . round ( { largestUnit : 'auto' } ) } ` , 'PT25H' ) ;
979979 } ) ;
980- it ( 'throws on disallowed or invalid smallestUnit' , ( ) => {
980+ it ( 'throws on disallowed or invalid smallestUnit (object param) ' , ( ) => {
981981 [ 'era' , 'nonsense' ] . forEach ( ( smallestUnit ) => {
982982 throws ( ( ) => d . round ( { smallestUnit } ) , RangeError ) ;
983983 } ) ;
984984 } ) ;
985+ it ( 'throws on disallowed or invalid smallestUnit (string param)' , ( ) => {
986+ [ 'era' , 'nonsense' ] . forEach ( ( smallestUnit ) => {
987+ throws ( ( ) => d . round ( smallestUnit ) , RangeError ) ;
988+ } ) ;
989+ } ) ;
985990 it ( 'throws if smallestUnit is larger than largestUnit' , ( ) => {
986991 const units = [
987992 'years' ,
@@ -1003,6 +1008,24 @@ describe('Duration', () => {
10031008 }
10041009 }
10051010 } ) ;
1011+ it ( 'accepts string parameter as a shortcut for {smallestUnit}' , ( ) => {
1012+ const d = Temporal . Duration . from ( {
1013+ days : 1 ,
1014+ hours : 2 ,
1015+ minutes : 3 ,
1016+ seconds : 4 ,
1017+ milliseconds : 5 ,
1018+ microseconds : 6 ,
1019+ nanoseconds : 7
1020+ } ) ;
1021+ equal ( d . round ( 'day' ) . toString ( ) , 'P1D' ) ;
1022+ equal ( d . round ( 'hour' ) . toString ( ) , 'P1DT2H' ) ;
1023+ equal ( d . round ( 'minute' ) . toString ( ) , 'P1DT2H3M' ) ;
1024+ equal ( d . round ( 'second' ) . toString ( ) , 'P1DT2H3M4S' ) ;
1025+ equal ( d . round ( 'millisecond' ) . toString ( ) , 'P1DT2H3M4.005S' ) ;
1026+ equal ( d . round ( 'microsecond' ) . toString ( ) , 'P1DT2H3M4.005006S' ) ;
1027+ equal ( d . round ( 'nanosecond' ) . toString ( ) , 'P1DT2H3M4.005006007S' ) ;
1028+ } ) ;
10061029 it ( 'assumes a different default for largestUnit if smallestUnit is larger than the default' , ( ) => {
10071030 const almostYear = Duration . from ( { days : 364 } ) ;
10081031 equal ( `${ almostYear . round ( { smallestUnit : 'years' , relativeTo } ) } ` , 'P1Y' ) ;
@@ -1171,12 +1194,26 @@ describe('Duration', () => {
11711194 } ) ;
11721195 it ( 'throws if neither one of largestUnit or smallestUnit is given' , ( ) => {
11731196 const hoursOnly = new Duration ( 0 , 0 , 0 , 0 , 1 ) ;
1174- [ { } , ( ) => { } , { roundingMode : 'ceil' } ] . forEach ( ( options ) => {
1175- throws ( ( ) => d . round ( options ) , RangeError ) ;
1176- throws ( ( ) => hoursOnly . round ( options ) , RangeError ) ;
1177- } ) ;
1178- } ) ;
1179- it ( 'relativeTo is not required for rounding non-calendar units in durations without calendar units' , ( ) => {
1197+ [ { } , ( ) => { } , { roundingMode : 'ceil' } ] . forEach ( ( roundTo ) => {
1198+ throws ( ( ) => d . round ( roundTo ) , RangeError ) ;
1199+ throws ( ( ) => hoursOnly . round ( roundTo ) , RangeError ) ;
1200+ } ) ;
1201+ } ) ;
1202+ it ( 'relativeTo not required to round non-calendar units in durations w/o calendar units (string param)' , ( ) => {
1203+ equal ( `${ d2 . round ( 'days' ) } ` , 'P5D' ) ;
1204+ equal ( `${ d2 . round ( 'hours' ) } ` , 'P5DT5H' ) ;
1205+ equal ( `${ d2 . round ( 'minutes' ) } ` , 'P5DT5H5M' ) ;
1206+ equal ( `${ d2 . round ( 'seconds' ) } ` , 'P5DT5H5M5S' ) ;
1207+ equal ( `${ d2 . round ( 'milliseconds' ) } ` , 'P5DT5H5M5.005S' ) ;
1208+ equal ( `${ d2 . round ( 'microseconds' ) } ` , 'P5DT5H5M5.005005S' ) ;
1209+ equal ( `${ d2 . round ( 'nanoseconds' ) } ` , 'P5DT5H5M5.005005005S' ) ;
1210+ } ) ;
1211+ it ( 'relativeTo is required to round calendar units even in durations w/o calendar units (string param)' , ( ) => {
1212+ throws ( ( ) => d2 . round ( 'years' ) , RangeError ) ;
1213+ throws ( ( ) => d2 . round ( 'months' ) , RangeError ) ;
1214+ throws ( ( ) => d2 . round ( 'weeks' ) , RangeError ) ;
1215+ } ) ;
1216+ it ( 'relativeTo not required to round non-calendar units in durations w/o calendar units (object param)' , ( ) => {
11801217 equal ( `${ d2 . round ( { smallestUnit : 'days' } ) } ` , 'P5D' ) ;
11811218 equal ( `${ d2 . round ( { smallestUnit : 'hours' } ) } ` , 'P5DT5H' ) ;
11821219 equal ( `${ d2 . round ( { smallestUnit : 'minutes' } ) } ` , 'P5DT5H5M' ) ;
@@ -1185,7 +1222,7 @@ describe('Duration', () => {
11851222 equal ( `${ d2 . round ( { smallestUnit : 'microseconds' } ) } ` , 'P5DT5H5M5.005005S' ) ;
11861223 equal ( `${ d2 . round ( { smallestUnit : 'nanoseconds' } ) } ` , 'P5DT5H5M5.005005005S' ) ;
11871224 } ) ;
1188- it ( 'relativeTo is required for rounding calendar units even in durations without calendar units' , ( ) => {
1225+ it ( 'relativeTo is required to round calendar units even in durations w/o calendar units (object param) ' , ( ) => {
11891226 throws ( ( ) => d2 . round ( { smallestUnit : 'years' } ) , RangeError ) ;
11901227 throws ( ( ) => d2 . round ( { smallestUnit : 'months' } ) , RangeError ) ;
11911228 throws ( ( ) => d2 . round ( { smallestUnit : 'weeks' } ) , RangeError ) ;
@@ -1378,16 +1415,16 @@ describe('Duration', () => {
13781415 [ 'minutes' , 'seconds' ] . forEach ( ( smallestUnit ) => {
13791416 it ( `valid ${ smallestUnit } increments divide into 60` , ( ) => {
13801417 [ 1 , 2 , 3 , 4 , 5 , 6 , 10 , 12 , 15 , 20 , 30 ] . forEach ( ( roundingIncrement ) => {
1381- const options = { smallestUnit, roundingIncrement, relativeTo } ;
1382- assert ( d . round ( options ) instanceof Temporal . Duration ) ;
1418+ const roundTo = { smallestUnit, roundingIncrement, relativeTo } ;
1419+ assert ( d . round ( roundTo ) instanceof Temporal . Duration ) ;
13831420 } ) ;
13841421 } ) ;
13851422 } ) ;
13861423 [ 'milliseconds' , 'microseconds' , 'nanoseconds' ] . forEach ( ( smallestUnit ) => {
13871424 it ( `valid ${ smallestUnit } increments divide into 1000` , ( ) => {
13881425 [ 1 , 2 , 4 , 5 , 8 , 10 , 20 , 25 , 40 , 50 , 100 , 125 , 200 , 250 , 500 ] . forEach ( ( roundingIncrement ) => {
1389- const options = { smallestUnit, roundingIncrement, relativeTo } ;
1390- assert ( d . round ( options ) instanceof Temporal . Duration ) ;
1426+ const roundTo = { smallestUnit, roundingIncrement, relativeTo } ;
1427+ assert ( d . round ( roundTo ) instanceof Temporal . Duration ) ;
13911428 } ) ;
13921429 } ) ;
13931430 } ) ;
@@ -1468,14 +1505,28 @@ describe('Duration', () => {
14681505 const d = new Duration ( 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 ) ;
14691506 const d2 = new Duration ( 0 , 0 , 0 , 5 , 5 , 5 , 5 , 5 , 5 , 5 ) ;
14701507 const relativeTo = Temporal . PlainDateTime . from ( '2020-01-01T00:00' ) ;
1471- it ( 'options may only be an object' , ( ) => {
1472- [ null , 1 , 'hello' , true , Symbol ( 'foo' ) , 1n ] . forEach ( ( badOptions ) => throws ( ( ) => d . total ( badOptions ) , TypeError ) ) ;
1473- } ) ;
1474- it ( 'throws on disallowed or invalid smallestUnit' , ( ) => {
1508+ it ( 'parameter may only be an object or string' , ( ) => {
1509+ [ null , 1 , true , Symbol ( 'foo' ) , 1n ] . forEach ( ( badOptions ) => throws ( ( ) => d . total ( badOptions ) , TypeError ) ) ;
1510+ } ) ;
1511+ it ( 'accepts string parameter as shortcut for {unit}' , ( ) => {
1512+ equal ( d2 . total ( { unit : 'days' } ) . toString ( ) , d2 . total ( 'days' ) . toString ( ) ) ;
1513+ equal ( d2 . total ( { unit : 'hours' } ) . toString ( ) , d2 . total ( 'hours' ) . toString ( ) ) ;
1514+ equal ( d2 . total ( { unit : 'minutes' } ) . toString ( ) , d2 . total ( 'minutes' ) . toString ( ) ) ;
1515+ equal ( d2 . total ( { unit : 'seconds' } ) . toString ( ) , d2 . total ( 'seconds' ) . toString ( ) ) ;
1516+ equal ( d2 . total ( { unit : 'milliseconds' } ) . toString ( ) , d2 . total ( 'milliseconds' ) . toString ( ) ) ;
1517+ equal ( d2 . total ( { unit : 'microseconds' } ) . toString ( ) , d2 . total ( 'microseconds' ) . toString ( ) ) ;
1518+ equal ( d2 . total ( { unit : 'nanoseconds' } ) . toString ( ) , d2 . total ( 'nanoseconds' ) . toString ( ) ) ;
1519+ } ) ;
1520+ it ( 'throws on disallowed or invalid unit (object param)' , ( ) => {
14751521 [ 'era' , 'nonsense' ] . forEach ( ( unit ) => {
14761522 throws ( ( ) => d . total ( { unit } ) , RangeError ) ;
14771523 } ) ;
14781524 } ) ;
1525+ it ( 'throws on disallowed or invalid unit (string param)' , ( ) => {
1526+ [ 'era' , 'nonsense' ] . forEach ( ( unit ) => {
1527+ throws ( ( ) => d . total ( unit ) , RangeError ) ;
1528+ } ) ;
1529+ } ) ;
14791530 it ( 'does not lose precision for seconds and smaller units' , ( ) => {
14801531 const s = Temporal . Duration . from ( { milliseconds : 2 , microseconds : 31 } ) . total ( { unit : 'seconds' } ) ;
14811532 equal ( s , 0.002031 ) ;
@@ -1533,14 +1584,19 @@ describe('Duration', () => {
15331584 equal ( oneMonth . total ( { unit : 'months' , relativeTo : { year : 2020 , month : 1 , day : 1 , months : 2 } } ) , 1 ) ;
15341585 } ) ;
15351586 it ( 'throws RangeError if unit property is missing' , ( ) => {
1536- [ { } , ( ) => { } , { roundingMode : 'ceil' } ] . forEach ( ( options ) => throws ( ( ) => d . total ( options ) , RangeError ) ) ;
1587+ [ { } , ( ) => { } , { roundingMode : 'ceil' } ] . forEach ( ( roundTo ) => throws ( ( ) => d . total ( roundTo ) , RangeError ) ) ;
15371588 } ) ;
1538- it ( 'relativeTo is required for rounding calendar units even in durations without calendar units' , ( ) => {
1589+ it ( 'relativeTo required to round calendar units even in durations w/o calendar units (object param) ' , ( ) => {
15391590 throws ( ( ) => d2 . total ( { unit : 'years' } ) , RangeError ) ;
15401591 throws ( ( ) => d2 . total ( { unit : 'months' } ) , RangeError ) ;
15411592 throws ( ( ) => d2 . total ( { unit : 'weeks' } ) , RangeError ) ;
15421593 } ) ;
1543- it ( 'relativeTo is required for rounding durations with calendar units' , ( ) => {
1594+ it ( 'relativeTo required to round calendar units even in durations w/o calendar units (string param)' , ( ) => {
1595+ throws ( ( ) => d2 . total ( 'years' ) , RangeError ) ;
1596+ throws ( ( ) => d2 . total ( 'months' ) , RangeError ) ;
1597+ throws ( ( ) => d2 . total ( 'weeks' ) , RangeError ) ;
1598+ } ) ;
1599+ it ( 'relativeTo is required to round durations with calendar units (object param)' , ( ) => {
15441600 throws ( ( ) => d . total ( { unit : 'years' } ) , RangeError ) ;
15451601 throws ( ( ) => d . total ( { unit : 'months' } ) , RangeError ) ;
15461602 throws ( ( ) => d . total ( { unit : 'weeks' } ) , RangeError ) ;
@@ -1552,6 +1608,18 @@ describe('Duration', () => {
15521608 throws ( ( ) => d . total ( { unit : 'microseconds' } ) , RangeError ) ;
15531609 throws ( ( ) => d . total ( { unit : 'nanoseconds' } ) , RangeError ) ;
15541610 } ) ;
1611+ it ( 'relativeTo is required to round durations with calendar units (string param)' , ( ) => {
1612+ throws ( ( ) => d . total ( 'years' ) , RangeError ) ;
1613+ throws ( ( ) => d . total ( 'months' ) , RangeError ) ;
1614+ throws ( ( ) => d . total ( 'weeks' ) , RangeError ) ;
1615+ throws ( ( ) => d . total ( 'days' ) , RangeError ) ;
1616+ throws ( ( ) => d . total ( 'hours' ) , RangeError ) ;
1617+ throws ( ( ) => d . total ( 'minutes' ) , RangeError ) ;
1618+ throws ( ( ) => d . total ( 'seconds' ) , RangeError ) ;
1619+ throws ( ( ) => d . total ( 'milliseconds' ) , RangeError ) ;
1620+ throws ( ( ) => d . total ( 'microseconds' ) , RangeError ) ;
1621+ throws ( ( ) => d . total ( 'nanoseconds' ) , RangeError ) ;
1622+ } ) ;
15551623 const d2Nanoseconds =
15561624 d2 . days * 24 * 3.6e12 +
15571625 d2 . hours * 3.6e12 +
0 commit comments