https://github.com/willfarrell/node-semver-compare-range
Comparator for semver sorting.
https://github.com/willfarrell/node-semver-compare-range
Last synced: 10 months ago
JSON representation
Comparator for semver sorting.
- Host: GitHub
- URL: https://github.com/willfarrell/node-semver-compare-range
- Owner: willfarrell
- License: mit
- Created: 2016-03-01T06:19:13.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-06-25T14:01:09.000Z (over 2 years ago)
- Last Synced: 2025-04-21T02:20:46.061Z (11 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 5
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# semver compare
[](https://travis-ci.org/willfarrell/node-semver-compare-range)
[](https://gemnasium.com/willfarrell/node-semver-compare-range)
[](https://coveralls.io/github/willfarrell/node-semver-compare-range?branch=master)
[](https://nodei.co/npm/semver-compare-range/)
Compare two semver version or version range strings, returning -1,0,1. To be used by passing into `[].sort()`. Developed to order versioned sql files to ensure proper order of execution.
## Example
```javascript
var cmp = require('semver-compare-range');
var versions = [
'1.0.0',
'0.0.1 - 0.0.9',
'1.1.0 - 1.1.0',
'0.0.0 - 0.0.1',
'0.0.0',
'0.0.9 - 1.0.0',
];
console.log(versions.sort(cmp).join('\n'));
```
### Example Return
```
0.0.0
0.0.0 - 0.0.1
0.0.1 - 0.0.9
0.0.9 - 1.0.0
1.0.0
1.0.0 - 1.1.0
```
## Methods
`var cmp = require('semver-compare-range');`
### cmp(a,b)
- `*` or `(empty string)` will return `-1`.
- Invalid version will return `0`.
- `a < b` return `-1`; `a == b` return `0`; `a > b` return `1`.
- When `a` is not a version range and `b` is a version range: `a <= min(b)` return `-1`; `a > min(b)` return `1`.
- When `a` and `b` are version ranges: `min(a) < min(b)` return `-1`; `min(a) > min(b)` return `1`; `min(a) == min(b) && max(a) < max(b)` return `-1`; `min(a) == min(b) && max(a) > max(b)` return `1`; `a == b` return `0`.
## Install
`npm install semver-compare-range`
## License
MIT