Skip to content

Commit 207d3ad

Browse files
committed
docs: add documentation for expanded :semver selector
1 parent ce7acf6 commit 207d3ad

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

docs/content/using-npm/dependency-selectors.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,31 @@ The [`npm query`](/commands/npm-query) commmand exposes a new dependency selecto
5858
- `:extraneous` when a dependency exists but is not defined as a dependency of any node
5959
- `:invalid` when a dependency version is out of its ancestors specified range
6060
- `:missing` when a dependency is not found on disk
61-
- `:semver(<spec>)` matching a valid [`node-semver`](https://github.com/npm/node-semver) spec
61+
- `:semver([spec], [selector], [function])` match a valid [`node-semver`](https://github.com/npm/node-semver) version or range to an optional selector (defaults to `[version]`) and providing an optional semver function name
6262
- `:path(<path>)` [glob](https://www.npmjs.com/package/glob) matching based on dependencies path relative to the project
6363
- `:type(<type>)` [based on currently recognized types](https://github.com/npm/npm-package-arg#result-object)
6464

65+
##### `:semver([spec], [selector], [function])`
66+
67+
The `:semver()` pseudo selector allows comparing fields from each node's `package.json` using [semver](https://github.com/npm/node-semver#readme) methods. It accepts up to 3 parameters, all of which are optional.
68+
69+
- `spec` a semver version or range (no default)
70+
- `selector` an attribute selector for each node
71+
- `function` a semver method to apply, one of: `satisfies`, `intersects`, `subset`, `gt`, `gte`, `gtr`, `lt`, `lte`, `ltr`, `eq` or `neq`
72+
73+
If no `spec` is provided this selector will return every node that defines a `version`.
74+
75+
If only a `spec` is provided `selector` defaults to `[version]` and `function` to `satisfies`.
76+
77+
If a `spec` and a `selector` are provided, the `function` will default to one of `satisfies`, `intersects` or `eq` depending on the value of the `spec` as well as the resulting value of the `selector`. If both values are versions `eq` is used. If both values are ranges `intersects` is used. If the values are mixed types `satisfies` is used. Any other semver method must be passed explicitly.
78+
79+
Some examples:
80+
81+
- `:semver()` returns every node that has a defined `version`
82+
- `:semver(^1.0.0)` returns every node that has a `version` satisfied by the provided range `^1.0.0`
83+
- `:semver(16.0.0, :attr(engines, [node]))` returns every node which has an `engines.node` property satisfying the version `16.0.0`
84+
- `:semver(1.0.0, [version], lt)` every node with a `version` less than `1.0.0`
85+
6586
#### [Attribute Selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors)
6687

6788
The attribute selector evaluates the key/value pairs in `package.json` if they are `String`s.

0 commit comments

Comments
 (0)