Compare Versions and Match Semver Ranges
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
Write compareVersions(a, b), which returns -1, 0 or 1 as version a is lower than, equal to or higher than b. Versions are three whole numbers, major.minor.patch, optionally followed by "-" and a pre-release made of identifiers joined by dots, such as "1.0.0-beta.2". Compare major, then minor, then patch, as numbers. A version with a pre-release is lower than the same version without one. Two pre-releases are compared identifier by identifier: two numbers by value, a number below any identifier with letters, two others in plain string order; if one runs out first, it's the lower. Then write satisfies(version, range), for versions without a pre-release. A range is one or more comparators separated by spaces, all of which must hold: an exact version, such as "1.2.3"; an operator <, <=, >, >= or = written directly before a version; "~1.2.3", meaning at least 1.2.3 and below 1.3.0; or "^1.2.3", meaning at least 1.2.3 and below the next change to its first non-zero number, so below 2.0.0, while "^0.2.3" is below 0.3.0 and "^0.0.3" below 0.0.4.
Your code must pass
- Compares versions number by number
- Ranks a pre-release below its release
- Orders pre-release identifiers
- Matches exact versions and comparisons
- Matches caret ranges
- Matches tilde ranges