MetricUnitParseUtils
Reads measurements from text, for chat commands, admin tools, configs and text boxes. Returns plain MetricUnitData; MetricUnit.parse wraps it in a MetricUnit.
Accepts:
-
A number and a unit, with or without a space:
"5 km","5km","2.5 kilometers","45%","20 °C","20C","-3 °F","60 mph","1.5 MiB". A unit is any unit id, constructor name, symbol, English name (American or Commonwealth spelling), a common alias such as"kph"or"Mbps", or one of@quenty/time's names for a time unit. - Several of one dimension, added up:
"1h30m","1 hour, 30 minutes","5 ft 3 in". - A composed unit:
"5 m/s","1.6 MB/s","6 m·kg/s","9.81 m/s^2","5/d". -
Clock text:
"1:30"is one minute thirty (m:ss) and"1:30:00"one hour thirty (h:mm:ss), as MetricUnitFormatUtils writes them withdurationStyle = "clock". - An ISO 8601 duration without years or months, such as
"PT1H30M".
Text such as "m" can name more than one unit: the metre, and @quenty/time's minute. The rule is:
- With an expected
kind, the unit of that kind wins:parse("5 m", "time")is five minutes. - Otherwise, in text with several parts, the dimension every part can share wins:
"1m30s"is minutes. - Otherwise the SI symbol wins over
@quenty/time's alias:"5 m"is five metres.
Symbols match case exactly ("MB" is a megabyte, "Mb" is not); names match without case ("Kilometers").
Numbers use the locale's decimal mark when options.locale is given ("1,5 km" in de-de), else ..
No-break spaces read as spaces, so formatted text reads back.
Types
ParseOptions
interface ParseOptions {locale: string?--
Reads numbers with this locale's decimal mark and grouping, defaults to . and ,
}Options for MetricUnitParseUtils.parse.
Functions
parse
MetricUnitParseUtils.parse() → (string?)
Reads text as a measurement. Returns the measurement as plain data, or nil and a message suitable to show a
player. kind is what the caller expects, such as "length"; it settles ambiguous units and turns a mismatch or
a bare number into a helpful message.
MetricUnitParseUtils.parse("5 km") -- { amount = 5000, dimensions = { length = 1 } }
MetricUnitParseUtils.parse("5", "length") -- nil, 'Add a unit, such as "5 m"'
MetricUnitParseUtils.parse("5", "length", { defaultUnit = "meter" }) -- { amount = 5, dimensions = { length = 1 } }
MetricUnitParseUtils.parse("20 C") -- an absolute temperature
MetricUnitParseUtils.parse("20 C", "temperatureDifference") -- a difference of 20 K