npm Semver Ranges
Hard · Tooling
npm decides which package versions to install from ranges like ^1.2.3 or >=2 <3 || 4.x. Implement satisfies(version, range) and maxSatisfying(versions, range) with the same rules as npm's semver package.
Versions are MAJOR.MINOR.PATCH, optionally with a v in front, a prerelease -alpha.1 and build metadata +build.5 (ignored when comparing). Anything else is invalid and never satisfies. Comparing: major, minor, patch numerically. A version with a prerelease is lower than the same version without one. Prerelease identifiers are compared one by one: numbers numerically, numbers lower than words, words in ASCII order, and a shorter list is lower when all shared identifiers are equal (1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta < 1.0.0-beta.2 < 1.0.0-beta.10 < 1.0.0).
Ranges: || separates alternatives (OR). Inside one, comparators separated by spaces must all match (AND). An operator may be followed by spaces (>= 1.2.0). Missing parts or x, X, * are wildcards. <V-0 below means "below V, excluding its prereleases".
- 1.2.3 or =1.2.3: exactly that version. <, <=, >, >= compare as usual.
- * or an empty range: any version. 1.x / 1: >=1.0.0 <2.0.0-0. 1.2.x / 1.2: >=1.2.0 <1.3.0-0.
- Partial versions after operators: >1.2 → >=1.3.0, >1 → >=2.0.0, <=1.2 → <1.3.0-0, <1.2 → <1.2.0-0, >=1.2 → >=1.2.0.
- ~1.2.3 → >=1.2.3 <1.3.0-0, ~1.2 → >=1.2.0 <1.3.0-0, ~1 → >=1.0.0 <2.0.0-0.
- ^ allows changes that do not modify the left-most non-zero part: ^1.2.3 → >=1.2.3 <2.0.0-0, ^0.2.3 → >=0.2.3 <0.3.0-0, ^0.0.3 → >=0.0.3 <0.0.4-0, ^1.2 → >=1.2.0 <2.0.0-0, ^0.2 → >=0.2.0 <0.3.0-0, ^0.0 → >=0.0.0 <0.1.0-0, ^1 → >=1.0.0 <2.0.0-0.
- Hyphen ranges A - B (spaces around -): >=A <=B. A partial A is filled with zeros. A partial B becomes an exclusive bound: 1.2.3 - 2.3 → >=1.2.3 <2.4.0-0, 1.2.3 - 2 → >=1.2.3 <3.0.0-0.
- Prereleases: a version with a prerelease matches an alternative only if all its comparators match and one of them has a prerelease on the same MAJOR.MINOR.PATCH (counting the -0 bounds too). So 1.3.0-beta does not satisfy >=1.2.0, but 1.2.3-beta.4 satisfies >=1.2.3-beta.2.
maxSatisfying returns the highest satisfying version string as it was given (the first one if several are equal), or null. The driver semverReport(range, versions) is already written.
Next: WebSocket Frame Decoder