@quenty/time 2.4.6 → 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.
- package/CHANGELOG.md +23 -67
- package/README.md +2 -69
- package/deploy.nevermore.json +10 -0
- package/package.json +12 -3
- package/src/Shared/RelativeTimeUtils.lua +220 -0
- package/src/Shared/RelativeTimeUtils.spec.lua +308 -0
- package/src/Shared/Time.lua +665 -353
- package/src/Shared/Time.spec.lua +714 -0
- package/src/Shared/TimeCalendarUtils.lua +92 -0
- package/src/Shared/TimeCalendarUtils.spec.lua +156 -0
- package/src/Shared/TimeDurationUtils.lua +890 -0
- package/src/Shared/TimeDurationUtils.spec.lua +362 -0
- package/src/Shared/TimeLocalizationUtils.lua +964 -0
- package/src/Shared/TimeLocalizationUtils.spec.lua +229 -0
- package/src/jest.config.lua +3 -0
- package/src/node_modules.project.json +7 -0
- package/test/default.project.json +17 -0
- package/test/scripts/Server/ServerMain.server.lua +12 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[[
|
|
3
|
+
@class RelativeTimeUtils.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
|
+
|
|
11
|
+
local describe = Jest.Globals.describe
|
|
12
|
+
local expect = Jest.Globals.expect
|
|
13
|
+
local it = Jest.Globals.it
|
|
14
|
+
|
|
15
|
+
local BASE = 1758499200 -- 2025-09-22 Monday 00:00:00 UTC
|
|
16
|
+
local WITHOUT_SUFFIX = { withoutSuffix = true }
|
|
17
|
+
|
|
18
|
+
describe("RelativeTimeUtils.from", function()
|
|
19
|
+
local function fromOffset(seconds: number): string
|
|
20
|
+
return RelativeTimeUtils.from(BASE + seconds, BASE, WITHOUT_SUFFIX)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it("should pick the threshold for seconds, minutes and hours", function()
|
|
24
|
+
expect(fromOffset(0)).toBe("a few seconds")
|
|
25
|
+
expect(fromOffset(44)).toBe("a few seconds")
|
|
26
|
+
expect(fromOffset(45)).toBe("a minute")
|
|
27
|
+
expect(fromOffset(89)).toBe("a minute")
|
|
28
|
+
expect(fromOffset(90)).toBe("2 minutes")
|
|
29
|
+
expect(fromOffset(44 * 60)).toBe("44 minutes")
|
|
30
|
+
expect(fromOffset(45 * 60)).toBe("an hour")
|
|
31
|
+
expect(fromOffset(89 * 60)).toBe("an hour")
|
|
32
|
+
expect(fromOffset(90 * 60)).toBe("2 hours")
|
|
33
|
+
expect(fromOffset(21 * 3600)).toBe("21 hours")
|
|
34
|
+
end)
|
|
35
|
+
|
|
36
|
+
it("should pick the threshold for days and months", function()
|
|
37
|
+
expect(fromOffset(22 * 3600)).toBe("a day")
|
|
38
|
+
expect(fromOffset(35 * 3600)).toBe("a day")
|
|
39
|
+
expect(fromOffset(36 * 3600)).toBe("2 days")
|
|
40
|
+
expect(fromOffset(25 * 86400)).toBe("25 days")
|
|
41
|
+
expect(fromOffset(26 * 86400)).toBe("a month")
|
|
42
|
+
expect(fromOffset(45 * 86400)).toBe("a month")
|
|
43
|
+
expect(fromOffset(46 * 86400)).toBe("2 months")
|
|
44
|
+
end)
|
|
45
|
+
|
|
46
|
+
it("should count months and years on the calendar", function()
|
|
47
|
+
expect(RelativeTimeUtils.from("2026-07-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("10 months")
|
|
48
|
+
expect(RelativeTimeUtils.from("2026-08-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("a year")
|
|
49
|
+
expect(RelativeTimeUtils.from("2027-02-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("a year")
|
|
50
|
+
expect(RelativeTimeUtils.from("2027-03-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("2 years")
|
|
51
|
+
expect(RelativeTimeUtils.from("2035-09-22T00:00:00Z", BASE, WITHOUT_SUFFIX)).toBe("10 years")
|
|
52
|
+
expect(RelativeTimeUtils.from("2025-02-28T00:00:00Z", "2025-01-31T00:00:00Z", WITHOUT_SUFFIX)).toBe("a month")
|
|
53
|
+
end)
|
|
54
|
+
|
|
55
|
+
it("should say in for the future and ago for the past", function()
|
|
56
|
+
expect(RelativeTimeUtils.from(BASE + 86400, BASE)).toBe("in a day")
|
|
57
|
+
expect(RelativeTimeUtils.from(BASE - 86400, BASE)).toBe("a day ago")
|
|
58
|
+
expect(RelativeTimeUtils.from(BASE, BASE)).toBe("a few seconds ago")
|
|
59
|
+
expect(RelativeTimeUtils.from(BASE - 3 * 3600, BASE)).toBe("3 hours ago")
|
|
60
|
+
end)
|
|
61
|
+
|
|
62
|
+
it("should accept any DateTimeLike on both sides", function()
|
|
63
|
+
expect(RelativeTimeUtils.from(DateTime.fromUnixTimestamp(BASE + 3600), "2025-09-22T00:00:00Z")).toBe(
|
|
64
|
+
"in an hour"
|
|
65
|
+
)
|
|
66
|
+
end)
|
|
67
|
+
end)
|
|
68
|
+
|
|
69
|
+
describe("RelativeTimeUtils.to", function()
|
|
70
|
+
it("should mirror from", function()
|
|
71
|
+
expect(RelativeTimeUtils.to(BASE, BASE + 86400)).toBe("in a day")
|
|
72
|
+
expect(RelativeTimeUtils.to(BASE + 86400, BASE)).toBe("a day ago")
|
|
73
|
+
expect(RelativeTimeUtils.to(BASE, BASE + 90, WITHOUT_SUFFIX)).toBe("2 minutes")
|
|
74
|
+
end)
|
|
75
|
+
end)
|
|
76
|
+
|
|
77
|
+
describe("RelativeTimeUtils.fromNow / toNow", function()
|
|
78
|
+
it("should compare against the current time", function()
|
|
79
|
+
local hourAgo = DateTime.now().UnixTimestamp - 3600
|
|
80
|
+
|
|
81
|
+
expect(RelativeTimeUtils.fromNow(hourAgo)).toBe("an hour ago")
|
|
82
|
+
expect(RelativeTimeUtils.fromNow(hourAgo, WITHOUT_SUFFIX)).toBe("an hour")
|
|
83
|
+
expect(RelativeTimeUtils.toNow(hourAgo)).toBe("in an hour")
|
|
84
|
+
expect(RelativeTimeUtils.fromNow(nil)).toBe("a few seconds ago")
|
|
85
|
+
end)
|
|
86
|
+
end)
|
|
87
|
+
|
|
88
|
+
describe("RelativeTimeUtils.from with options", function()
|
|
89
|
+
it("should use custom thresholds and strings", function()
|
|
90
|
+
local thresholds: { RelativeTimeUtils.RelativeTimeThreshold } = {
|
|
91
|
+
{ key = "s", limit = 59, unit = "second" },
|
|
92
|
+
{ key = "mm", limit = 59, unit = "minute" },
|
|
93
|
+
{ key = "hh", limit = 23, unit = "hour" },
|
|
94
|
+
{ key = "ww", limit = 3, unit = "week" },
|
|
95
|
+
{ key = "dd", unit = "day" },
|
|
96
|
+
}
|
|
97
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = {
|
|
98
|
+
s = "%d seconds",
|
|
99
|
+
ww = "%d weeks",
|
|
100
|
+
}
|
|
101
|
+
local options: RelativeTimeUtils.RelativeTimeOptions = {
|
|
102
|
+
withoutSuffix = true,
|
|
103
|
+
thresholds = thresholds,
|
|
104
|
+
strings = strings,
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
expect(RelativeTimeUtils.from(BASE + 30, BASE, options)).toBe("30 seconds")
|
|
108
|
+
expect(RelativeTimeUtils.from(BASE + 90, BASE, options)).toBe("2 minutes")
|
|
109
|
+
expect(RelativeTimeUtils.from(BASE + 5 * 3600, BASE, options)).toBe("5 hours")
|
|
110
|
+
expect(RelativeTimeUtils.from(BASE + 14 * 86400, BASE, options)).toBe("2 weeks")
|
|
111
|
+
expect(RelativeTimeUtils.from(BASE + 40 * 86400, BASE, options)).toBe("40 days")
|
|
112
|
+
end)
|
|
113
|
+
|
|
114
|
+
it("should override the suffix strings", function()
|
|
115
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { future = "%s from now", past = "%s earlier" }
|
|
116
|
+
local options = { strings = strings }
|
|
117
|
+
|
|
118
|
+
expect(RelativeTimeUtils.from(BASE + 3600, BASE, options)).toBe("an hour from now")
|
|
119
|
+
expect(RelativeTimeUtils.from(BASE - 3600, BASE, options)).toBe("an hour earlier")
|
|
120
|
+
end)
|
|
121
|
+
|
|
122
|
+
it("should use custom rounding", function()
|
|
123
|
+
expect(RelativeTimeUtils.from(BASE + 90, BASE, { withoutSuffix = true, rounding = math.floor })).toBe(
|
|
124
|
+
"a minute"
|
|
125
|
+
)
|
|
126
|
+
expect(RelativeTimeUtils.from(BASE + 90, BASE, { withoutSuffix = true, rounding = math.ceil })).toBe(
|
|
127
|
+
"2 minutes"
|
|
128
|
+
)
|
|
129
|
+
end)
|
|
130
|
+
|
|
131
|
+
it("should error on a threshold key without a string", function()
|
|
132
|
+
local thresholds: { RelativeTimeUtils.RelativeTimeThreshold } = { { key = "nope", unit = "second" } }
|
|
133
|
+
|
|
134
|
+
expect(function()
|
|
135
|
+
RelativeTimeUtils.from(BASE + 30, BASE, { thresholds = thresholds })
|
|
136
|
+
end).toThrow("No relative time string")
|
|
137
|
+
end)
|
|
138
|
+
end)
|
|
139
|
+
|
|
140
|
+
describe("RelativeTimeUtils.from with a locale", function()
|
|
141
|
+
local function from(seconds: number, locale: string, withoutSuffix: boolean?): string
|
|
142
|
+
return RelativeTimeUtils.from(BASE + seconds, BASE, { locale = locale, withoutSuffix = withoutSuffix })
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
it("should use the locale's strings and suffixes", function()
|
|
146
|
+
expect(from(-60, "fr-fr")).toBe("il y a une minute")
|
|
147
|
+
expect(from(3 * 3600, "es-es")).toBe("en 3 horas")
|
|
148
|
+
expect(from(-2 * 86400, "pt-br")).toBe("há 2 dias")
|
|
149
|
+
expect(from(60, "ja-jp")).toBe("1分後")
|
|
150
|
+
expect(from(3 * 86400, "zh-cn")).toBe("3 天内")
|
|
151
|
+
expect(from(-3 * 86400, "zh-tw")).toBe("3 天前")
|
|
152
|
+
expect(from(3600, "ko-kr")).toBe("한 시간 후")
|
|
153
|
+
expect(from(5 * 60, "tr-tr")).toBe("5 dakika sonra")
|
|
154
|
+
end)
|
|
155
|
+
|
|
156
|
+
it("should inflect German after in and vor", function()
|
|
157
|
+
expect(from(60, "de-de")).toBe("in einer Minute")
|
|
158
|
+
expect(from(-60, "de-de")).toBe("vor einer Minute")
|
|
159
|
+
expect(from(60, "de-de", true)).toBe("eine Minute")
|
|
160
|
+
expect(from(-3 * 86400, "de-de")).toBe("vor 3 Tagen")
|
|
161
|
+
expect(from(3 * 86400, "de-de", true)).toBe("3 Tage")
|
|
162
|
+
end)
|
|
163
|
+
|
|
164
|
+
it("should pick Russian and Polish plural forms", function()
|
|
165
|
+
expect(from(2 * 60, "ru-ru")).toBe("через 2 минуты")
|
|
166
|
+
expect(from(5 * 60, "ru-ru")).toBe("через 5 минут")
|
|
167
|
+
expect(from(21 * 60, "ru-ru")).toBe("через 21 минуту")
|
|
168
|
+
expect(from(21 * 60, "ru-ru", true)).toBe("21 минута")
|
|
169
|
+
expect(from(-60, "ru-ru")).toBe("минуту назад")
|
|
170
|
+
expect(from(2 * 3600, "pl-pl")).toBe("za 2 godziny")
|
|
171
|
+
expect(from(5 * 3600, "pl-pl")).toBe("za 5 godzin")
|
|
172
|
+
expect(from(60, "pl-pl")).toBe("za minutę")
|
|
173
|
+
expect(from(60, "pl-pl", true)).toBe("minuta")
|
|
174
|
+
end)
|
|
175
|
+
|
|
176
|
+
it("should resolve regional variants and let overrides win", function()
|
|
177
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { h = "une petite heure" }
|
|
178
|
+
|
|
179
|
+
expect(from(-60, "fr-ca")).toBe("il y a une minute")
|
|
180
|
+
expect(RelativeTimeUtils.from(BASE + 3600, BASE, { locale = "fr-fr", strings = strings })).toBe(
|
|
181
|
+
"dans une petite heure"
|
|
182
|
+
)
|
|
183
|
+
end)
|
|
184
|
+
end)
|
|
185
|
+
|
|
186
|
+
describe("RelativeTimeUtils localization edge cases", function()
|
|
187
|
+
local function from(seconds: number, locale: string, withoutSuffix: boolean?): string
|
|
188
|
+
return RelativeTimeUtils.from(BASE + seconds, BASE, { locale = locale, withoutSuffix = withoutSuffix })
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
it("should call function valued strings with the amount, suffix flag, key and direction", function()
|
|
192
|
+
local calls: { { any } } = {}
|
|
193
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = {
|
|
194
|
+
mm = function(amount: number, withoutSuffix: boolean, key: string, isFuture: boolean): string
|
|
195
|
+
table.insert(calls, { amount :: any, withoutSuffix, key, isFuture })
|
|
196
|
+
return "custom"
|
|
197
|
+
end,
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
expect(RelativeTimeUtils.from(BASE + 5 * 60, BASE, { strings = strings })).toBe("in custom")
|
|
201
|
+
expect(RelativeTimeUtils.from(BASE - 5 * 60, BASE, { strings = strings, withoutSuffix = true })).toBe("custom")
|
|
202
|
+
expect(calls).toEqual({
|
|
203
|
+
{ 5 :: any, false, "mm", true },
|
|
204
|
+
{ 5 :: any, true, "mm", false },
|
|
205
|
+
})
|
|
206
|
+
end)
|
|
207
|
+
|
|
208
|
+
it("should let a string override replace a locale's function", function()
|
|
209
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { m = "1 Min." }
|
|
210
|
+
|
|
211
|
+
expect(RelativeTimeUtils.from(BASE + 60, BASE, { locale = "de-de", strings = strings })).toBe("in 1 Min.")
|
|
212
|
+
expect(RelativeTimeUtils.from(BASE + 3600, BASE, { locale = "de-de", strings = strings })).toBe(
|
|
213
|
+
"in einer Stunde"
|
|
214
|
+
)
|
|
215
|
+
end)
|
|
216
|
+
|
|
217
|
+
it("should reject function valued future and past", function()
|
|
218
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = {
|
|
219
|
+
future = function(): string
|
|
220
|
+
return "soon"
|
|
221
|
+
end,
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
expect(function()
|
|
225
|
+
RelativeTimeUtils.from(BASE + 60, BASE, { strings = strings })
|
|
226
|
+
end).toThrow("future and past must be strings")
|
|
227
|
+
end)
|
|
228
|
+
|
|
229
|
+
it("should use the singular step for a rounded amount of 1 in every locale", function()
|
|
230
|
+
expect(from(60, "de-de")).toBe("in einer Minute")
|
|
231
|
+
expect(from(60 * 60, "ru-ru")).toBe("через час")
|
|
232
|
+
expect(from(-24 * 3600, "pl-pl")).toBe("1 dzień temu")
|
|
233
|
+
expect(from(24 * 3600, "ja-jp")).toBe("1日後")
|
|
234
|
+
end)
|
|
235
|
+
|
|
236
|
+
it("should treat 11 to 14 as many in Russian even past 100", function()
|
|
237
|
+
expect(from(11 * 60, "ru-ru")).toBe("через 11 минут")
|
|
238
|
+
expect(from(12 * 3600, "ru-ru")).toBe("через 12 часов")
|
|
239
|
+
expect(from(14 * 3600, "ru-ru")).toBe("через 14 часов")
|
|
240
|
+
expect(RelativeTimeUtils.from("2136-09-22T00:00:00Z", BASE, { locale = "ru-ru", withoutSuffix = true })).toBe(
|
|
241
|
+
"111 лет"
|
|
242
|
+
)
|
|
243
|
+
expect(RelativeTimeUtils.from("2126-09-22T00:00:00Z", BASE, { locale = "ru-ru" })).toBe("через 101 год")
|
|
244
|
+
end)
|
|
245
|
+
|
|
246
|
+
it("should keep Polish teens and 22 apart", function()
|
|
247
|
+
expect(from(12 * 3600, "pl-pl")).toBe("za 12 godzin")
|
|
248
|
+
expect(from(22 * 60, "pl-pl")).toBe("za 22 minuty")
|
|
249
|
+
expect(RelativeTimeUtils.from("2137-09-22T00:00:00Z", BASE, { locale = "pl-pl", withoutSuffix = true })).toBe(
|
|
250
|
+
"112 lat"
|
|
251
|
+
)
|
|
252
|
+
expect(RelativeTimeUtils.from("2047-09-22T00:00:00Z", BASE, { locale = "pl-pl" })).toBe("za 22 lata")
|
|
253
|
+
expect(RelativeTimeUtils.from("2037-09-22T00:00:00Z", BASE, { locale = "pl-pl" })).toBe("za 12 lat")
|
|
254
|
+
end)
|
|
255
|
+
|
|
256
|
+
it("should inflect German months and years after vor", function()
|
|
257
|
+
expect(RelativeTimeUtils.from("2025-07-01T00:00:00Z", BASE, { locale = "de-de" })).toBe("vor 3 Monaten")
|
|
258
|
+
expect(RelativeTimeUtils.from("2025-07-01T00:00:00Z", BASE, { locale = "de-de", withoutSuffix = true })).toBe(
|
|
259
|
+
"3 Monate"
|
|
260
|
+
)
|
|
261
|
+
expect(RelativeTimeUtils.from("2020-09-22T00:00:00Z", BASE, { locale = "de-de" })).toBe("vor 5 Jahren")
|
|
262
|
+
expect(RelativeTimeUtils.from("2024-09-22T00:00:00Z", BASE, { locale = "de-de" })).toBe("vor einem Jahr")
|
|
263
|
+
end)
|
|
264
|
+
|
|
265
|
+
it("should place the suffix where the language puts it", function()
|
|
266
|
+
expect(from(3600, "it-it")).toBe("tra un'ora")
|
|
267
|
+
expect(from(-3600, "it-it")).toBe("un'ora fa")
|
|
268
|
+
expect(from(-3600, "id-id")).toBe("sejam yang lalu")
|
|
269
|
+
expect(from(2 * 86400, "vi-vn")).toBe("2 ngày tới")
|
|
270
|
+
expect(from(-2 * 86400, "th-th")).toBe("2 วันที่แล้ว")
|
|
271
|
+
expect(from(2 * 86400, "ar")).toBe("بعد 2 أيام")
|
|
272
|
+
end)
|
|
273
|
+
|
|
274
|
+
it("should combine a locale with custom thresholds", function()
|
|
275
|
+
local thresholds: { RelativeTimeUtils.RelativeTimeThreshold } = {
|
|
276
|
+
{ key = "s", limit = 59, unit = "second" },
|
|
277
|
+
{ key = "mm", limit = 59, unit = "minute" },
|
|
278
|
+
{ key = "hh", limit = 23, unit = "hour" },
|
|
279
|
+
{ key = "ww", unit = "week" },
|
|
280
|
+
}
|
|
281
|
+
local strings: RelativeTimeUtils.RelativeTimeStringOverrides = { ww = "%d semaines" }
|
|
282
|
+
|
|
283
|
+
expect(
|
|
284
|
+
RelativeTimeUtils.from(
|
|
285
|
+
BASE + 14 * 86400,
|
|
286
|
+
BASE,
|
|
287
|
+
{ locale = "fr-fr", thresholds = thresholds, strings = strings }
|
|
288
|
+
)
|
|
289
|
+
).toBe("dans 2 semaines")
|
|
290
|
+
expect(
|
|
291
|
+
RelativeTimeUtils.from(
|
|
292
|
+
BASE + 5 * 3600,
|
|
293
|
+
BASE,
|
|
294
|
+
{ locale = "fr-fr", thresholds = thresholds, strings = strings }
|
|
295
|
+
)
|
|
296
|
+
).toBe("dans 5 heures")
|
|
297
|
+
expect(function()
|
|
298
|
+
RelativeTimeUtils.from(BASE + 14 * 86400, BASE, { locale = "fr-fr", thresholds = thresholds })
|
|
299
|
+
end).toThrow("No relative time string")
|
|
300
|
+
end)
|
|
301
|
+
|
|
302
|
+
it("should resolve the locale loosely", function()
|
|
303
|
+
expect(from(-60, "FR-FR")).toBe("il y a une minute")
|
|
304
|
+
expect(from(-60, "fr_CA")).toBe("il y a une minute")
|
|
305
|
+
expect(from(-60, "zh-Hant")).toBe("1 分鐘前")
|
|
306
|
+
expect(from(-60, "xx-yy")).toBe("a minute ago")
|
|
307
|
+
end)
|
|
308
|
+
end)
|