Parse a Duration
Hard · Parsing
Write a function parseDuration(text) that turns a duration like "1h 30m" into a number of milliseconds. Return null if the text is not a valid duration.
- A duration has one or more parts. A part is a whole number directly followed by a unit: d (days), h (hours), m (minutes), s (seconds) or ms (milliseconds).
- Parts may be separated by spaces or written together: "1h30m" equals "1h 30m". Spaces at the start or end are fine.
- Units must go from largest to smallest, each used at most once: "30m 1h" and "1m 2m" are invalid.
- A number can be larger than its unit normally allows: "90m" is valid.
- Anything else is invalid: empty text, a number without a unit, unknown or uppercase units, a space inside a part ("1 h"), decimals, other characters.
Next: Get Value by Path