Repository navigation
Conversation
Most versions seen by npm are plain `X.Y.Z`. They are now read in one pass from the end of the string (digits and two dots, no leading zeros, up to 9 digits per part); anything else is rejected after a character or two and goes to the regular expression as before, so results never differ. Also replaces the per-identifier `/^[0-9]+$/` test in the prerelease map with a char-code loop and `String#match` with `RegExp#exec`.
Range and Comparator ran every normalization step on every input, even when the string had no whitespace, no `+`, `^`, `~` or `*`. A shared, conservative pre-check (every character `\s` matches is <= 0x20 or >= 0xa0) now skips the trim/split/replace passes that cannot change the string, `||` is only split when present, and a range with a single comparator is built directly. Inputs that might contain whitespace still go through the exact normalization, so results are unchanged.
Comparator.test went through cmp(), which re-wraps both sides in SemVer
and dispatches on the operator string. For the five operators the parser
produces ('', '>', '>=', '<', '<=') it now calls SemVer#compare directly;
any other operator still goes through cmp(), so a hand-set '!=' or an
invalid operator behaves (and throws) exactly as before.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three independent commits. Each passes
npm test(100% coverage) andnpm run linton its own, so any of them can be reviewed, reordered or dropped separately. No API or behaviour change, and the LRU cache is untouched (see #893).fix: parse plain X.Y.Z versions without the regular expressionX.Y.Z. They are now read in one backward pass: digits and two dots, no leading zeros, at most 9 digits per part./^[0-9]+$/test becomes a char-code loop;String#matchbecomesRegExp#exec.fix: skip whitespace and regex passes that cannot change a range\smatches is<= 0x20or>= 0xa0) now skips trim/split/replace passes that cannot change the string.||is only split when it is present.fix: compare directly in Comparator.test for parsed operatorstest()callsSemVer#comparedirectly instead of going throughcmp().'!=', or an invalid one) still goes throughcmp()and behaves and throws as before.Benchmarks
main= b37b033.benchmarks/bench-parse.js(ops/sec, this branch / main)parse(1.0.0),parse(2.1.0),parse(3.2.1)parse(v1.2.3)parse(v1.2.3)is 1.00 to 1.04x,parse(xyz)0.97 to 1.00x, andparse(1.2.3-123)1.07 to 1.11x.benchmarks/bench-satisfies.js: 1.42 to 1.62x across all 12 cases (one run).A real dependency tree. #893 asked how changes like this behave on a real install, so I measured that too:
npm installhere: 916 packages, 1,675 edges, 395 distinct ranges).satisfies(installedVersion, declaredRange), which is roughly what validating a tree does.Scripts for the real-tree numbers
node corpus.mjs edges.json .builds the edge list from./node_modules;node bench-real.mjs edges.json 15 <path-to-main> <path-to-branch>runs the comparison.Correctness
npm testpasses 55 suites with 23,502 assertions at 100% coverage;npm run lintis clean.main:parse,valid,compare,new Range().range,satisfiesandComparator#test.looseandincludePrerelease.vprefix, unicode whitespace, invalid input, the 9/10-digit boundary andMAX_SAFE_INTEGER).MAX_SAFE_INTEGERcheck; longer numbers take the regex path as before (tested at 9, 10 and 16 digits).hasMaybeWhitespaceis deliberately conservative. A test checks that every character inWHITESPACE_CHARACTERSis caught.How this was made
I built this with AI coding agents. They worked inside a harness that only accepts a change when the test suite stays green and the change matches unmodified
mainon the differential check above. I then reviewed every line myself. I'm happy to split this, drop a commit, or rework anything.