@quenty/metricutils 2.4.4 → 2.5.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.
Files changed (64) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +80 -2
  3. package/deploy.nevermore.json +10 -0
  4. package/package.json +22 -4
  5. package/src/Shared/Definitions/MetricUnitDefinitions.lua +1082 -0
  6. package/src/Shared/Definitions/MetricUnitDefinitions.spec.lua +55 -0
  7. package/src/Shared/Definitions/MetricUnitDimensionUtils.lua +164 -0
  8. package/src/Shared/Definitions/MetricUnitDimensionUtils.spec.lua +49 -0
  9. package/src/Shared/Definitions/MetricUnitKinds.lua +237 -0
  10. package/src/Shared/Definitions/MetricUnitKinds.spec.lua +51 -0
  11. package/src/Shared/Formatting/MetricUnitDisplayUnitUtils.lua +129 -0
  12. package/src/Shared/Formatting/MetricUnitFormatUtils.lua +924 -0
  13. package/src/Shared/Formatting/MetricUnitFormatUtils.spec.lua +280 -0
  14. package/src/Shared/Formatting/RxMetricUnitFormatUtils.lua +62 -0
  15. package/src/Shared/Localization/MetricUnitFallbackStrings.lua +289 -0
  16. package/src/Shared/Localization/MetricUnitFallbackStrings.spec.lua +82 -0
  17. package/src/Shared/Localization/MetricUnitTranslator/ar.json.txt +182 -0
  18. package/src/Shared/Localization/MetricUnitTranslator/de.json.txt +182 -0
  19. package/src/Shared/Localization/MetricUnitTranslator/en.json.txt +182 -0
  20. package/src/Shared/Localization/MetricUnitTranslator/es.json.txt +182 -0
  21. package/src/Shared/Localization/MetricUnitTranslator/fr.json.txt +182 -0
  22. package/src/Shared/Localization/MetricUnitTranslator/id.json.txt +182 -0
  23. package/src/Shared/Localization/MetricUnitTranslator/init.lua +92 -0
  24. package/src/Shared/Localization/MetricUnitTranslator/it.json.txt +182 -0
  25. package/src/Shared/Localization/MetricUnitTranslator/ja.json.txt +182 -0
  26. package/src/Shared/Localization/MetricUnitTranslator/ko.json.txt +182 -0
  27. package/src/Shared/Localization/MetricUnitTranslator/pl.json.txt +182 -0
  28. package/src/Shared/Localization/MetricUnitTranslator/pt.json.txt +182 -0
  29. package/src/Shared/Localization/MetricUnitTranslator/ru.json.txt +182 -0
  30. package/src/Shared/Localization/MetricUnitTranslator/th.json.txt +182 -0
  31. package/src/Shared/Localization/MetricUnitTranslator/tr.json.txt +182 -0
  32. package/src/Shared/Localization/MetricUnitTranslator/vi.json.txt +182 -0
  33. package/src/Shared/Localization/MetricUnitTranslator/zh-cn.json.txt +182 -0
  34. package/src/Shared/Localization/MetricUnitTranslator/zh-tw.json.txt +182 -0
  35. package/src/Shared/Localization/MetricUnitTranslator.spec.lua +309 -0
  36. package/src/Shared/Localization/PluralCategoryUtils.lua +103 -0
  37. package/src/Shared/Localization/PluralCategoryUtils.spec.lua +77 -0
  38. package/src/Shared/Logarithmic/DecibelUtils.lua +72 -0
  39. package/src/Shared/Logarithmic/DecibelUtils.spec.lua +35 -0
  40. package/src/Shared/MetricUnit.Angles.spec.lua +80 -0
  41. package/src/Shared/MetricUnit.Customary.spec.lua +104 -0
  42. package/src/Shared/MetricUnit.Derived.spec.lua +173 -0
  43. package/src/Shared/MetricUnit.Display.spec.lua +153 -0
  44. package/src/Shared/MetricUnit.Electrical.spec.lua +114 -0
  45. package/src/Shared/MetricUnit.Math.spec.lua +112 -0
  46. package/src/Shared/MetricUnit.Roblox.spec.lua +34 -0
  47. package/src/Shared/MetricUnit.SI.spec.lua +127 -0
  48. package/src/Shared/MetricUnit.Spring.spec.lua +94 -0
  49. package/src/Shared/MetricUnit.Temperature.spec.lua +212 -0
  50. package/src/Shared/MetricUnit.Time.spec.lua +202 -0
  51. package/src/Shared/MetricUnit.Units.spec.lua +210 -0
  52. package/src/Shared/MetricUnit.lua +1730 -0
  53. package/src/Shared/MetricUnit.spec.lua +362 -0
  54. package/src/Shared/MetricUtils.lua +46 -15
  55. package/src/Shared/MetricUtils.spec.lua +76 -0
  56. package/src/Shared/Parsing/MetricUnitParseUtils.lua +799 -0
  57. package/src/Shared/Parsing/MetricUnitParseUtils.spec.lua +189 -0
  58. package/src/Shared/Systems/MetricUnitSystem.lua +322 -0
  59. package/src/Shared/Systems/MetricUnitSystem.spec.lua +181 -0
  60. package/src/Shared/Systems/MetricUnitSystems.lua +225 -0
  61. package/src/jest.config.lua +3 -0
  62. package/src/node_modules.project.json +7 -0
  63. package/test/default.project.json +25 -0
  64. package/test/scripts/Server/ServerMain.server.lua +14 -0
@@ -0,0 +1,225 @@
1
+ --!strict
2
+ --[=[
3
+ The well-known [MetricUnitSystem]s, and a builder for Roblox systems at a game's own scale.
4
+
5
+ - `METRIC`: SI and the metric units people read, such as km, kg, L and °C. The default for every value.
6
+ - `US_CUSTOMARY`: the United States: US gallons, quarts and cups, short tons, mpg (US).
7
+ - `IMPERIAL`: Britain: imperial gallons, pints and fluid ounces, stone, mpg (UK).
8
+ - `US_CUSTOMARY_AND_IMPERIAL`: the units both share, which the two fall back to: miles, feet and inches, mph,
9
+ pounds, BTU, horsepower, psi, °F.
10
+ - `ROBLOX`: studs against the default avatar, 28 cm each, with studs per second and Roblox mass units.
11
+
12
+ A value shows itself in the system of the unit it was made from. To show it in another, convert it or ask
13
+ that system to format it:
14
+
15
+ ```lua
16
+ local MINI = MetricUnitSystems.roblox({ name = "robloxMini", metersPerStud = 0.028 })
17
+
18
+ local speed = MINI:New(part.AssemblyLinearVelocity.Magnitude, "studPerSecond")
19
+ MetricUnit.format(speed) -- in the game's studs per second
20
+ MetricUnitSystems.IMPERIAL:Format(speed) -- in mph, for a player who chose imperial
21
+ MetricUnit.format(speed:ToMetric()) -- in m/s
22
+ ```
23
+
24
+ @class MetricUnitSystems
25
+ ]=]
26
+
27
+ local require = require(script.Parent.loader).load(script)
28
+
29
+ local MetricUnitDefinitions = require("MetricUnitDefinitions")
30
+ local MetricUnitSystem = require("MetricUnitSystem")
31
+
32
+ local MetricUnitSystems = {}
33
+
34
+ --[=[
35
+ SI and the metric units people read.
36
+
37
+ @prop METRIC MetricUnitSystem
38
+ @within MetricUnitSystems
39
+ ]=]
40
+ MetricUnitSystems.METRIC = MetricUnitSystem.new({
41
+ name = "metric",
42
+ displayUnits = {
43
+ { "year", "day", "hour", "minute", "second", "millisecond", "microsecond" },
44
+ { "kilometer", "meter", "centimeter", "millimeter", "micrometer", "nanometer" },
45
+ { "squareKilometer", "squareMeter", "squareCentimeter", "squareMillimeter" },
46
+ { "cubicMeter", "liter", "milliliter" },
47
+ { "petabyte", "terabyte", "gigabyte", "megabyte", "kilobyte", "byte" },
48
+ { "tonne", "kilogram", "gram", "milligram", "microgram" },
49
+ { "meterPerSecondSquared" },
50
+ { "meganewton", "kilonewton", "newton" },
51
+ { "megajoule", "kilojoule", "joule" },
52
+ { "gigawatt", "megawatt", "kilowatt", "watt", "milliwatt" },
53
+ { "megapascal", "kilopascal", "pascal" },
54
+ { "kilogramPerCubicMeter" },
55
+ { "kelvin" },
56
+ { "degree" },
57
+ { "degreePerSecond" },
58
+ { "radianPerSecondSquared" },
59
+ { "ampere", "milliampere", "microampere" },
60
+ { "coulomb" },
61
+ { "kilovolt", "volt", "millivolt" },
62
+ { "megohm", "kilohm", "ohm" },
63
+ { "siemens" },
64
+ { "farad", "microfarad", "nanofarad", "picofarad" },
65
+ { "henry", "millihenry", "microhenry" },
66
+ { "weber" },
67
+ { "tesla", "millitesla" },
68
+ { "kilomole", "mole", "millimole", "micromole" },
69
+ { "gramPerMole" },
70
+ { "molePerLiter", "millimolePerLiter" },
71
+ { "katal" },
72
+ { "candela" },
73
+ { "steradian" },
74
+ { "lumen" },
75
+ { "kilolux", "lux" },
76
+ { "gray", "milligray" },
77
+ { "kilometerPerLiter" },
78
+ },
79
+ pointDisplayUnits = {
80
+ { "celsius" },
81
+ },
82
+ })
83
+
84
+ --[=[
85
+ The units the United States and Britain share, which [MetricUnitSystems.US_CUSTOMARY] and
86
+ [MetricUnitSystems.IMPERIAL] fall back to before metric.
87
+
88
+ @prop US_CUSTOMARY_AND_IMPERIAL MetricUnitSystem
89
+ @within MetricUnitSystems
90
+ ]=]
91
+ MetricUnitSystems.US_CUSTOMARY_AND_IMPERIAL = MetricUnitSystem.new({
92
+ name = "usCustomaryAndImperial",
93
+ displayUnits = {
94
+ { "mile", "foot", "inch" },
95
+ { "squareMile", "acre", "squareFoot", "squareInch" },
96
+ { "milePerHour" },
97
+ { "footPerSecondSquared" },
98
+ { "poundForce" },
99
+ { "britishThermalUnit" },
100
+ { "horsepower" },
101
+ { "poundForcePerSquareInch" },
102
+ { "poundPerCubicFoot" },
103
+ { "fahrenheit" },
104
+ },
105
+ fallback = MetricUnitSystems.METRIC,
106
+ })
107
+
108
+ --[=[
109
+ United States customary units.
110
+
111
+ @prop US_CUSTOMARY MetricUnitSystem
112
+ @within MetricUnitSystems
113
+ ]=]
114
+ MetricUnitSystems.US_CUSTOMARY = MetricUnitSystem.new({
115
+ name = "usCustomary",
116
+ displayUnits = {
117
+ { "usGallon", "usQuart", "usCup", "usFluidOunce", "usTablespoon", "usTeaspoon" },
118
+ { "shortTon", "pound", "ounce" },
119
+ { "milePerUsGallon" },
120
+ },
121
+ fallback = MetricUnitSystems.US_CUSTOMARY_AND_IMPERIAL,
122
+ })
123
+
124
+ --[=[
125
+ British imperial units.
126
+
127
+ @prop IMPERIAL MetricUnitSystem
128
+ @within MetricUnitSystems
129
+ ]=]
130
+ MetricUnitSystems.IMPERIAL = MetricUnitSystem.new({
131
+ name = "imperial",
132
+ displayUnits = {
133
+ { "imperialGallon", "imperialPint", "imperialFluidOunce" },
134
+ { "stone", "pound", "ounce" },
135
+ { "milePerImperialGallon" },
136
+ },
137
+ fallback = MetricUnitSystems.US_CUSTOMARY_AND_IMPERIAL,
138
+ })
139
+
140
+ --[=[
141
+ The options for [MetricUnitSystems.roblox].
142
+
143
+ @interface RobloxSystemConfig
144
+ .name string
145
+ .metersPerStud number
146
+ .fallback MetricUnitSystem? -- defaults to METRIC
147
+ @within MetricUnitSystems
148
+ ]=]
149
+ export type RobloxSystemConfig = {
150
+ name: string,
151
+ metersPerStud: number,
152
+ fallback: MetricUnitSystem.MetricUnitSystem?,
153
+ }
154
+
155
+ --[=[
156
+ A Roblox system at a stud of `metersPerStud`: `stud`, `studPerSecond`, `studPerSecondSquared` and
157
+ `robloxMassUnit` at that scale, shown for lengths, speeds, accelerations and masses, and `fallback` (metric by
158
+ default) for everything else.
159
+
160
+ ```lua
161
+ local GIANT = MetricUnitSystems.roblox({ name = "robloxGiant", metersPerStud = 2.8 })
162
+ GIANT:New(10, "stud"):In("meter") -- 28
163
+ ```
164
+
165
+ @param config RobloxSystemConfig
166
+ @return MetricUnitSystem
167
+ ]=]
168
+ function MetricUnitSystems.roblox(config: RobloxSystemConfig): MetricUnitSystem.MetricUnitSystem
169
+ assert(type(config) == "table", "Bad config")
170
+ return MetricUnitSystem.new({
171
+ name = config.name,
172
+ units = MetricUnitDefinitions.createRobloxUnits(config.name, config.metersPerStud),
173
+ displayUnits = {
174
+ { "stud" },
175
+ { "studPerSecond" },
176
+ { "studPerSecondSquared" },
177
+ { "robloxMassUnit" },
178
+ },
179
+ fallback = config.fallback or MetricUnitSystems.METRIC,
180
+ })
181
+ end
182
+
183
+ --[=[
184
+ Studs against the default Roblox avatar, 28 cm each.
185
+
186
+ @prop ROBLOX MetricUnitSystem
187
+ @within MetricUnitSystems
188
+ ]=]
189
+ MetricUnitSystems.ROBLOX = MetricUnitSystems.roblox({
190
+ name = "roblox",
191
+ metersPerStud = MetricUnitDefinitions.ROBLOX_METERS_PER_STUD,
192
+ })
193
+
194
+ local WELL_KNOWN_BY_NAME: { [string]: MetricUnitSystem.MetricUnitSystem } = {}
195
+ for _, system in
196
+ {
197
+ MetricUnitSystems.METRIC,
198
+ MetricUnitSystems.US_CUSTOMARY_AND_IMPERIAL,
199
+ MetricUnitSystems.US_CUSTOMARY,
200
+ MetricUnitSystems.IMPERIAL,
201
+ MetricUnitSystems.ROBLOX,
202
+ }
203
+ do
204
+ WELL_KNOWN_BY_NAME[system:GetName()] = system
205
+ end
206
+ table.freeze(WELL_KNOWN_BY_NAME)
207
+
208
+ --[=[
209
+ The system that declares `unit`: a game's own system for its units, a well-known one such as `IMPERIAL` for
210
+ `imperialPint`, or nil for units every system shares, such as the second.
211
+
212
+ @param unit UnitDefinition
213
+ @return MetricUnitSystem?
214
+ ]=]
215
+ function MetricUnitSystems.getSystemOfUnit(
216
+ unit: MetricUnitDefinitions.UnitDefinition
217
+ ): MetricUnitSystem.MetricUnitSystem?
218
+ local declaring = MetricUnitSystem.getSystemDeclaring(unit)
219
+ if declaring then
220
+ return declaring
221
+ end
222
+ return if unit.system then WELL_KNOWN_BY_NAME[unit.system] else nil
223
+ end
224
+
225
+ return table.freeze(MetricUnitSystems)
@@ -0,0 +1,3 @@
1
+ return {
2
+ testMatch = { "**/*.spec" },
3
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "MetricutilsTest",
3
+ "globIgnorePaths": [
4
+ "**/.package-lock.json",
5
+ "**/.pnpm",
6
+ "**/.pnpm-workspace-state-v1.json",
7
+ "**/.modules.yaml",
8
+ "**/.ignored",
9
+ "**/.ignored_*"
10
+ ],
11
+ "tree": {
12
+ "$className": "DataModel",
13
+ "ServerScriptService": {
14
+ "$properties": {
15
+ "LoadStringEnabled": true
16
+ },
17
+ "metricutils": {
18
+ "$path": ".."
19
+ },
20
+ "Script": {
21
+ "$path": "scripts/Server"
22
+ }
23
+ }
24
+ }
25
+ }
@@ -0,0 +1,14 @@
1
+ --[[
2
+ @class ServerMain
3
+ ]]
4
+ local ServerScriptService = game:GetService("ServerScriptService")
5
+
6
+ local root = ServerScriptService.metricutils
7
+ local loader = root:FindFirstChild("LoaderUtils", true).Parent
8
+ local require = require(loader).bootstrapGame(root)
9
+
10
+ local NevermoreTestRunnerUtils = require("NevermoreTestRunnerUtils")
11
+ local results = NevermoreTestRunnerUtils.runTestsIfNeededAsync(root)
12
+ if results then
13
+ return results
14
+ end