Skip to content

Commit

Permalink
feat(satisfies): support malformed range input
Browse files Browse the repository at this point in the history
  • Loading branch information
omichelsen committed Jul 7, 2023
1 parent 282de66 commit 9e5b7a9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export const compare = (v1: string, v2: string, operator: CompareOperator) => {
* ```
*/
export const satisfies = (version: string, range: string): boolean => {
// clean input
range = range.replace(/([><=]+)\s+/g, '$1');

// handle multiple comparators
if (range.includes('||')) {
return range.split('||').some((r) => satisfies(version, r));
Expand Down
12 changes: 12 additions & 0 deletions test/satisfies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,16 @@ describe('satisfies versions', () => {
['2.4.0', '1.2.3 - 2.3.4', false],
]);
});

describe('malformed input - https://docs.npmjs.com/cli/v6/using-npm/semver#hyphen-ranges-xyz---abc', () => {
runTests([
['1.2.3', '> 1.2.3', false],
['1.2.4', '> 1.2.3', true],
['1.2.3', '> 1.2.3 < 1.2.5', false],
['1.2.4', '> 1.2.3 < 1.2.5', true],
['0.0.0', '> 1.2.3 <= 1.2.5 || 0.0.0', true],
['1.2.5', '> 1.2.3 <= 1.2.5 || 0.0.0', true],
['1.3.0', '> 1.2.3 <= 1.2.5 || 0.0.0', false],
]);
});
});

0 comments on commit 9e5b7a9

Please sign in to comment.