Functional Weave
Code in TypeScript

units.parse-quantity@1.0.0

vectors.json

4,148 bytes · view raw

[
  { "name": "a decimal and a symbol", "args": ["2.5 kg"], "expect": { "value": "2.5", "unit": "kg", "dimension": "mass" } },
  { "name": "thousands separators are removed", "args": ["1,200 m"], "expect": { "value": "1200", "unit": "m", "dimension": "length" } },
  { "name": "feet and inches sum exactly into inches", "args": ["12 ft 6 in"], "expect": { "value": "150", "unit": "in", "dimension": "length" } },
  { "name": "prime and double-prime marks", "args": ["5'11\""], "expect": { "value": "71", "unit": "in", "dimension": "length" } },
  { "name": "a fraction on the last part is kept", "args": ["5 ft 11.5 in"], "expect": { "value": "71.5", "unit": "in", "dimension": "length" } },
  { "name": "pounds and ounces", "args": ["5 lb 3 oz"], "expect": { "value": "83", "unit": "oz", "dimension": "mass" } },
  { "name": "stones and pounds", "args": ["11 st 4 lb"], "expect": { "value": "158", "unit": "lb", "dimension": "mass" } },
  { "name": "metric compounds work too", "args": ["1 m 5 cm"], "expect": { "value": "105", "unit": "cm", "dimension": "length" } },
  { "name": "a negative sign applies to the whole compound", "args": ["-5 ft 3 in"], "expect": { "value": "-63", "unit": "in", "dimension": "length" } },
  { "name": "word units in any letter case, trailing zeros trimmed", "args": ["  2.50 Kilograms "], "expect": { "value": "2.5", "unit": "kg", "dimension": "mass" } },
  { "name": "degree sign temperature, negative", "args": ["-40 °F"], "expect": { "value": "-40", "unit": "degF", "dimension": "temperature" } },
  { "name": "grouped digits with a fraction", "args": ["12,345,678.90 mL"], "expect": { "value": "12345678.9", "unit": "mL", "dimension": "volume" } },
  { "name": "a two-word unit", "args": ["8 US fl oz"], "expect": { "value": "8", "unit": "floz_us", "dimension": "volume" } },
  { "name": "no space between number and unit, leading zeros dropped", "args": ["007kWh"], "expect": { "value": "7", "unit": "kWh", "dimension": "energy" } },
  { "name": "negative zero is zero", "args": ["-0.0 kg"], "expect": { "value": "0", "unit": "kg", "dimension": "mass" } },
  { "name": "square feet", "args": ["3 sq ft"], "expect": { "value": "3", "unit": "ft2", "dimension": "area" } },
  { "name": "empty text", "args": ["   "], "expectError": "quantity text is empty" },
  { "name": "a unit with no number", "args": ["kg"], "expectError": "expected a number at \"kg\"" },
  { "name": "a number with no unit", "args": ["12"], "expectError": "missing unit after \"12\"" },
  { "name": "bad thousands grouping", "args": ["1,20 m"], "expectError": "bad thousands grouping in \"1,20\"" },
  { "name": "two decimal points", "args": ["1.2.3 m"], "expectError": "malformed number \"1.2.3\"" },
  { "name": "unknown unit", "args": ["3 furlongs"], "expectError": "unknown unit \"furlongs\"" },
  { "name": "a bare gallon is ambiguous", "args": ["2 gal"], "expectError": "ambiguous unit \"gal\": use gal_us or gal_imp" },
  { "name": "food Calorie is not the calorie symbol, whatever the case", "args": ["250 CAL"], "expectError": "ambiguous unit \"CAL\": use cal (thermochemical calorie) or kcal (food Calorie)" },
  { "name": "smallest unit first is refused", "args": ["6 in 12 ft"], "expectError": "units must run from largest to smallest: \"in\" cannot come before \"ft\"" },
  { "name": "mixed dimensions are refused", "args": ["5 ft 3 lb"], "expectError": "cannot combine ft (length) and lb (mass) in one quantity" },
  { "name": "mixed systems cannot sum exactly", "args": ["1 ft 2 cm"], "expectError": "cannot combine ft and cm exactly: 1 ft is not a whole number of cm" },
  { "name": "a fraction before the last part is refused", "args": ["5.5 ft 3 in"], "expectError": "only the last number of a compound quantity may have a fraction" },
  { "name": "a minus sign inside a compound is refused", "args": ["5 ft -3 in"], "expectError": "only the first number may be negative" },
  { "name": "temperatures are never compound", "args": ["10 degC 5 K"], "expectError": "temperatures cannot be compound" },
  { "name": "more than 15 significant digits", "args": ["1234567890123456 m"], "expectError": "has too many digits" }
]