@quenty/time 2.5.0 → 2.6.1
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 +15 -0
- 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 +664 -352
- 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,229 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[[
|
|
3
|
+
@class TimeLocalizationUtils.spec.lua
|
|
4
|
+
]]
|
|
5
|
+
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local Jest = require("Jest")
|
|
9
|
+
local TimeLocalizationUtils = require("TimeLocalizationUtils")
|
|
10
|
+
|
|
11
|
+
local describe = Jest.Globals.describe
|
|
12
|
+
local expect = Jest.Globals.expect
|
|
13
|
+
local it = Jest.Globals.it
|
|
14
|
+
|
|
15
|
+
-- The locales NumberLocalizationUtils covers
|
|
16
|
+
local LOCALES = {
|
|
17
|
+
"en-us",
|
|
18
|
+
"es-es",
|
|
19
|
+
"fr-fr",
|
|
20
|
+
"de-de",
|
|
21
|
+
"pt-br",
|
|
22
|
+
"zh-cn",
|
|
23
|
+
"zh-cjv",
|
|
24
|
+
"zh-tw",
|
|
25
|
+
"ko-kr",
|
|
26
|
+
"ja-jp",
|
|
27
|
+
"it-it",
|
|
28
|
+
"ru-ru",
|
|
29
|
+
"id-id",
|
|
30
|
+
"vi-vn",
|
|
31
|
+
"th-th",
|
|
32
|
+
"tr-tr",
|
|
33
|
+
"pl-pl",
|
|
34
|
+
"ar",
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
local CALENDAR_KEYS = { "sameDay", "nextDay", "nextWeek", "lastDay", "lastWeek", "sameElse" }
|
|
38
|
+
local RELATIVE_KEYS = { "future", "past", "s", "m", "mm", "h", "hh", "d", "dd", "M", "MM", "y", "yy" }
|
|
39
|
+
local DURATION_KEYS = { "years", "months", "weeks", "days", "hours", "minutes", "seconds", "milliseconds" }
|
|
40
|
+
|
|
41
|
+
describe("TimeLocalizationUtils", function()
|
|
42
|
+
it("should default to English", function()
|
|
43
|
+
expect(TimeLocalizationUtils.getCalendarFormatsForLocale(nil).sameDay).toBe("[Today at] LT")
|
|
44
|
+
expect(TimeLocalizationUtils.getRelativeTimeStringsForLocale(nil).future).toBe("in %s")
|
|
45
|
+
expect((TimeLocalizationUtils.getDurationStringsForLocale(nil).hours :: any).other).toBe("%d hours")
|
|
46
|
+
end)
|
|
47
|
+
|
|
48
|
+
it("should resolve regional variants to their language", function()
|
|
49
|
+
expect(TimeLocalizationUtils.getCalendarFormatsForLocale("en-gb")).toBe(
|
|
50
|
+
TimeLocalizationUtils.getCalendarFormatsForLocale("en-us")
|
|
51
|
+
)
|
|
52
|
+
expect(TimeLocalizationUtils.getRelativeTimeStringsForLocale("es-mx")).toBe(
|
|
53
|
+
TimeLocalizationUtils.getRelativeTimeStringsForLocale("es-es")
|
|
54
|
+
)
|
|
55
|
+
expect(TimeLocalizationUtils.getDurationStringsForLocale("zh-hant")).toBe(
|
|
56
|
+
TimeLocalizationUtils.getDurationStringsForLocale("zh-tw")
|
|
57
|
+
)
|
|
58
|
+
expect(TimeLocalizationUtils.getDurationStringsForLocale("zh-hans")).toBe(
|
|
59
|
+
TimeLocalizationUtils.getDurationStringsForLocale("zh-cn")
|
|
60
|
+
)
|
|
61
|
+
end)
|
|
62
|
+
|
|
63
|
+
it("should cover every NumberLocalizationUtils locale completely", function()
|
|
64
|
+
for _, locale in LOCALES do
|
|
65
|
+
local calendarFormats: any = TimeLocalizationUtils.getCalendarFormatsForLocale(locale)
|
|
66
|
+
local relativeTimeStrings: any = TimeLocalizationUtils.getRelativeTimeStringsForLocale(locale)
|
|
67
|
+
local durationStrings: any = TimeLocalizationUtils.getDurationStringsForLocale(locale)
|
|
68
|
+
|
|
69
|
+
for _, key in CALENDAR_KEYS do
|
|
70
|
+
expect(calendarFormats[key]).toEqual(expect.any("string"))
|
|
71
|
+
end
|
|
72
|
+
for _, key in RELATIVE_KEYS do
|
|
73
|
+
expect(relativeTimeStrings[key] ~= nil).toBe(true)
|
|
74
|
+
end
|
|
75
|
+
for _, key in DURATION_KEYS do
|
|
76
|
+
expect(durationStrings[key] ~= nil).toBe(true)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end)
|
|
80
|
+
|
|
81
|
+
it("should differ between languages", function()
|
|
82
|
+
expect(TimeLocalizationUtils.getRelativeTimeStringsForLocale("fr-fr").past).toBe("il y a %s")
|
|
83
|
+
expect(TimeLocalizationUtils.getRelativeTimeStringsForLocale("ja-jp").past).toBe("%s前")
|
|
84
|
+
expect(TimeLocalizationUtils.getCalendarFormatsForLocale("de-de").sameDay).toBe("[heute um] LT [Uhr]")
|
|
85
|
+
end)
|
|
86
|
+
|
|
87
|
+
it("should expose the whole locale", function()
|
|
88
|
+
local locale = TimeLocalizationUtils.getLocale("fr-fr")
|
|
89
|
+
|
|
90
|
+
expect(locale.calendar).toBe(TimeLocalizationUtils.getCalendarFormatsForLocale("fr-fr"))
|
|
91
|
+
expect(locale.relativeTime).toBe(TimeLocalizationUtils.getRelativeTimeStringsForLocale("fr-fr"))
|
|
92
|
+
expect(locale.duration).toBe(TimeLocalizationUtils.getDurationStringsForLocale("fr-fr"))
|
|
93
|
+
expect(TimeLocalizationUtils.getLocale("zh-cjv")).toBe(TimeLocalizationUtils.getLocale("zh-cn"))
|
|
94
|
+
end)
|
|
95
|
+
|
|
96
|
+
it("should return read-only tables", function()
|
|
97
|
+
expect(table.isfrozen(TimeLocalizationUtils.getLocale("en-us"))).toBe(true)
|
|
98
|
+
expect(table.isfrozen(TimeLocalizationUtils.getCalendarFormatsForLocale("en-us"))).toBe(true)
|
|
99
|
+
expect(table.isfrozen(TimeLocalizationUtils.getRelativeTimeStringsForLocale("ru-ru"))).toBe(true)
|
|
100
|
+
expect(table.isfrozen(TimeLocalizationUtils.getDurationStringsForLocale("pl-pl"))).toBe(true)
|
|
101
|
+
end)
|
|
102
|
+
end)
|
|
103
|
+
|
|
104
|
+
describe("TimeLocalizationUtils locale resolution", function()
|
|
105
|
+
it("should ignore case and accept underscores", function()
|
|
106
|
+
local english = TimeLocalizationUtils.getLocale("en-us")
|
|
107
|
+
|
|
108
|
+
expect(TimeLocalizationUtils.getLocale("EN-US")).toBe(english)
|
|
109
|
+
expect(TimeLocalizationUtils.getLocale("en_US")).toBe(english)
|
|
110
|
+
expect(TimeLocalizationUtils.getLocale("En")).toBe(english)
|
|
111
|
+
expect(TimeLocalizationUtils.getLocale("Fr-CA")).toBe(TimeLocalizationUtils.getLocale("fr-fr"))
|
|
112
|
+
end)
|
|
113
|
+
|
|
114
|
+
it("should route Chinese by script and region", function()
|
|
115
|
+
local simplified = TimeLocalizationUtils.getLocale("zh-cn")
|
|
116
|
+
local traditional = TimeLocalizationUtils.getLocale("zh-tw")
|
|
117
|
+
|
|
118
|
+
expect(simplified).never.toBe(traditional)
|
|
119
|
+
expect(TimeLocalizationUtils.getLocale("zh")).toBe(simplified)
|
|
120
|
+
expect(TimeLocalizationUtils.getLocale("zh-hans")).toBe(simplified)
|
|
121
|
+
expect(TimeLocalizationUtils.getLocale("zh-sg")).toBe(simplified)
|
|
122
|
+
expect(TimeLocalizationUtils.getLocale("zh-hant")).toBe(traditional)
|
|
123
|
+
expect(TimeLocalizationUtils.getLocale("zh-hk")).toBe(traditional)
|
|
124
|
+
expect(TimeLocalizationUtils.getLocale("zh-Hant-TW")).toBe(traditional)
|
|
125
|
+
end)
|
|
126
|
+
|
|
127
|
+
it("should fall back to English for nil, empty and unknown locales", function()
|
|
128
|
+
local english = TimeLocalizationUtils.getLocale("en-us")
|
|
129
|
+
|
|
130
|
+
expect(TimeLocalizationUtils.getLocale(nil)).toBe(english)
|
|
131
|
+
expect(TimeLocalizationUtils.getLocale("")).toBe(english)
|
|
132
|
+
expect(TimeLocalizationUtils.getLocale("xx-yy")).toBe(english)
|
|
133
|
+
expect(TimeLocalizationUtils.getLocale("123")).toBe(english)
|
|
134
|
+
end)
|
|
135
|
+
|
|
136
|
+
it("should return the same table on every call", function()
|
|
137
|
+
expect(TimeLocalizationUtils.getLocale("ja-jp")).toBe(TimeLocalizationUtils.getLocale("ja-jp"))
|
|
138
|
+
expect(TimeLocalizationUtils.getCalendarFormatsForLocale("ja-jp")).toBe(
|
|
139
|
+
TimeLocalizationUtils.getCalendarFormatsForLocale("ja")
|
|
140
|
+
)
|
|
141
|
+
end)
|
|
142
|
+
|
|
143
|
+
it("should reject writes", function()
|
|
144
|
+
expect(function()
|
|
145
|
+
(TimeLocalizationUtils.getLocale("en-us") :: any).calendar = nil
|
|
146
|
+
end).toThrow("readonly")
|
|
147
|
+
expect(function()
|
|
148
|
+
(TimeLocalizationUtils.getRelativeTimeStringsForLocale("en-us") :: any).s = "now"
|
|
149
|
+
end).toThrow("readonly")
|
|
150
|
+
end)
|
|
151
|
+
end)
|
|
152
|
+
|
|
153
|
+
describe("TimeLocalizationUtils string invariants", function()
|
|
154
|
+
local AMOUNT_KEYS = { "mm", "hh", "dd", "MM", "yy" }
|
|
155
|
+
|
|
156
|
+
it("should give every locale a %s slot in future and past", function()
|
|
157
|
+
for _, locale in LOCALES do
|
|
158
|
+
local strings = TimeLocalizationUtils.getRelativeTimeStringsForLocale(locale)
|
|
159
|
+
|
|
160
|
+
expect(string.find(strings.future, "%%s") ~= nil).toBe(true)
|
|
161
|
+
expect(string.find(strings.past, "%%s") ~= nil).toBe(true)
|
|
162
|
+
end
|
|
163
|
+
end)
|
|
164
|
+
|
|
165
|
+
it("should put the amount into every plural relative time string", function()
|
|
166
|
+
for _, locale in LOCALES do
|
|
167
|
+
local strings: any = TimeLocalizationUtils.getRelativeTimeStringsForLocale(locale)
|
|
168
|
+
|
|
169
|
+
for _, key in AMOUNT_KEYS do
|
|
170
|
+
local value = strings[key]
|
|
171
|
+
if type(value) == "function" then
|
|
172
|
+
expect(string.find(value(7, false, key, true), "7", 1, true) ~= nil).toBe(true)
|
|
173
|
+
expect(string.find(value(7, true, key, false), "7", 1, true) ~= nil).toBe(true)
|
|
174
|
+
else
|
|
175
|
+
expect(string.find(value, "%%d") ~= nil).toBe(true)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end)
|
|
180
|
+
|
|
181
|
+
it("should return a string from every function valued relative time string", function()
|
|
182
|
+
for _, locale in LOCALES do
|
|
183
|
+
local strings: any = TimeLocalizationUtils.getRelativeTimeStringsForLocale(locale)
|
|
184
|
+
|
|
185
|
+
for _, key in RELATIVE_KEYS do
|
|
186
|
+
local value = strings[key]
|
|
187
|
+
if type(value) == "function" then
|
|
188
|
+
expect(value(1, true, key, false)).toEqual(expect.any("string"))
|
|
189
|
+
expect(value(1, false, key, true)).toEqual(expect.any("string"))
|
|
190
|
+
expect(value(2, false, key, false)).toEqual(expect.any("string"))
|
|
191
|
+
end
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
end)
|
|
195
|
+
|
|
196
|
+
it("should put the amount into every duration phrase", function()
|
|
197
|
+
for _, locale in LOCALES do
|
|
198
|
+
local strings: any = TimeLocalizationUtils.getDurationStringsForLocale(locale)
|
|
199
|
+
|
|
200
|
+
for _, key in DURATION_KEYS do
|
|
201
|
+
local phrase = strings[key]
|
|
202
|
+
if type(phrase) == "function" then
|
|
203
|
+
expect(string.find(phrase(3), "3", 1, true) ~= nil).toBe(true)
|
|
204
|
+
expect(string.find(phrase(1), "1", 1, true) ~= nil).toBe(true)
|
|
205
|
+
else
|
|
206
|
+
expect(string.find(phrase.one, "%%d") ~= nil).toBe(true)
|
|
207
|
+
expect(string.find(phrase.other, "%%d") ~= nil).toBe(true)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
end)
|
|
212
|
+
|
|
213
|
+
it("should give every calendar phrase a time except sameElse", function()
|
|
214
|
+
for _, locale in LOCALES do
|
|
215
|
+
local formats: any = TimeLocalizationUtils.getCalendarFormatsForLocale(locale)
|
|
216
|
+
|
|
217
|
+
for _, key in CALENDAR_KEYS do
|
|
218
|
+
if key == "sameElse" then
|
|
219
|
+
expect(formats[key]).toBe("L")
|
|
220
|
+
else
|
|
221
|
+
expect(string.find(formats[key], "LT", 1, true) ~= nil).toBe(true)
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
expect(string.find(formats.nextWeek, "ddd", 1, true) ~= nil).toBe(true)
|
|
226
|
+
expect(string.find(formats.lastWeek, "ddd", 1, true) ~= nil).toBe(true)
|
|
227
|
+
end
|
|
228
|
+
end)
|
|
229
|
+
end)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
--!nonstrict
|
|
2
|
+
local ServerScriptService = game:GetService("ServerScriptService")
|
|
3
|
+
|
|
4
|
+
local root = ServerScriptService.time
|
|
5
|
+
local loader = root:FindFirstChild("LoaderUtils", true).Parent
|
|
6
|
+
local require = require(loader).bootstrapGame(root)
|
|
7
|
+
|
|
8
|
+
local NevermoreTestRunnerUtils = require("NevermoreTestRunnerUtils")
|
|
9
|
+
|
|
10
|
+
if NevermoreTestRunnerUtils.runTestsIfNeededAsync(root) then
|
|
11
|
+
return
|
|
12
|
+
end
|