Skip to content

Commit

Permalink
feat(satisfies): add support for npm hypen ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
omichelsen committed Jun 28, 2023
1 parent 6376c8f commit 5905d3d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ satisfies('10.1.1', '<=10.2.2'); // true
satisfies('10.1.1', '>=10.2.2'); // false
satisfies('1.4.6', '1.2.7 || >=1.2.9 <2.0.0'); // true
satisfies('1.2.8', '1.2.7 || >=1.2.9 <2.0.0'); // false
satisfies('1.5.1', '1.2.3 - 2.3.4'); // true
satisfies('2.3.5', '1.2.3 - 2.3.4'); // false
```

### Validate version numbers
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export const satisfies = (version: string, range: string): boolean => {
// handle multiple comparators
if (range.includes('||')) {
return range.split('||').some((r) => satisfies(version, r));
} else if (range.includes(' - ')) {
const [a, b] = range.split(' - ', 2);
return satisfies(version, `>=${a} <=${b}`);
} else if (range.includes(' ')) {
return range
.trim()
Expand Down
5 changes: 5 additions & 0 deletions test/satisfies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ describe('satisfies versions', () => {
['2.0.0', '1.2.7 || >=1.2.9 <2.0.0', false],
['1.4.6', ' 1.2.7 || >=1.2.9 <2.0.0 ', true],
['2.0.0', ' 1.2.7 || >=1.2.9 <2.0.0 ', false],
['1.0.0', '1.2.3 - 2.3.4', false],
['1.2.3', '1.2.3 - 2.3.4', true],
['1.5.1', '1.2.3 - 2.3.4', true],
['2.3.4', '1.2.3 - 2.3.4', true],
['2.4.0', '1.2.3 - 2.3.4', false],
]);
});
});

0 comments on commit 5905d3d

Please sign in to comment.