10aday › Version Constraint Checker › x, *, hyphen and || ranges

Wildcards (x, *), hyphen ranges and ||: npm range syntax next to Cargo, Composer, pip and RubyGems

Short answer: in npm, 1.x = 1.* = 1 = ≥1.0.0 <2.0.0; 1.2.3 - 2.3.4 includes both ends; a hyphen range with a partial upper bound (1.2 - 2.3) includes the whole 2.3.x line; || means “or”; a space means “and”. Cargo has no hyphen ranges and no || and uses a comma for “and”. Composer copies most of npm's syntax but reads partial versions as exact ones: 1.2 || 3 means “1.2.0 or 3.0.0”, and >1.2 already accepts 1.2.1.

Have a constraint and a list of versions? Paste them into the Version Constraint Checker: it shows, side by side, what npm, Cargo, Composer, pip and RubyGems accept.

Wildcards: x and *

pip spells wildcards ==1.*; RubyGems has no wildcard (use ~> 1.0).

ConstraintnpmCargoComposerpipRubyGems
*
pip and RubyGems: leave the constraint empty
any versionany versionany versionnot validnot validtry it
1.x
RubyGems reads “1.x” as a pre-release version number
≥1.0.0 <2.0.0≥1.0.0 <2.0.0≥1.0.0 <2.0.0not validno releasetry it
1.*1.*
≥1.0.0 <2.0.0
1.*
≥1.0.0 <2.0.0
1.*
≥1.0.0 <2.0.0
==1.*
≥1.0.0 <2.0.0
1.*
not valid
try it
1.2.x≥1.2.0 <1.3.0≥1.2.0 <1.3.0≥1.2.0 <1.3.0not validno releasetry it
1.2.*1.2.*
≥1.2.0 <1.3.0
1.2.*
≥1.2.0 <1.3.0
1.2.*
≥1.2.0 <1.3.0
==1.2.*
≥1.2.0 <1.3.0
1.2.*
not valid
try it

Hyphen ranges

Only npm and Composer have them; Cargo, pip and RubyGems reject 1.2.3 - 2.3.4.

ConstraintnpmComposerCargo
1.2.3 - 2.3.4
both ends included
≥1.2.3 ≤2.3.4≥1.2.3 ≤2.3.4not validtry it
1.2 - 2.3
partial upper bound: all of 2.3.x
≥1.2.0 <2.4.0≥1.2.0 <2.4.0not validtry it
1 - 2≥1.0.0 <3.0.0≥1.0.0 <3.0.0not validtry it

“Or” with ||

Highlighted: Composer reads a partial version as an exact one, and keeps its own tilde rule, so the same string allows different versions.

ConstraintnpmComposerCargo
^1.2 || ^3.0≥1.2.0 <2.0.0 or ≥3.0.0 <4.0.0≥1.2.0 <2.0.0 or ≥3.0.0 <4.0.0not validtry it
1.2 || 3≥1.2.0 <1.3.0 or ≥3.0.0 <4.0.0exactly 1.2.0 or exactly 3.0.0not validtry it
~1.2 || ~2.3≥1.2.0 <1.3.0 or ≥2.3.0 <2.4.0≥1.2.0 <2.0.0 or ≥2.3.0 <3.0.0not validtry it

“And”: space or comma

ConstraintnpmCargoComposerpipRubyGems
>=1.2 <2
space = and (npm, Composer)
≥1.2.0 <2.0.0not valid≥1.2.0 <2.0.0not validnot validtry it
>=1.2, <2
comma = and (all but npm)
not valid≥1.2.0 <2.0.0≥1.2.0 <2.0.0≥1.2.0 <2.0.0≥1.2.0 <2.0.0try it
>=1.2, !=1.2.3
npm and Cargo have no !=
not validnot valid≥1.2.0, not 1.2.3≥1.2.0, not 1.2.3≥1.2.0, not 1.2.3try it

The partial-version trap in comparisons

npm and Cargo expand a partial version to the whole line it names; Composer and RubyGems pad it with zeros.

ConstraintnpmCargoComposerpipRubyGems
>1.2≥1.3.0≥1.3.0>1.2.0>1.2.0>1.2.0try it
<=2.3<2.4.0<2.4.0≤2.3.0≤2.3.0≤2.3.0try it

Ranges shown for release versions. Pre-releases follow extra rules in each tool; the checker shows them.

Common questions

What does 1.x mean in package.json?

1.x, 1.* and a bare 1 all mean ≥1.0.0 <2.0.0 in npm. 1.2.x means ≥1.2.0 <1.3.0. A lone * or x means any release version.

Are npm hyphen ranges inclusive?

Yes. 1.2.3 - 2.3.4 means ≥1.2.3 ≤2.3.4. If the upper bound is partial, every version it names is included: 1.2 - 2.3 means ≥1.2.0 <2.4.0, so 2.3.9 matches.

How do I write "and" and "or" in a version range?

npm: a space is “and” (>=1.2 <2), || is “or”. Composer: space or comma for “and”, || for “or”. Cargo, pip and RubyGems: comma for “and”, no “or” at all.

Does >1.2 accept 1.2.5?

Not in npm or Cargo: a partial version stands for the whole 1.2.x line, so >1.2 starts at 1.3.0. In Composer, >1.2 means >1.2.0, so 1.2.5 matches. Write three numbers to avoid the ambiguity.

Related

Sources