@quenty/time 2.5.0 → 2.6.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,362 @@
1
+ --!strict
2
+ --[[
3
+ @class TimeDurationUtils.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Jest = require("Jest")
9
+ local RelativeTimeUtils = require("RelativeTimeUtils")
10
+ local TimeDurationUtils = require("TimeDurationUtils")
11
+
12
+ local describe = Jest.Globals.describe
13
+ local expect = Jest.Globals.expect
14
+ local it = Jest.Globals.it
15
+
16
+ local SECOND = 1
17
+ local MINUTE = 60 * SECOND
18
+ local HOUR = 60 * MINUTE
19
+ local DAY = 24 * HOUR
20
+
21
+ describe("TimeDurationUtils.toSeconds", function()
22
+ it("should treat a bare number as seconds", function()
23
+ expect(TimeDurationUtils.toSeconds(1.5)).toBe(1.5)
24
+ expect(TimeDurationUtils.toMilliseconds(1.5)).toBe(1500)
25
+ expect(TimeDurationUtils.toMilliseconds(5, "minutes")).toBe(300000)
26
+ end)
27
+
28
+ it("should scale a number by a unit", function()
29
+ expect(TimeDurationUtils.toSeconds(5, "minutes")).toBe(5 * MINUTE)
30
+ expect(TimeDurationUtils.toSeconds(2, "h")).toBe(2 * HOUR)
31
+ expect(TimeDurationUtils.toSeconds(1, "week")).toBe(7 * DAY)
32
+ expect(TimeDurationUtils.toSeconds(1, "month")).toBe(30 * DAY)
33
+ expect(TimeDurationUtils.toSeconds(1, "year")).toBe(365 * DAY)
34
+ expect(TimeDurationUtils.toSeconds(1, "quarter")).toBe(90 * DAY)
35
+ end)
36
+
37
+ it("should sum a duration table", function()
38
+ expect(TimeDurationUtils.toSeconds({ hours = 1, minutes = 30 })).toBe(90 * MINUTE)
39
+ expect(TimeDurationUtils.toSeconds({ weeks = 1, days = 1 })).toBe(8 * DAY)
40
+ expect(TimeDurationUtils.toSeconds({ second = 2, millisecond = 5 })).toBe(2.005)
41
+ expect(TimeDurationUtils.toSeconds({})).toBe(0)
42
+ end)
43
+
44
+ it("should parse ISO 8601 durations", function()
45
+ expect(TimeDurationUtils.toSeconds("PT1H30M")).toBe(90 * MINUTE)
46
+ expect(TimeDurationUtils.toSeconds("P1DT12H")).toBe(36 * HOUR)
47
+ expect(TimeDurationUtils.toSeconds("P1W")).toBe(7 * DAY)
48
+ expect(TimeDurationUtils.toSeconds("P1Y2M3DT4H5M6S")).toBe(
49
+ 365 * DAY + 60 * DAY + 3 * DAY + 4 * HOUR + 5 * MINUTE + 6 * SECOND
50
+ )
51
+ expect(TimeDurationUtils.toSeconds("PT1.5S")).toBe(1.5)
52
+ expect(TimeDurationUtils.toSeconds("PT0,5S")).toBe(0.5)
53
+ expect(TimeDurationUtils.toSeconds("-PT5S")).toBe(-5 * SECOND)
54
+ expect(TimeDurationUtils.toSeconds("P0D")).toBe(0)
55
+ end)
56
+
57
+ it("should reject malformed input", function()
58
+ expect(function()
59
+ TimeDurationUtils.toSeconds("1H")
60
+ end).toThrow("Bad ISO 8601 duration")
61
+ expect(function()
62
+ TimeDurationUtils.toSeconds("PT1X")
63
+ end).toThrow("Bad ISO 8601 duration")
64
+ expect(function()
65
+ TimeDurationUtils.toSeconds({ fortnights = 1 } :: any)
66
+ end).toThrow("Bad unit")
67
+ expect(function()
68
+ TimeDurationUtils.toSeconds("PT1H", "minutes")
69
+ end).toThrow("cannot be given")
70
+ end)
71
+ end)
72
+
73
+ describe("TimeDurationUtils.as", function()
74
+ it("should convert to a fractional amount of a unit", function()
75
+ expect(TimeDurationUtils.as(90 * SECOND, "minutes")).toBe(1.5)
76
+ expect(TimeDurationUtils.as({ days = 1 }, "hours")).toBe(24)
77
+ expect(TimeDurationUtils.as({ days = 14 }, "weeks")).toBe(2)
78
+ expect(TimeDurationUtils.as("PT36H", "days")).toBe(1.5)
79
+ expect(TimeDurationUtils.as({ years = 1 }, "days")).toBe(365)
80
+ end)
81
+ end)
82
+
83
+ describe("TimeDurationUtils.toTable / get", function()
84
+ it("should break a duration into whole units", function()
85
+ expect(TimeDurationUtils.toTable({ hours = 1, minutes = 30 })).toEqual({
86
+ years = 0,
87
+ months = 0,
88
+ days = 0,
89
+ hours = 1,
90
+ minutes = 30,
91
+ seconds = 0,
92
+ milliseconds = 0,
93
+ })
94
+ expect(TimeDurationUtils.toTable(400 * DAY + 90 * MINUTE + 1.25)).toEqual({
95
+ years = 1,
96
+ months = 1,
97
+ days = 5,
98
+ hours = 1,
99
+ minutes = 30,
100
+ seconds = 1,
101
+ milliseconds = 250,
102
+ })
103
+ end)
104
+
105
+ it("should read whole units", function()
106
+ expect(TimeDurationUtils.get({ hours = 25 }, "hours")).toBe(1)
107
+ expect(TimeDurationUtils.get({ hours = 25 }, "days")).toBe(1)
108
+ expect(TimeDurationUtils.get(90 * SECOND, "seconds")).toBe(30)
109
+ expect(TimeDurationUtils.get(90 * SECOND, "minutes")).toBe(1)
110
+ expect(TimeDurationUtils.get({ days = 15 }, "weeks")).toBe(2)
111
+ expect(TimeDurationUtils.get({ days = 15 }, "d")).toBe(15)
112
+ expect(TimeDurationUtils.get({ months = 7 }, "quarters")).toBe(2)
113
+ expect(TimeDurationUtils.get(1.25, "ms")).toBe(250)
114
+ end)
115
+
116
+ it("should break negative durations into negative parts", function()
117
+ expect(TimeDurationUtils.get(-90 * SECOND, "minutes")).toBe(-1)
118
+ expect(TimeDurationUtils.get(-90 * SECOND, "seconds")).toBe(-30)
119
+ end)
120
+ end)
121
+
122
+ describe("TimeDurationUtils.add / subtract", function()
123
+ it("should combine durations in milliseconds", function()
124
+ expect(TimeDurationUtils.add(MINUTE, { seconds = 30 })).toBe(90 * SECOND)
125
+ expect(TimeDurationUtils.add(MINUTE, 5, "seconds")).toBe(65 * SECOND)
126
+ expect(TimeDurationUtils.subtract({ hours = 1 }, "PT15M")).toBe(45 * MINUTE)
127
+ end)
128
+ end)
129
+
130
+ describe("TimeDurationUtils.format", function()
131
+ it("should let the largest token absorb the overflow", function()
132
+ expect(TimeDurationUtils.format(47 * HOUR, "h:mm:ss")).toBe("47:00:00")
133
+ expect(TimeDurationUtils.format({ days = 45 }, "d __")).toBe("45 days")
134
+ expect(TimeDurationUtils.format(HOUR, "m:ss")).toBe("60:00")
135
+ expect(TimeDurationUtils.format(MINUTE, "s")).toBe("60")
136
+ expect(TimeDurationUtils.format({ minutes = 90 }, "hh:mm:ss")).toBe("01:30:00")
137
+ expect(TimeDurationUtils.format({ days = 10 }, "w __ d __")).toBe("1 week 3 days")
138
+ end)
139
+
140
+ it("should pad repeated letters and leave single ones bare", function()
141
+ expect(TimeDurationUtils.format(HOUR + MINUTE + SECOND, "hh:mm:ss")).toBe("01:01:01")
142
+ expect(TimeDurationUtils.format(HOUR + MINUTE + SECOND, "h:m:s")).toBe("1:1:1")
143
+ expect(TimeDurationUtils.format({ days = 12, months = 3 }, "M/dd")).toBe("3/12")
144
+ expect(TimeDurationUtils.format({ years = 12 }, "yy yyyy")).toBe("12 0012")
145
+ expect(TimeDurationUtils.format(1.25, "s.SSS")).toBe("1.250")
146
+ expect(TimeDurationUtils.format(0.25, "S __")).toBe("250 milliseconds")
147
+ expect(TimeDurationUtils.format(0.001, "S__")).toBe("1 millisecond")
148
+ end)
149
+
150
+ it("should treat bracketed text as literal and leave other characters alone", function()
151
+ expect(TimeDurationUtils.format(HOUR + MINUTE + SECOND, "h [hours] m [minutes] s [seconds]")).toBe(
152
+ "1 hours 1 minutes 1 seconds"
153
+ )
154
+ expect(TimeDurationUtils.format(90 * MINUTE, "mm:ss (hh)")).toBe("30:00 (01)")
155
+ expect(TimeDurationUtils.format(5, "[s] s [s]")).toBe("s 5 s")
156
+ end)
157
+
158
+ it("should drop leading zero tokens by default", function()
159
+ expect(TimeDurationUtils.format(123 * MINUTE, "d __ h:mm:ss")).toBe("2:03:00")
160
+ expect(TimeDurationUtils.format(45, "h:mm:ss")).toBe("45")
161
+ expect(TimeDurationUtils.format(5 * MINUTE, "h:mm:ss")).toBe("5:00")
162
+ expect(TimeDurationUtils.format(0, "h:mm:ss")).toBe("0")
163
+ expect(TimeDurationUtils.format(45, "h:mm:ss", { trim = false })).toBe("0:00:45")
164
+ expect(TimeDurationUtils.format(5 * MINUTE, "h:mm:ss", { forceLength = true })).toBe("05:00")
165
+ end)
166
+
167
+ it("should drop trailing, interior or all zero tokens when asked", function()
168
+ expect(TimeDurationUtils.format({ days = 2 }, "d __ h __ m __", { trim = "both" })).toBe("2 days")
169
+ expect(TimeDurationUtils.format({ hours = 2 }, "d __ h __ m __", { trim = "both" })).toBe("2 hours")
170
+ expect(TimeDurationUtils.format({ hours = 2 }, "d __ h __ m __", { trim = "small" })).toBe("0 days 2 hours")
171
+ expect(TimeDurationUtils.format({ years = 1, days = 3 }, "y __, M __, d __", { trim = "mid" })).toBe(
172
+ "1 year, 3 days"
173
+ )
174
+ expect(TimeDurationUtils.format({ years = 1, days = 3 }, "y __, M __, d __, h __", { trim = "all" })).toBe(
175
+ "1 year, 3 days"
176
+ )
177
+ expect(TimeDurationUtils.format(0, "d __ h __ m __", { trim = "all" })).toBe("0 minutes")
178
+ end)
179
+
180
+ it("should drop the text next to a dropped token", function()
181
+ expect(TimeDurationUtils.format(45, "[in] h:mm:ss")).toBe("in 45")
182
+ expect(TimeDurationUtils.format({ hours = 2 }, "h:mm [left]", { trim = "both" })).toBe("2 left")
183
+ expect(TimeDurationUtils.format({ days = 2 }, "d [days] h:mm:ss", { trim = "both" })).toBe("2")
184
+ end)
185
+
186
+ it("should keep pinned tokens", function()
187
+ expect(TimeDurationUtils.format(45, "h:mm:ss", { stopTrim = "m" })).toBe("0:45")
188
+ expect(TimeDurationUtils.format(45, "h:*mm:ss")).toBe("0:45")
189
+ expect(TimeDurationUtils.format(45, "*h:mm:ss")).toBe("0:00:45")
190
+ end)
191
+
192
+ it("should round the smallest token unless truncating", function()
193
+ expect(TimeDurationUtils.format(59.6, "m:ss")).toBe("1:00")
194
+ expect(TimeDurationUtils.format(59.6, "m:ss", { trunc = true })).toBe("59")
195
+ expect(TimeDurationUtils.format(90, "m")).toBe("2")
196
+ expect(TimeDurationUtils.format(90, "m", { trunc = true })).toBe("1")
197
+ expect(TimeDurationUtils.format(29, "m")).toBe("0")
198
+ expect(TimeDurationUtils.format(0.3, "SSS")).toBe("300")
199
+ end)
200
+
201
+ it("should format centiseconds", function()
202
+ expect(TimeDurationUtils.format(65.432, "mm:ss:CC", { trunc = true })).toBe("01:05:43")
203
+ expect(TimeDurationUtils.format(65.438, "mm:ss:CC")).toBe("01:05:44")
204
+ expect(TimeDurationUtils.format(0.05, "hh:mm:ss:CC", { trim = false })).toBe("00:00:00:05")
205
+ expect(TimeDurationUtils.format(1.234, "s.CC.S", { trunc = true })).toBe("1.23.4")
206
+ expect(TimeDurationUtils.format(2, "CC")).toBe("200")
207
+ expect(function()
208
+ TimeDurationUtils.format(1, "C __")
209
+ end).toThrow()
210
+ end)
211
+
212
+ it("should print decimals on the smallest token with precision", function()
213
+ expect(TimeDurationUtils.format(90, "m", { precision = 1 })).toBe("1.5")
214
+ expect(TimeDurationUtils.format({ hours = 1, minutes = 30 }, "h", { precision = 2 })).toBe("1.50")
215
+ expect(TimeDurationUtils.format(1234, "s", { precision = -2 })).toBe("1200")
216
+ expect(TimeDurationUtils.format(90, "m __", { precision = 1 })).toBe("1.5 minutes")
217
+ expect(TimeDurationUtils.format(3700, "m:ss", { precision = -2 })).toBe("61:40")
218
+ end)
219
+
220
+ it("should keep only the largest tokens when asked", function()
221
+ expect(TimeDurationUtils.format({ days = 1, minutes = 5 }, "d __, h __, m __, s __", { largest = 2 })).toBe(
222
+ "1 day, 5 minutes"
223
+ )
224
+ expect(TimeDurationUtils.format({ days = 1, hours = 2, minutes = 5 }, "d __, h __, m __", { largest = 2 })).toBe(
225
+ "1 day, 2 hours"
226
+ )
227
+ expect(TimeDurationUtils.format({ days = 1, hours = 2 }, "d __, h __, m __", { largest = 1 })).toBe("1 day")
228
+ end)
229
+
230
+ it("should let a token count up to its limit before using the next", function()
231
+ local limits: { [string]: number } = { minutes = 60 }
232
+ expect(TimeDurationUtils.format(HOUR, "h:mm:ss", { limits = limits })).toBe("60:00")
233
+ expect(TimeDurationUtils.format(HOUR + 59, "h:mm:ss", { limits = limits })).toBe("60:59")
234
+ expect(TimeDurationUtils.format(HOUR + MINUTE, "h:mm:ss", { limits = limits })).toBe("1:01:00")
235
+ expect(TimeDurationUtils.format(HOUR, "*h:mm:ss", { limits = limits })).toBe("1:00:00")
236
+ expect(TimeDurationUtils.format(47 * HOUR, "d __ h:mm:ss", { limits = { hours = 47 } })).toBe("47:00:00")
237
+ expect(TimeDurationUtils.format(2 * DAY - 1, "d __ h:mm:ss", { limits = { hours = 47 } })).toBe("47:59:59")
238
+ expect(TimeDurationUtils.format(2 * DAY, "d __ h:mm:ss", { limits = { hours = 47 } })).toBe("2 days 0:00:00")
239
+ expect(TimeDurationUtils.format({ days = 1, hours = 1 }, "d __ h __ m __", {
240
+ limits = { hours = 47, minutes = 1500 },
241
+ })).toBe("1500 minutes")
242
+ end)
243
+
244
+ it("should clamp to minValue and maxValue", function()
245
+ expect(TimeDurationUtils.format(30, "m", { minValue = 1 })).toBe("< 1")
246
+ expect(TimeDurationUtils.format(30, "m __", { minValue = 1 })).toBe("< 1 minute")
247
+ expect(TimeDurationUtils.format(90, "m __", { minValue = 1 })).toBe("2 minutes")
248
+ expect(TimeDurationUtils.format({ days = 400 }, "d __", { maxValue = 365 })).toBe("> 365 days")
249
+ end)
250
+
251
+ it("should put the sign before the first shown token", function()
252
+ expect(TimeDurationUtils.format(-(HOUR + MINUTE + SECOND), "h:mm:ss")).toBe("-1:01:01")
253
+ expect(TimeDurationUtils.format(-45, "h:mm:ss")).toBe("-45")
254
+ expect(TimeDurationUtils.format(-2 * MINUTE, "[T] m __")).toBe("T -2 minutes")
255
+ end)
256
+
257
+ it("should localize labels and let the locale place the number", function()
258
+ expect(TimeDurationUtils.format({ hours = 2 }, "h __", { locale = "es-es" })).toBe("2 horas")
259
+ expect(TimeDurationUtils.format({ hours = 1 }, "h __", { locale = "fr-fr" })).toBe("1 heure")
260
+ expect(TimeDurationUtils.format({ days = 5 }, "d __", { locale = "ru-ru" })).toBe("5 дней")
261
+ expect(TimeDurationUtils.format({ days = 2 }, "d__", { locale = "ko-kr" })).toBe("2일")
262
+ expect(TimeDurationUtils.format({ days = 2 }, "d __", { locale = "ko-kr" })).toBe("2일")
263
+ expect(TimeDurationUtils.format({ weeks = 2 }, "w __", { locale = "de-de" })).toBe("2 Wochen")
264
+ local strings: TimeDurationUtils.DurationStringOverrides = {
265
+ hours = { one = "%d hr", other = "%d hrs" },
266
+ }
267
+ expect(TimeDurationUtils.format({ hours = 2, minutes = 30 }, "h __ m __", { strings = strings })).toBe(
268
+ "2 hrs 30 minutes"
269
+ )
270
+ end)
271
+
272
+ it("should pick a template from the size of the duration", function()
273
+ expect(TimeDurationUtils.format(0)).toBe("0 seconds")
274
+ expect(TimeDurationUtils.format(0.25)).toBe("250 milliseconds")
275
+ expect(TimeDurationUtils.format(45)).toBe("0:45")
276
+ expect(TimeDurationUtils.format(123 * MINUTE)).toBe("2:03:00")
277
+ expect(TimeDurationUtils.format({ days = 3 })).toBe("3 days")
278
+ expect(TimeDurationUtils.format({ days = 14 })).toBe("2 weeks")
279
+ expect(TimeDurationUtils.format({ days = 10, hours = 2 })).toBe("1 week, 3 days, 2 hours")
280
+ expect(TimeDurationUtils.format({ days = 1, hours = 2 })).toBe("1 day, 2 hours")
281
+ expect(TimeDurationUtils.format({ years = 2 })).toBe("2 years")
282
+ expect(TimeDurationUtils.format({ years = 1, months = 2, days = 3 })).toBe("1 year, 2 months, 3 days")
283
+ end)
284
+
285
+ it("should reject a template without tokens", function()
286
+ expect(function()
287
+ TimeDurationUtils.format(5, "[none]")
288
+ end).toThrow()
289
+ end)
290
+ end)
291
+
292
+ describe("TimeDurationUtils.formatUnit", function()
293
+ it("should pluralize the amount as given", function()
294
+ expect(TimeDurationUtils.formatUnit("days", 1)).toBe("1 day")
295
+ expect(TimeDurationUtils.formatUnit("days", 45)).toBe("45 days")
296
+ expect(TimeDurationUtils.formatUnit("hours", 0)).toBe("0 hours")
297
+ expect(TimeDurationUtils.formatUnit("h", 36)).toBe("36 hours")
298
+ expect(TimeDurationUtils.formatUnit("ms", 250)).toBe("250 milliseconds")
299
+ expect(TimeDurationUtils.formatUnit("weeks", 2)).toBe("2 weeks")
300
+ end)
301
+
302
+ it("should keep fractional and negative amounts", function()
303
+ expect(TimeDurationUtils.formatUnit("minutes", 1.5)).toBe("1.5 minutes")
304
+ expect(TimeDurationUtils.formatUnit("minutes", -1)).toBe("-1 minute")
305
+ end)
306
+
307
+ it("should localize and accept overrides", function()
308
+ expect(TimeDurationUtils.formatUnit("days", 2, { locale = "es-es" })).toBe("2 días")
309
+ expect(TimeDurationUtils.formatUnit("days", 2, { locale = "ru-ru" })).toBe("2 дня")
310
+ local strings: TimeDurationUtils.DurationStringOverrides = {
311
+ days = { one = "%dd", other = "%dd" },
312
+ }
313
+ expect(TimeDurationUtils.formatUnit("days", 2, { strings = strings })).toBe("2d")
314
+ end)
315
+
316
+ it("should reject units without a phrase", function()
317
+ expect(function()
318
+ TimeDurationUtils.formatUnit("quarters", 2)
319
+ end).toThrow()
320
+ end)
321
+ end)
322
+
323
+ describe("TimeDurationUtils.humanize", function()
324
+ it("should describe the duration in words", function()
325
+ expect(TimeDurationUtils.humanize(HOUR)).toBe("an hour")
326
+ expect(TimeDurationUtils.humanize({ days = 2 })).toBe("2 days")
327
+ expect(TimeDurationUtils.humanize(45 * SECOND)).toBe("a minute")
328
+ expect(TimeDurationUtils.humanize("PT3H")).toBe("3 hours")
329
+ end)
330
+
331
+ it("should add a suffix by direction", function()
332
+ expect(TimeDurationUtils.humanize(HOUR, true)).toBe("in an hour")
333
+ expect(TimeDurationUtils.humanize(-HOUR, true)).toBe("an hour ago")
334
+ end)
335
+
336
+ it("should pass relative time options through", function()
337
+ local hourStrings: RelativeTimeUtils.RelativeTimeStringOverrides = { h = "one hour" }
338
+ local futureStrings: RelativeTimeUtils.RelativeTimeStringOverrides = { future = "%s from now" }
339
+
340
+ expect(TimeDurationUtils.humanize(HOUR, false, { strings = hourStrings })).toBe("one hour")
341
+ expect(TimeDurationUtils.humanize(HOUR, true, { strings = futureStrings })).toBe("an hour from now")
342
+ expect(TimeDurationUtils.humanize(HOUR, true, { locale = "fr-fr" })).toBe("dans une heure")
343
+ end)
344
+ end)
345
+
346
+ describe("TimeDurationUtils.toIsoString", function()
347
+ it("should format ISO 8601 durations", function()
348
+ expect(TimeDurationUtils.toIsoString({ hours = 1, minutes = 30 })).toBe("PT1H30M")
349
+ expect(TimeDurationUtils.toIsoString({ days = 1 })).toBe("P1D")
350
+ expect(TimeDurationUtils.toIsoString({ weeks = 1 })).toBe("P7D")
351
+ expect(TimeDurationUtils.toIsoString({ years = 1, months = 2 })).toBe("P1Y2M")
352
+ expect(TimeDurationUtils.toIsoString(1.5)).toBe("PT1.5S")
353
+ expect(TimeDurationUtils.toIsoString(0)).toBe("P0D")
354
+ expect(TimeDurationUtils.toIsoString(-5 * SECOND)).toBe("-PT5S")
355
+ end)
356
+
357
+ it("should round trip", function()
358
+ for _, iso in { "PT1H30M", "P1DT12H", "P1Y2M3DT4H5M6S", "PT1.5S" } do
359
+ expect(TimeDurationUtils.toIsoString(iso)).toBe(iso)
360
+ end
361
+ end)
362
+ end)