Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compare-versions

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compare-versions - npm Package Compare versions

Comparing version 3.6.0 to 4.0.0

index.mjs

12

package.json
{
"name": "compare-versions",
"version": "3.6.0",
"version": "4.0.0",
"description": "Compare semver version strings to find greater, equal or lesser.",

@@ -23,13 +23,17 @@ "repository": {

"scripts": {
"test": "nyc mocha"
"test": "c8 mocha"
},
"main": "index.js",
"module": "index.mjs",
"type": "module",
"types": "index.d.ts",
"files": [
"index.js",
"index.mjs",
"index.d.ts"
],
"devDependencies": {
"mocha": "^7.0.1",
"nyc": "^15.0.0"
"c8": "^7.10.0",
"mocha": "^9.0.0"
}
}

@@ -7,8 +7,8 @@ # compare-versions

Compare [semver](https://semver.org/) version strings to find greater, equal or lesser. Runs in the browser as well as Node.js/React Native etc. Has no dependencies and is tiny (~630 bytes gzipped).
Compare [semver](https://semver.org/) version strings to find greater, equal or lesser. Runs in the browser as well as Node.js/React Native etc. Has no dependencies and is tiny.
This library supports the full semver specification, including comparing versions with different number of digits like `1.0.0`, `1.0`, `1`, and pre-release versions like `1.0.0-alpha`. Additionally supports the following variations:
Supports the full semver specification including versions with different number of digits like `1.0.0`, `1.0`, `1` and pre-releases like `1.0.0-alpha`. Additionally supports the following variations:
- Supports wildcards for minor and patch version like `1.0.x` or `1.0.*`.
- Supports [Chromium version numbers](https://www.chromium.org/developers/version-numbers) with 4 parts, e.g. version `25.0.1364.126`.
- Wildcards for minor and patch version like `1.0.x` or `1.0.*`.
- [Chromium version numbers](https://www.chromium.org/developers/version-numbers) with 4 parts, e.g. version `25.0.1364.126`.
- Any leading `v` is ignored, e.g. `v1.0` is interpreted as `1.0`.

@@ -23,26 +23,20 @@ - Leading zero is ignored, e.g. `1.01.1` is interpreted as `1.1.1`.

Note: Starting from v4 this library includes a ESM version which will automatically be selected by your bundler (webpack, parcel etc). The old CJS version is `index.js` and the new ESM version is `index.mjs`.
## Usage
### Import
Will return `1` if first version is greater, `0` if versions are equal, and `-1` if the second version is greater:
```javascript
// ES6/TypeScript
import compareVersions from 'compare-versions';
// Node
var compareVersions = require('compare-versions');
compareVersions('11.1.1', '10.0.0'); // 1
compareVersions('10.0.0', '10.0.0'); // 0
compareVersions('10.0.0', '11.1.1'); // -1
```
### Compare
```javascript
compareVersions('10.1.8', '10.0.4'); // 1
compareVersions('10.0.1', '10.0.1'); // 0
compareVersions('10.1.1', '10.2.2'); // -1
```
Can also be used for sorting:
```javascript
var versions = [
const versions = [
'1.5.19',

@@ -52,3 +46,3 @@ '1.2.3',

]
var sorted = versions.sort(compareVersions);
const sorted = versions.sort(compareVersions);
/*

@@ -61,3 +55,3 @@ [

*/
var sortDescending = versions.sort(compareVersions).reverse();
const sortDescending = versions.sort(compareVersions).reverse();
/*

@@ -74,11 +68,12 @@ [

The normal compare function doesn't return a self-explanatory value (using `1`, `0` and `-1`).
This version returns the boolean which fulfills the specified operator.
The alternative `compare` function accepts an operator which will be more familiar to humans:
```js
compareVersions.compare('10.1.8', '10.0.4', '>'); // return true
compareVersions.compare('10.0.1', '10.0.1', '='); // return true
compareVersions.compare('10.1.1', '10.2.2', '<'); // return true
compareVersions.compare('10.1.1', '10.2.2', '<='); // return true
compareVersions.compare('10.1.1', '10.2.2', '>='); // return false
import { compare } from 'compare-versions';
compare('10.1.8', '10.0.4', '>'); // true
compare('10.0.1', '10.0.1', '='); // true
compare('10.1.1', '10.2.2', '<'); // true
compare('10.1.1', '10.2.2', '<='); // true
compare('10.1.1', '10.2.2', '>='); // false
```

@@ -88,8 +83,10 @@

Applies the same ruleset as used before comparing version numbers and returns a boolean:
Applies the same ruleset used comparing version numbers and returns a boolean:
```javascript
compareVersions.validate('1.0.0-rc.1'); // return true
compareVersions.validate('1.0-rc.1'); // return false
compareVersions.validate('foo'); // return false
import { validate } from 'compare-versions';
validate('1.0.0-rc.1'); // true
validate('1.0-rc.1'); // false
validate('foo'); // false
```

@@ -104,4 +101,6 @@

<script>
window.compareVersions('10.0.0', '10.1.0');
window.compareVersions('11.0.0', '10.0.0');
window.compareVersions.compare('11.0.0', '10.0.0', '>');
window.compareVersions.validate('11.0.0');
</script>
```

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc