# NpmRange

> Represents an npm version range evaluated with npm semantics on top of Composer's constraint engine.

[API reference](/docs/api/index.md) › [Foxy](/docs/extensions/foxy/reference/index.md) › [Native](/docs/extensions/foxy/reference/native/index.md) › NpmRange

| final readonly class | `Foxy\Native\NpmRange` |
| --- | --- |
| Implements | [`Stringable`](https://www.php.net/manual/en/class.stringable.php) |
| Source | [`src/Native/NpmRange.php`](https://github.com/php-forge/foxy/blob/main/src/Native/NpmRange.php#L32) |

Represents an npm version range evaluated with npm semantics on top of Composer's constraint engine.

Each `||` branch is rewritten into a Composer constraint (bare partials become wildcards, `~M.m` keeps the minor,
partial `>` and `<=` bounds are bumped, hyphen ranges become `>=` and `<=` pairs) and a prerelease version matches
a branch only when a comparator of that branch carries a prerelease on the same `major.minor.patch` tuple.
Wildcard majors with an operator (`>=*`) and prerelease tags Composer cannot normalize (`1.0.0-next.1`) are rejected.

See also: <https://github.com/npm/node-semver#ranges>.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__toString()`](#method-__toString) | Returns the original npm range, trimmed. | `NpmRange` |
| [`parse()`](#method-parse) static | Parses an npm range. | `NpmRange` |
| [`satisfies()`](#method-satisfies) | Returns whether a version satisfies the range. | `NpmRange` |

## Method details

### __toString() <span id="method-__toString"></span>

```php
public function __toString(): string
```

Returns the original npm range, trimmed.

**Returns**

`string`

### parse() <span id="method-parse"></span>

```php
public static function parse(string $range, string $package): self
```

Parses an npm range.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$range` | `string` | Npm range, such as `^1.2.3`, `1.x \|\| >=2.1 <3`, or `1.2 - 2.3`. |
| `$package` | `string` | Package name used in the error message. |

**Returns**

`self`

**Throws**

- [`Foxy\Exception\RuntimeException`](/docs/extensions/foxy/reference/exception/runtimeexception/index.md): if the range is not valid npm syntax or cannot be expressed as a Composer constraint.

### satisfies() <span id="method-satisfies"></span>

```php
public function satisfies(string $version): bool
```

Returns whether a version satisfies the range.

Build metadata is ignored; a version Composer cannot normalize never satisfies the range.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$version` | `string` | Version as published in the registry, such as `1.2.3` or `1.0.0-beta.1`. |

**Returns**

`bool`