@w6s/compare-versions

1.0.1 • Public • Published

compare-versions

A simple version comparison tool.

The comparison principle is very simple, the version string is converted into a string of the same length, the default is 10, and then directly determine the size. But if the version of the type is inconsistent, such as 3.13.1 and 3.14, may get unexpected results, So, please make sure the version type you entered is the same.

'3.13.1' => '0000000003.0000000013.0000000001'

Judgment rules and return results are as follows:

  • if a > b, return 1
  • if a === b, return 0
  • if a < b, return -1
  • if (versions type is inconsistent) return null

Install

npm install -S @w6s/compare-versions

Usage

Import

// ES6
import compareVersions from '@w6s/compare-versions';

// Node
const compareVersions = require('@w6s/compare-versions');

Compare

/**
 * compareVersions(a, b, c)
 * 
 * @param {string} a version
 * @param {string} b another version
 * @param {number} c string length, default 10
 */

compareVersions('3.13.1', '3.13.0', 10); //  1
compareVersions('3.14.1', '3.14.1', 2); //  0
compareVersions('3.14.1', '4.14.1'); // -1
compareVersions('3.14.1', '4.14'); // null

Author

License

MIT

Package Sidebar

Install

npm i @w6s/compare-versions

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

12.9 kB

Total Files

12

Last publish

Collaborators

  • q106357
  • muzi23
  • hejx