mergeRanges
Write an ES module exporting mergeRanges(ranges). Input is an array of [start, end] pairs of finite numbers with start <= end. Return new pairs sorted by start, merging overlapping or touching ranges. Empty input returns []. Do not mutate the input or its nested pairs. Throw TypeError for a non-array, a malformed pair, a non-finite number or start > end. No packages, filesystem, network or top-level side effects. Return the complete function in one JavaScript code block, followed by a short complexity explanation.

latestById
Write an ES module exporting latestById(records). Each record has a string id and a non-negative safe-integer timestamp, plus arbitrary other fields. Return one record for each id: the greatest timestamp wins; for equal timestamps the last occurrence wins. Preserve the order in which each id first appeared. Empty input returns []. Do not mutate the array or records. References to original records in the result are allowed. Inputs satisfy this contract; additional validation is not required. Handle ids such as "__proto__" and "constructor" as ordinary ids. No packages, filesystem, network or top-level side effects. Return the complete function in one JavaScript code block and explain how you handle ties.

parseDuration
Fix this function and export parseDuration(text) from an ES module. Accept exactly five ASCII characters in HH:MM format, with hours 00 through 23 and minutes 00 through 59. Return total minutes as a number. Return null for every other input, including non-strings, whitespace, one-digit hours, trailing characters and a trailing newline. No packages, filesystem, network or top-level side effects. Return the full replacement function in one JavaScript code block and explain the original bugs.

function parseDuration(text) {
  const [hours, minutes] = text.split(":").map(Number);
  return hours * 60 + minutes;
}
