@@ -283,13 +283,84 @@ test("Duration#toFormat returns a lame string for invalid durations", () => {
283
283
expect ( Duration . invalid ( "because" ) . toFormat ( "yy" ) ) . toBe ( "Invalid Duration" ) ;
284
284
} ) ;
285
285
286
- test ( "Duration#toFormat shows negative sign on the largest unit" , ( ) => {
287
- expect ( Duration . fromObject ( { years : - 3 , seconds : - 45 } ) . toFormat ( "yyss" ) ) . toBe ( "-0345" ) ;
286
+ // - signMode negativeLargestOnly
287
+
288
+ test ( "Duration#toFormat shows negative sign on the largest unit when using signMode negativeLargestOnly" , ( ) => {
289
+ expect (
290
+ Duration . fromObject ( { years : - 3 , seconds : - 45 } ) . toFormat ( "yyss" , {
291
+ signMode : "negativeLargestOnly" ,
292
+ } )
293
+ ) . toBe ( "-0345" ) ;
288
294
expect (
289
- Duration . fromObject ( { years : - 3 , seconds : - 45 } ) . toFormat ( "'before'yy'between'ss'after'" )
295
+ Duration . fromObject ( { years : - 3 , seconds : - 45 } ) . toFormat ( "'before'yy'between'ss'after'" , {
296
+ signMode : "negativeLargestOnly" ,
297
+ } )
290
298
) . toBe ( "before-03between45after" ) ;
291
299
// Intentionally have the seconds not first to make sure years is still picked as the largest unit
292
- expect ( Duration . fromObject ( { seconds : - 45 , years : - 3 } ) . toFormat ( "ssyy" ) ) . toBe ( "45-03" ) ;
300
+ expect (
301
+ Duration . fromObject ( { seconds : - 45 , years : - 3 } ) . toFormat ( "ssyy" , {
302
+ signMode : "negativeLargestOnly" ,
303
+ } )
304
+ ) . toBe ( "45-03" ) ;
305
+ } ) ;
306
+
307
+ test ( "Duration#toFormat shows no negative sign on the largest unit when using signMode negativeLargestOnly with positive Duration" , ( ) => {
308
+ expect (
309
+ Duration . fromObject ( { years : 3 , seconds : 45 } ) . toFormat ( "yyss" , {
310
+ signMode : "negativeLargestOnly" ,
311
+ } )
312
+ ) . toBe ( "0345" ) ;
313
+ expect (
314
+ Duration . fromObject ( { years : 3 , seconds : 45 } ) . toFormat ( "'before'yy'between'ss'after'" , {
315
+ signMode : "negativeLargestOnly" ,
316
+ } )
317
+ ) . toBe ( "before03between45after" ) ;
318
+ // Intentionally have the seconds not first to make sure years is still picked as the largest unit
319
+ expect (
320
+ Duration . fromObject ( { years : 3 , seconds : 45 } ) . toFormat ( "ssyy" , {
321
+ signMode : "negativeLargestOnly" ,
322
+ } )
323
+ ) . toBe ( "4503" ) ;
324
+ } ) ;
325
+
326
+ // - signMode all
327
+
328
+ test ( "Duration#toFormat with signMode all shows positive sign on positive durations" , ( ) => {
329
+ expect (
330
+ Duration . fromObject ( { years : 3 , seconds : 45 } ) . toFormat ( "yyss" , {
331
+ signMode : "all" ,
332
+ } )
333
+ ) . toBe ( "+03+45" ) ;
334
+ expect (
335
+ Duration . fromObject ( { years : 3 , seconds : 45 } ) . toFormat ( "'before'yy'between'ss'after'" , {
336
+ signMode : "all" ,
337
+ } )
338
+ ) . toBe ( "before+03between+45after" ) ;
339
+ // Intentionally have the seconds not first to make sure years is still picked as the largest unit
340
+ expect (
341
+ Duration . fromObject ( { years : 3 , seconds : 45 } ) . toFormat ( "ssyy" , {
342
+ signMode : "all" ,
343
+ } )
344
+ ) . toBe ( "+45+03" ) ;
345
+ } ) ;
346
+
347
+ test ( "Duration#toFormat with signMode all shows positive sign on negative durations" , ( ) => {
348
+ expect (
349
+ Duration . fromObject ( { years : - 3 , seconds : - 45 } ) . toFormat ( "yyss" , {
350
+ signMode : "all" ,
351
+ } )
352
+ ) . toBe ( "-03-45" ) ;
353
+ expect (
354
+ Duration . fromObject ( { years : - 3 , seconds : - 45 } ) . toFormat ( "'before'yy'between'ss'after'" , {
355
+ signMode : "all" ,
356
+ } )
357
+ ) . toBe ( "before-03between-45after" ) ;
358
+ // Intentionally have the seconds not first to make sure years is still picked as the largest unit
359
+ expect (
360
+ Duration . fromObject ( { years : - 3 , seconds : - 45 } ) . toFormat ( "ssyy" , {
361
+ signMode : "all" ,
362
+ } )
363
+ ) . toBe ( "-45-03" ) ;
293
364
} ) ;
294
365
295
366
//------
0 commit comments