{"ScriptPreparationCode":"var versionString1 = \u00271-999745,999830-999864\u0027;\r\nvar versionString2 = \u0027999746-999829,999865-999979\u0027;\r\nvar validInputRegex = /^\\s*(\\d\u002B(-\\d\u002B)?)(,\\s*\\d\u002B(-\\d\u002B)?)*\\s*$/;","TestCases":[{"Name":"Exhaustive list of version numbers","Code":"function mergeVersionStrings(versions1, versions2) {\r\n const parsedVersions1 = parseVersions(versions1);\r\n const parsedVersions2 = parseVersions(versions2);\r\n return generateVersionsString(sortAndRemoveDuplicates([...parsedVersions1, ...parsedVersions2]));\r\n}\r\n\r\nfunction parseVersions(versions) {\r\n // Check if the input string matches any of the expected formats\r\n if (!versions || versions.trim().length == 0 || versions === \u0027undefined\u0027) {\r\n return [];\r\n }\r\n\r\n if (!versions.match(validInputRegex)) {\r\n throw new Error(\u0060Invalid input format. Expected format: \u00273\u0027 or \u00271,2,3\u0027 or \u00271-3\u0027. Received: ${versions}\u0060);\r\n\r\n }\r\n\r\n const result = [];\r\n const sections = versions.split(\u0027,\u0027);\r\n\r\n for (const section of sections) {\r\n if (section.includes(\u0027-\u0027)) {\r\n const [start, end] = section.split(\u0027-\u0027).map(Number);\r\n for (let i = start; i \u003C= end; i\u002B\u002B) {\r\n result.push(i);\r\n }\r\n } else {\r\n result.push(Number(section));\r\n }\r\n }\r\n\r\n return result;\r\n\r\n}\r\n\r\nfunction sortAndRemoveDuplicates(versions) {\r\n return Array.from(new Set(versions)).sort((a, b) =\u003E a - b);\r\n}\r\n\r\nfunction generateVersionsString(versions) {\r\n if (versions.length === 0) {\r\n return \u0027\u0027;\r\n }\r\n let result = \u0027\u0027;\r\n let normalizedVersions = sortAndRemoveDuplicates(versions);\r\n let rangeStart = normalizedVersions[0];\r\n let rangeEnd = normalizedVersions[0];\r\n\r\n for (let i = 1; i \u003C normalizedVersions.length; i\u002B\u002B) {\r\n if (normalizedVersions[i] === rangeEnd \u002B 1) {\r\n rangeEnd = normalizedVersions[i];\r\n } else {\r\n result \u002B= (rangeStart === rangeEnd ? rangeStart : \u0060${rangeStart}-${rangeEnd}\u0060) \u002B \u0027,\u0027;\r\n rangeStart = normalizedVersions[i];\r\n rangeEnd = normalizedVersions[i];\r\n }\r\n }\r\n\r\n result \u002B= (rangeStart === rangeEnd ? rangeStart : \u0060${rangeStart}-${rangeEnd}\u0060);\r\n return result;\r\n}\r\nmergeVersionStrings(versionString1, versionString2);","IsDeferred":false},{"Name":"Handle only specified ranges","Code":"function mergeVersionStrings(a, b) {\r\n const rangesA = parseVersionString(a);\r\n const rangesB = parseVersionString(b);\r\n const mergedRanges = optimizeRanges(rangesA.concat(rangesB));\r\n return formatVersionString(mergedRanges);\r\n}\r\n\r\nfunction parseVersionString(versionString) {\r\n const ranges = [];\r\n if (!versionString || !versionString.trim()) {\r\n return ranges;\r\n }\r\n const parts = versionString.split(\u0027,\u0027);\r\n for (const part of parts) {\r\n if (part.includes(\u0027-\u0027)) {\r\n const [start, end] = part.split(\u0027-\u0027).map(Number);\r\n ranges.push({start, end});\r\n } else {\r\n const num = Number(part);\r\n ranges.push({start: num, end: num});\r\n }\r\n }\r\n return optimizeRanges(ranges);\r\n}\r\n\r\nfunction optimizeRanges(ranges) {\r\n return ranges\r\n .sort((a, b) =\u003E a.start - b.start)\r\n .reduce((optimized, current) =\u003E {\r\n const last = optimized[optimized.length - 1];\r\n if (!last || current.start \u003E last.end \u002B 1) {\r\n optimized.push(current);\r\n } else {\r\n last.end = Math.max(last.end, current.end);\r\n }\r\n return optimized;\r\n }, []);\r\n}\r\n\r\nfunction formatVersionString(ranges) {\r\n return ranges\r\n .map((range) =\u003E (range.start === range.end ? \u0060${range.start}\u0060 : \u0060${range.start}-${range.end}\u0060))\r\n .join(\u0027,\u0027);\r\n}\r\nmergeVersionStrings(versionString1, versionString2);","IsDeferred":false}]}