PythonTypeScript
python-semver in TypeScript
The module-level functions of python-semver 3, in TypeScript, giving the answers Python gives. It ports python-semver 3 (commit 72734e6).
100%
24,876 of 24,876 calls recorded from the Python original answer the same in the port. Replayed again on .
What it ports
compare, match, parse, max_ver, min_ver, format_version, bump_major, bump_minor, bump_patch, bump_prerelease, bump_build, finalize_version, replace, Version.is_valid.
Install
The package is @pushkal-gupta/semver-py, version 0.1.0. It is not on npm yet; until it is, install it from the GitHub release. Needs Node 22 or later, or any runtime with ES2022 and BigInt.
npm install https://github.com/Pushkal-Gupta/shapeshift-semver-ts/releases/download/v0.1.0/pushkal-gupta-semver-py-0.1.0.tgzOnce it is on npm: npm install @pushkal-gupta/semver-py
Use it
import { compare, match, parse, bumpMinor, finalizeVersion, isValid } from "@pushkal-gupta/semver-py";
compare("1.0.0", "2.0.0"); // -1
match("2.3.7", ">=2.3.6"); // true
parse("1.2.3"); // { major: 1n, minor: 2n, patch: 3n, prerelease: null, build: null }
bumpMinor("1.2.3"); // "1.3.0"
finalizeVersion("1.2.3-rc.5"); // "1.2.3"
isValid("1.2.3"); // trueChecked by the documentation-example unit test in semver-ts/src/test/semver.test.ts.
Why use the port
Python's answers in JavaScript
A web page or a Node service orders and matches versions exactly as a Python backend using python-semver does, including Python's int() rules for version parts.
No Python at run time
The package has no runtime dependencies, and its library files import no Node built-in modules. It needs ES2022 and BigInt, nothing else.
Types
Written in strict TypeScript and shipped with declarations. Version numbers are bigint, so parts larger than 2^53 keep their exact value, as Python integers do.
Errors by name
Invalid input throws a PyError whose pyType is the exception Python raises, ValueError or TypeError, so error handling can follow the original.
Speed, as measured in the repository
100,000 compare() calls on the same 162 pairs of valid versions, best of five runs, run three times on one arm64 machine under macOS (bench/bench.py and bench/bench.mjs). Times for 100,000 calls; lower is faster. Other machines will differ.
| Run | Time |
|---|---|
| Python 3.12.3, semver.compare | 627 to 638 ms |
| Python 3.12.3, Version.parse(a).compare(b) | 525 to 534 ms |
| Node 24.21.0, the port's compare | 134 to 141 ms |
The tapes
A tape is a file of calls recorded from the original while it ran: the arguments, and the result or the exception type. A call matches when the port gives the same result or raises the same exception type. The counted tapes make up the headline figure.
| Tape | Where the calls came from | Calls | Match | Counted |
|---|---|---|---|---|
shadow/suite.jsonl | python-semver's own test suite and documentation examples | 159 | 159 | yes |
shadow/random.jsonl | random versions from fuzz/gen_tape.py, valid and invalid numbers, prerelease and build parts | 4,229 | 4,229 | yes |
shadow/holdout.jsonl | the same generator with another seed, recorded after the port matched and never used to change it | 20,112 | 20,112 | yes |
shadow/edge.jsonl | hand-picked edge cases: Unicode-digit prerelease identifiers, integers at the 10^255 limit, floats as parts | 376 | 376 | yes |
Replay it yourself
With pgremlin installed:
git clone https://github.com/Pushkal-Gupta/shapeshift-semver-ts
cd shapeshift-semver-ts
(cd semver-ts && npm ci && npm run build)
goblin shapeshift replay shadow/suite.jsonl -- node semver-ts/dist/adapter.js
goblin shapeshift replay shadow/random.jsonl -- node semver-ts/dist/adapter.js
goblin shapeshift replay shadow/holdout.jsonl -- node semver-ts/dist/adapter.js
goblin shapeshift replay shadow/edge.jsonl -- node semver-ts/dist/adapter.jsOr with the public checker, Shapeshifter, from the repository root:
shapeshifter replay shadow/suite.jsonl -- node semver-ts/dist/adapter.js
shapeshifter replay shadow/random.jsonl -- node semver-ts/dist/adapter.js
shapeshifter replay shadow/holdout.jsonl -- node semver-ts/dist/adapter.js
shapeshifter replay shadow/edge.jsonl -- node semver-ts/dist/adapter.jsWhat the tapes do not cover
- bytes arguments: JSON cannot carry them, so the port accepts strings only.
- Version objects passed as arguments, parse_version_info, the command-line functions and the Version methods no function uses.
- The deprecation warnings python-semver 3 emits for these functions.
- Unicode digits added after Unicode 15.0, which Node may know and Python 3.12 does not.
- NaN and infinity, which the recorder cannot write as JSON.
Links
- Repository, tapes and log: github.com/Pushkal-Gupta/shapeshift-semver-ts
- Release: shapeshift-semver-ts v0.1.0
- Licence: BSD-3-Clause, the licence of the original