@sproutsocial/seeds-react-duration 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,548 @@
1
+ import testDuration from "./testDuration";
2
+
3
+ // this value should NOT round up the last digit when applicable (1m 29s = 1)
4
+ const duration = 127769499; // 1d 11h 29m 29s 499ms
5
+
6
+ // this value SHOULD round up the last digit when applicable (1m 30s = 2m)
7
+ const roundingDuration = 131430500; // 1d 12h 30m 30s 500ms
8
+
9
+ describe("When using display units...", () => {
10
+ describe("... it renders properly", () => {
11
+ testDuration(
12
+ duration,
13
+ {
14
+ displayUnits: {
15
+ days: true,
16
+ hours: true,
17
+ minutes: true,
18
+ seconds: true,
19
+ milliseconds: true,
20
+ },
21
+ },
22
+ { text: "1d 11h 29m 29s 499ms" }
23
+ );
24
+ testDuration(
25
+ duration,
26
+ {
27
+ displayUnits: {
28
+ hours: true,
29
+ minutes: true,
30
+ seconds: true,
31
+ milliseconds: true,
32
+ },
33
+ },
34
+ { text: "35h 29m 29s 499ms" }
35
+ );
36
+ testDuration(
37
+ duration,
38
+ {
39
+ displayUnits: {
40
+ days: true,
41
+ minutes: true,
42
+ seconds: true,
43
+ milliseconds: true,
44
+ },
45
+ },
46
+ { text: "1d 689m 29s 499ms" }
47
+ );
48
+ testDuration(
49
+ duration,
50
+ {
51
+ displayUnits: {
52
+ days: true,
53
+ hours: true,
54
+ seconds: true,
55
+ milliseconds: true,
56
+ },
57
+ },
58
+ { text: "1d 11h 1,769s 499ms" }
59
+ );
60
+ testDuration(
61
+ duration,
62
+ {
63
+ displayUnits: {
64
+ days: true,
65
+ hours: true,
66
+ minutes: true,
67
+ milliseconds: true,
68
+ },
69
+ },
70
+ { text: "1d 11h 29m 29,499ms" }
71
+ );
72
+ testDuration(
73
+ duration,
74
+ {
75
+ displayUnits: {
76
+ days: true,
77
+ hours: true,
78
+ minutes: true,
79
+ seconds: true,
80
+ },
81
+ },
82
+ { text: "1d 11h 29m 29s" }
83
+ );
84
+ testDuration(
85
+ duration,
86
+ {
87
+ displayUnits: { minutes: true, seconds: true, milliseconds: true },
88
+ },
89
+ { text: "2,129m 29s 499ms" }
90
+ );
91
+ testDuration(
92
+ duration,
93
+ {
94
+ displayUnits: { hours: true, seconds: true, milliseconds: true },
95
+ },
96
+ { text: "35h 1,769s 499ms" }
97
+ );
98
+ testDuration(
99
+ duration,
100
+ {
101
+ displayUnits: { hours: true, minutes: true, milliseconds: true },
102
+ },
103
+ { text: "35h 29m 29,499ms" }
104
+ );
105
+ testDuration(
106
+ duration,
107
+ {
108
+ displayUnits: { hours: true, minutes: true, seconds: true },
109
+ },
110
+ { text: "35h 29m 29s" }
111
+ );
112
+ testDuration(
113
+ duration,
114
+ {
115
+ displayUnits: { days: true, seconds: true, milliseconds: true },
116
+ },
117
+ { text: "1d 41,369s 499ms" }
118
+ );
119
+ testDuration(
120
+ duration,
121
+ {
122
+ displayUnits: { days: true, minutes: true, milliseconds: true },
123
+ },
124
+ { text: "1d 689m 29,499ms" }
125
+ );
126
+ testDuration(
127
+ duration,
128
+ {
129
+ displayUnits: { days: true, minutes: true, seconds: true },
130
+ },
131
+ { text: "1d 689m 29s" }
132
+ );
133
+ testDuration(
134
+ duration,
135
+ {
136
+ displayUnits: { days: true, hours: true, milliseconds: true },
137
+ },
138
+ { text: "1d 11h 1,769,499ms" }
139
+ );
140
+ testDuration(
141
+ duration,
142
+ {
143
+ displayUnits: { days: true, hours: true, seconds: true },
144
+ },
145
+ { text: "1d 11h 1,769s" }
146
+ );
147
+ testDuration(
148
+ duration,
149
+ {
150
+ displayUnits: { days: true, hours: true, minutes: true },
151
+ },
152
+ { text: "1d 11h 29m" }
153
+ );
154
+ testDuration(
155
+ duration,
156
+ {
157
+ displayUnits: {
158
+ seconds: true,
159
+ milliseconds: true,
160
+ },
161
+ },
162
+ { text: "127,769s 499ms" }
163
+ );
164
+ testDuration(
165
+ duration,
166
+ {
167
+ displayUnits: {
168
+ minutes: true,
169
+ milliseconds: true,
170
+ },
171
+ },
172
+ { text: "2,129m 29,499ms" }
173
+ );
174
+ testDuration(
175
+ duration,
176
+ {
177
+ displayUnits: {
178
+ minutes: true,
179
+ seconds: true,
180
+ },
181
+ },
182
+ { text: "2,129m 29s" }
183
+ );
184
+ testDuration(
185
+ duration,
186
+ {
187
+ displayUnits: {
188
+ hours: true,
189
+ milliseconds: true,
190
+ },
191
+ },
192
+ { text: "35h 1,769,499ms" }
193
+ );
194
+ testDuration(
195
+ duration,
196
+ {
197
+ displayUnits: {
198
+ hours: true,
199
+ seconds: true,
200
+ },
201
+ },
202
+ { text: "35h 1,769s" }
203
+ );
204
+ testDuration(
205
+ duration,
206
+ {
207
+ displayUnits: {
208
+ hours: true,
209
+ minutes: true,
210
+ },
211
+ },
212
+ { text: "35h 29m" }
213
+ );
214
+ testDuration(
215
+ duration,
216
+ {
217
+ displayUnits: {
218
+ days: true,
219
+ milliseconds: true,
220
+ },
221
+ },
222
+ { text: "1d 41,369,499ms" }
223
+ );
224
+ testDuration(
225
+ duration,
226
+ {
227
+ displayUnits: {
228
+ days: true,
229
+ seconds: true,
230
+ },
231
+ },
232
+ { text: "1d 41,369s" }
233
+ );
234
+ testDuration(
235
+ duration,
236
+ {
237
+ displayUnits: {
238
+ days: true,
239
+ minutes: true,
240
+ },
241
+ },
242
+ { text: "1d 689m" }
243
+ );
244
+ testDuration(
245
+ duration,
246
+ {
247
+ displayUnits: {
248
+ days: true,
249
+ hours: true,
250
+ },
251
+ },
252
+ { text: "1d 11h" }
253
+ );
254
+ testDuration(duration, { displayUnits: { days: true } }, { text: "1d" });
255
+ testDuration(duration, { displayUnits: { hours: true } }, { text: "35h" });
256
+ testDuration(
257
+ duration,
258
+ { displayUnits: { minutes: true } },
259
+ { text: "2,129m" }
260
+ );
261
+ testDuration(
262
+ duration,
263
+ { displayUnits: { seconds: true } },
264
+ { text: "127,769s" }
265
+ );
266
+ testDuration(
267
+ duration,
268
+ { displayUnits: { milliseconds: true } },
269
+ { text: "127,769,499ms" }
270
+ );
271
+ testDuration(duration, { displayUnits: {} }, { text: "1d 11h 29m 29s" });
272
+ });
273
+ describe("... it renders properly while rounding up the last digit", () => {
274
+ testDuration(
275
+ roundingDuration,
276
+ {
277
+ displayUnits: {
278
+ days: true,
279
+ hours: true,
280
+ minutes: true,
281
+ seconds: true,
282
+ milliseconds: true,
283
+ },
284
+ },
285
+ { text: "1d 12h 30m 30s 500ms" }
286
+ );
287
+ testDuration(
288
+ roundingDuration,
289
+ {
290
+ displayUnits: {
291
+ hours: true,
292
+ minutes: true,
293
+ seconds: true,
294
+ milliseconds: true,
295
+ },
296
+ },
297
+ { text: "36h 30m 30s 500ms" }
298
+ );
299
+ testDuration(
300
+ roundingDuration,
301
+ {
302
+ displayUnits: {
303
+ days: true,
304
+ minutes: true,
305
+ seconds: true,
306
+ milliseconds: true,
307
+ },
308
+ },
309
+ { text: "1d 750m 30s 500ms" }
310
+ );
311
+ testDuration(
312
+ roundingDuration,
313
+ {
314
+ displayUnits: {
315
+ days: true,
316
+ hours: true,
317
+ seconds: true,
318
+ milliseconds: true,
319
+ },
320
+ },
321
+ { text: "1d 12h 1,830s 500ms" }
322
+ );
323
+ testDuration(
324
+ roundingDuration,
325
+ {
326
+ displayUnits: {
327
+ days: true,
328
+ hours: true,
329
+ minutes: true,
330
+ milliseconds: true,
331
+ },
332
+ },
333
+ { text: "1d 12h 30m 30,500ms" }
334
+ );
335
+ testDuration(
336
+ roundingDuration,
337
+ {
338
+ displayUnits: {
339
+ days: true,
340
+ hours: true,
341
+ minutes: true,
342
+ seconds: true,
343
+ },
344
+ },
345
+ { text: "1d 12h 30m 31s" }
346
+ );
347
+ testDuration(
348
+ roundingDuration,
349
+ {
350
+ displayUnits: { minutes: true, seconds: true, milliseconds: true },
351
+ },
352
+ { text: "2,190m 30s 500ms" }
353
+ );
354
+ testDuration(
355
+ roundingDuration,
356
+ {
357
+ displayUnits: { hours: true, seconds: true, milliseconds: true },
358
+ },
359
+ { text: "36h 1,830s 500ms" }
360
+ );
361
+ testDuration(
362
+ roundingDuration,
363
+ {
364
+ displayUnits: { hours: true, minutes: true, milliseconds: true },
365
+ },
366
+ { text: "36h 30m 30,500ms" }
367
+ );
368
+ testDuration(
369
+ roundingDuration,
370
+ {
371
+ displayUnits: { hours: true, minutes: true, seconds: true },
372
+ },
373
+ { text: "36h 30m 31s" }
374
+ );
375
+ testDuration(
376
+ roundingDuration,
377
+ {
378
+ displayUnits: { days: true, seconds: true, milliseconds: true },
379
+ },
380
+ { text: "1d 45,030s 500ms" }
381
+ );
382
+ testDuration(
383
+ roundingDuration,
384
+ {
385
+ displayUnits: { days: true, minutes: true, milliseconds: true },
386
+ },
387
+ { text: "1d 750m 30,500ms" }
388
+ );
389
+ testDuration(
390
+ roundingDuration,
391
+ {
392
+ displayUnits: { days: true, minutes: true, seconds: true },
393
+ },
394
+ { text: "1d 750m 31s" }
395
+ );
396
+ testDuration(
397
+ roundingDuration,
398
+ {
399
+ displayUnits: { days: true, hours: true, milliseconds: true },
400
+ },
401
+ { text: "1d 12h 1,830,500ms" }
402
+ );
403
+ testDuration(
404
+ roundingDuration,
405
+ {
406
+ displayUnits: { days: true, hours: true, seconds: true },
407
+ },
408
+ { text: "1d 12h 1,831s" }
409
+ );
410
+ testDuration(
411
+ roundingDuration,
412
+ {
413
+ displayUnits: { days: true, hours: true, minutes: true },
414
+ },
415
+ { text: "1d 12h 31m" }
416
+ );
417
+ testDuration(
418
+ roundingDuration,
419
+ {
420
+ displayUnits: {
421
+ seconds: true,
422
+ milliseconds: true,
423
+ },
424
+ },
425
+ { text: "131,430s 500ms" }
426
+ );
427
+ testDuration(
428
+ roundingDuration,
429
+ {
430
+ displayUnits: {
431
+ minutes: true,
432
+ milliseconds: true,
433
+ },
434
+ },
435
+ { text: "2,190m 30,500ms" }
436
+ );
437
+ testDuration(
438
+ roundingDuration,
439
+ {
440
+ displayUnits: {
441
+ minutes: true,
442
+ seconds: true,
443
+ },
444
+ },
445
+ { text: "2,190m 31s" }
446
+ );
447
+ testDuration(
448
+ roundingDuration,
449
+ {
450
+ displayUnits: {
451
+ hours: true,
452
+ milliseconds: true,
453
+ },
454
+ },
455
+ { text: "36h 1,830,500ms" }
456
+ );
457
+ testDuration(
458
+ roundingDuration,
459
+ {
460
+ displayUnits: {
461
+ hours: true,
462
+ seconds: true,
463
+ },
464
+ },
465
+ { text: "36h 1,831s" }
466
+ );
467
+ testDuration(
468
+ roundingDuration,
469
+ {
470
+ displayUnits: {
471
+ hours: true,
472
+ minutes: true,
473
+ },
474
+ },
475
+ { text: "36h 31m" }
476
+ );
477
+ testDuration(
478
+ roundingDuration,
479
+ {
480
+ displayUnits: {
481
+ days: true,
482
+ milliseconds: true,
483
+ },
484
+ },
485
+ { text: "1d 45,030,500ms" }
486
+ );
487
+ testDuration(
488
+ roundingDuration,
489
+ {
490
+ displayUnits: {
491
+ days: true,
492
+ seconds: true,
493
+ },
494
+ },
495
+ { text: "1d 45,031s" }
496
+ );
497
+ testDuration(
498
+ roundingDuration,
499
+ {
500
+ displayUnits: {
501
+ days: true,
502
+ minutes: true,
503
+ },
504
+ },
505
+ { text: "1d 751m" }
506
+ );
507
+ testDuration(
508
+ roundingDuration,
509
+ {
510
+ displayUnits: {
511
+ days: true,
512
+ hours: true,
513
+ },
514
+ },
515
+ { text: "1d 13h" }
516
+ );
517
+ testDuration(
518
+ roundingDuration,
519
+ { displayUnits: { days: true } },
520
+ { text: "2d" }
521
+ );
522
+ testDuration(
523
+ roundingDuration,
524
+ { displayUnits: { hours: true } },
525
+ { text: "37h" }
526
+ );
527
+ testDuration(
528
+ roundingDuration,
529
+ { displayUnits: { minutes: true } },
530
+ { text: "2,191m" }
531
+ );
532
+ testDuration(
533
+ roundingDuration,
534
+ { displayUnits: { seconds: true } },
535
+ { text: "131,431s" }
536
+ );
537
+ testDuration(
538
+ roundingDuration,
539
+ { displayUnits: { milliseconds: true } },
540
+ { text: "131,430,500ms" }
541
+ );
542
+ testDuration(
543
+ roundingDuration,
544
+ { displayUnits: {} },
545
+ { text: "1d 12h 30m 31s" }
546
+ );
547
+ });
548
+ });
@@ -0,0 +1,32 @@
1
+ import { EM_DASH } from "../../constants";
2
+ import testDuration from "./testDuration";
3
+
4
+ describe("When using an invalid value...", () => {
5
+ describe("... it renders a dash", () => {
6
+ testDuration(
7
+ undefined,
8
+ { invalidMillisecondsLabel: "Not available" },
9
+ { text: EM_DASH, label: "Not available" }
10
+ );
11
+ testDuration(
12
+ null,
13
+ { invalidMillisecondsLabel: "Not available" },
14
+ { text: EM_DASH, label: "Not available" }
15
+ );
16
+ testDuration(
17
+ NaN,
18
+ { invalidMillisecondsLabel: "Not available" },
19
+ { text: EM_DASH, label: "Not available" }
20
+ );
21
+ testDuration(
22
+ Infinity,
23
+ { invalidMillisecondsLabel: "Not available" },
24
+ { text: EM_DASH, label: "Not available" }
25
+ );
26
+ testDuration(
27
+ -Infinity,
28
+ { invalidMillisecondsLabel: "Not available" },
29
+ { text: EM_DASH, label: "Not available" }
30
+ );
31
+ });
32
+ });
@@ -0,0 +1,87 @@
1
+ import testDuration from "./testDuration";
2
+
3
+ describe("When using a locale...", () => {
4
+ describe("... it should use the proper punctuation and spacing", () => {
5
+ testDuration(90061001, { locale: "en-US" }, { text: "1d 1h 1m 1s" });
6
+ testDuration(180122002, { locale: "en-US" }, { text: "2d 2h 2m 2s" });
7
+ testDuration(90061001, { locale: "es-LA" }, { text: "1d 1h 1min 1s" });
8
+ testDuration(180122002, { locale: "es-LA" }, { text: "2d 2h 2min 2s" });
9
+ testDuration(90061001, { locale: "fr-FR" }, { text: "1j 1h 1min 1s" });
10
+ testDuration(180122002, { locale: "fr-FR" }, { text: "2j 2h 2min 2s" });
11
+ testDuration(90061001, { locale: "it-IT" }, { text: "1g 1h 1min 1s" });
12
+ testDuration(180122002, { locale: "it-IT" }, { text: "2gg 2h 2min 2s" });
13
+ testDuration(
14
+ 90061001,
15
+ { locale: "pt-BR" },
16
+ { text: "1 dia 1 h 1 min 1 s" }
17
+ );
18
+ testDuration(
19
+ 180122002,
20
+ { locale: "pt-BR" },
21
+ { text: "2 dias 2 h 2 min 2 s" }
22
+ );
23
+ });
24
+
25
+ describe("... it should work with display", () => {
26
+ const options = { display: "long" };
27
+
28
+ testDuration(
29
+ 90061001,
30
+ { ...options, locale: "en-US" },
31
+ { text: "1 day 1 hour 1 minute 1 second" }
32
+ );
33
+ testDuration(
34
+ 180122002,
35
+ { ...options, locale: "en-US" },
36
+ { text: "2 days 2 hours 2 minutes 2 seconds" }
37
+ );
38
+ testDuration(
39
+ 90061001,
40
+ { ...options, locale: "es-LA" },
41
+ { text: "1 día 1 hora 1 minuto 1 segundo" }
42
+ );
43
+ testDuration(
44
+ 180122002,
45
+ { ...options, locale: "es-LA" },
46
+ { text: "2 días 2 horas 2 minutos 2 segundos" }
47
+ );
48
+ testDuration(
49
+ 90061001,
50
+ { ...options, locale: "fr-FR" },
51
+ {
52
+ text: "1 jour 1 heure 1 minute 1 seconde",
53
+ // see this article as to why the \xa0 is needed: https://stackoverflow.com/a/66409196
54
+ format: "1\xa0jour 1\xa0heure 1 minute 1\xa0seconde",
55
+ }
56
+ );
57
+ testDuration(
58
+ 180122002,
59
+ { ...options, locale: "fr-FR" },
60
+ {
61
+ text: "2 jours 2 heures 2 minutes 2 secondes",
62
+ // see this article as to why the \xa0 is needed: https://stackoverflow.com/a/66409196
63
+ format: "2\xa0jours 2\xa0heures 2 minutes 2\xa0secondes",
64
+ }
65
+ );
66
+ testDuration(
67
+ 90061001,
68
+ { ...options, locale: "it-IT" },
69
+ { text: "1 giorno 1 ora 1 minuto 1 secondo" }
70
+ );
71
+ testDuration(
72
+ 180122002,
73
+ { ...options, locale: "it-IT" },
74
+ { text: "2 giorni 2 ore 2 minuti 2 secondi" }
75
+ );
76
+ testDuration(
77
+ 90061001,
78
+ { ...options, locale: "pt-BR" },
79
+ { text: "1 dia 1 hora 1 minuto 1 segundo" }
80
+ );
81
+ testDuration(
82
+ 180122002,
83
+ { ...options, locale: "pt-BR" },
84
+ { text: "2 dias 2 horas 2 minutos 2 segundos" }
85
+ );
86
+ });
87
+ });