Functional Weave
Code in TypeScript

monitor.format-duration@1.0.0

vectors.json

1,751 bytes · view raw

[
  {"name": "zero is 0s, not an empty string", "args": [0, 2], "expect": "0s"},
  {"name": "under a minute is seconds only", "args": [45, 2], "expect": "45s"},
  {"name": "three hours twelve minutes", "args": [11520, 2], "expect": "3h 12m"},
  {"name": "two days four hours", "args": [187200, 2], "expect": "2d 4h"},
  {"name": "zero minutes are dropped and the seconds are beyond two units, so 1h", "args": [3605, 2], "expect": "1h"},
  {"name": "with three units the seconds come back and the zero minutes stay out", "args": [3605, 3], "expect": "1h 5s"},
  {"name": "every unit", "args": [90061, 4], "expect": "1d 1h 1m 1s"},
  {"name": "one unit only", "args": [90061, 1], "expect": "1d"},
  {"name": "truncates: one second short of a day is 23h, never 1d", "args": [86399, 1], "expect": "23h"},
  {"name": "one second short of a day in full", "args": [86399, 4], "expect": "23h 59m 59s"},
  {"name": "exactly a minute", "args": [60, 4], "expect": "1m"},
  {"name": "a million seconds to three units", "args": [1000000, 3], "expect": "11d 13h 46m"},
  {"name": "days never roll up into years", "args": [34560000, 2], "expect": "400d"},
  {"name": "negative seconds are an error", "args": [-1, 2], "expectError": "seconds must not be negative, received -1"},
  {"name": "fractional seconds are an error", "args": [1.5, 2], "expectError": "seconds must be a whole number, received 1.5"},
  {"name": "zero units is an error", "args": [60, 0], "expectError": "maxUnits must be 1 to 4, received 0"},
  {"name": "five units is an error: there are only four", "args": [60, 5], "expectError": "maxUnits must be 1 to 4, received 5"},
  {"name": "fractional units are an error", "args": [60, 2.5], "expectError": "maxUnits must be 1 to 4, received 2.5"}
]