Skip to content

v0.4.0 — coerce text, place versions

Choose a tag to compare

@TYEclipse TYEclipse released this 11 Oct 21:58
· 2 commits to main since this release

dsh-semver v0.4.0 — coerce text, place versions

Two new tools, both anchored to npm's own semver package.

semver_coerce — pull the first version-like number out of arbitrary text (npm semver.coerce):
"node v18.16.1" → 18.16.1, "42" → 42.0.0, "1.2" → 1.2.0, "1.2.3.4" → 1.2.3, "x.y.z" → no version.
It reports the two "first number wins" traps instead of hiding them: a changelog line coerces to its date
("changelog 2026-10-12: 4.5.6 released" → 2026.0.0) and a commit sha to 4.0.0. A component above
Number.MAX_SAFE_INTEGER is reported as no version with a reason — npm's parse() swallows that error into
null — never as a rounded number.

semver_outside — is a version above everything a range accepts, below everything, inside it, or in the gap
between alternatives? (npm semver.gtr / semver.ltr, ported comparator by comparator.) Four outcomes, no forced
direction: 2.0.0 against ^1.0.0 || ^3.0.0 is neither inside nor above nor below, and the tool names that gap.
npm's tuple-lock quirk is surfaced rather than smoothed over: a prerelease never satisfies a plain range, so npm
calls 1.2.3-beta.1 both above and below >=1.2.0 <2.0.0.

Testing: 132 → 154 tests, coverage 91.47% → 92.55%. The anchors now also cover npm's comparator shape —
54 ranges compared against new Range(r).set comparator for comparator, because the -0 upper bound npm
generates for ^1.2.3 (>=1.2.3 <2.0.0-0) is load-bearing for gtr/ltr — plus 28 coerce cases and 864
gtr/ltr cases. The oracle self-check re-derives every anchor from npm and requires byte equality with the
committed file; one of its own invariants was wrong when first written (it claimed a version can never be both
above and below a range), and the corpus showed npm really does that for tuple-locked prereleases.


中文说明

semver_coerce:从任意文本里抓出第一个版本号(npm semver.coerce)——"node v18.16.1" → 18.16.1、
"42" → 42.0.0、"1.2.3.4" → 1.2.3、"x.y.z" → 没有版本。两个「会骗人」的情形照实呈现:changelog 行
抓到的是日期(2026.0.0),commit sha 抓成 4.0.0;16 位档位超上限时报「没有版本」并给原因
(npm 的 parse() 把异常吞成 null),绝不返回四舍五入的数字。

semver_outside:版本相对整个范围的位置(npm semver.gtr/semver.ltr 逐比较符移植)——高于全部、
低于全部、在范围内,或落在两个分支之间的空隙(2.0.0 对 ^1.0.0 || ^3.0.0:不在范围内,两个方向也都是
false)。三个布尔分开给,不硬凑方向。

测试 132 → 154、覆盖率 91.47% → 92.55%;锚点新增 npm 的比较符形状对拍(54 个范围与 new Range(r).set
逐条比对,^1.2.3 上界的 -0 是判定的关键)、28 条 coerce 用例、864 条 gtr/ltr 用例。

安装 / Install

dsh plugin --profile web add github:TYEclipse/dsh-semver#v0.4.0