[Pkg-javascript-commits] [node-compare-versions] 01/02: New upstream version 3.1.0

Julien Puydt julien.puydt at laposte.net
Wed Feb 28 08:19:27 UTC 2018


This is an automated email from the git hooks/post-receive script.

jpuydt-guest pushed a commit to branch master
in repository node-compare-versions.

commit d367c0a2c164d5ddb0f2eb980cf5be98d82321ee
Author: Julien Puydt <julien.puydt at laposte.net>
Date:   Wed Feb 28 08:14:07 2018 +0100

    New upstream version 3.1.0
---
 .editorconfig   |  5 +++
 .gitignore      |  2 ++
 .travis.yml     |  8 +++++
 CHANGELOG.md    | 31 ++++++++++++++++++
 LICENSE         | 21 ++++++++++++
 README.md       | 51 +++++++++++++++++++++++++++++
 bower.json      | 29 +++++++++++++++++
 index.js        | 68 +++++++++++++++++++++++++++++++++++++++
 package.json    | 28 ++++++++++++++++
 test/compare.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/gt.js      | 57 +++++++++++++++++++++++++++++++++
 test/sort.js    | 60 ++++++++++++++++++++++++++++++++++
 12 files changed, 459 insertions(+)

diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..adbc04b
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,5 @@
+root = true
+
+[*]
+indent_style = spaces
+indent_size = 4
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..62562b7
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+coverage
+node_modules
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..712e423
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,8 @@
+language: node_js
+node_js:
+  - 0.10
+  - 0.12
+  - 4
+  - 6
+script: "npm run-script test-travis"
+after_script: "npm install coveralls at 2.10.0 && cat ./coverage/lcov.info | coveralls"
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..7f9bfb0
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,31 @@
+# Changelog
+
+## [3.1.0](https://github.com/omichelsen/compare-versions/releases/tag/v3.1.0) - 2017-09-25
+- Ignore leading zero in numbers.
+
+## [3.0.1](https://github.com/omichelsen/compare-versions/releases/tag/v3.0.1) - 2017-04-01
+- Fix for leading 'v'.
+
+## [3.0.0](https://github.com/omichelsen/compare-versions/releases/tag/v3.0.0) - 2016-08-08
+- Validate input data.
+
+## [2.0.2](https://github.com/omichelsen/compare-versions/releases/tag/v2.0.2) - 2016-06-06
+- Handle numbers in pre-release versions.
+
+## [2.0.1](https://github.com/omichelsen/compare-versions/releases/tag/v2.0.1) - 2015-09-13
+- Fix for versions with <3 digits.
+
+## [2.0.0](https://github.com/omichelsen/compare-versions/releases/tag/v2.0.0) - 2015-09-07
+- Change global window accessor from returnExports to compareVersions.
+
+## [1.1.2](https://github.com/omichelsen/compare-versions/releases/tag/v1.1.2) - 2015-05-03
+- Move patch check outside of the for loop.
+
+## [1.1.1](https://github.com/omichelsen/compare-versions/releases/tag/v1.1.1) - 2015-05-03
+- Add a base 10 radix.
+
+## [1.1.0](https://github.com/omichelsen/compare-versions/releases/tag/v1.1.0) - 2015-03-18
+- Added support for semver pre-release and metadata syntax.
+
+## [1.0.0](https://github.com/omichelsen/compare-versions/releases/tag/v1.0.0) - 2015-03-18
+- Initial release.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..de682fe
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2015
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f2a1e24
--- /dev/null
+++ b/README.md
@@ -0,0 +1,51 @@
+# compare-versions
+
+[![Build Status](https://img.shields.io/travis/omichelsen/compare-versions/master.svg)](https://travis-ci.org/omichelsen/compare-versions)
+[![Coverage Status](https://coveralls.io/repos/omichelsen/compare-versions/badge.svg?branch=master&service=github)](https://coveralls.io/github/omichelsen/compare-versions?branch=master)
+
+Compare [semver](http://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 (<600 bytes gzipped).
+
+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`. Also supports wildcards for minor and patch version like `1.0.x` or `1.0.*`. Any leading `v` is ignored. Numbers with leading zero is handled as normal numbers ignoring the zero.
+
+## Install
+
+Install with `npm` or `bower`:
+
+```bash
+$ npm install compare-versions --save
+```
+
+```bash
+$ bower install compare-versions --save
+```
+
+## Usage
+
+```javascript
+var compareVersions = require('compare-versions');
+
+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 = [
+    '1.5.19',
+    '1.2.3',
+    '1.5.5'
+];
+console.log(versions.sort(compareVersions));
+```
+
+Outputs:
+
+```javascript
+[
+    '1.2.3',
+    '1.5.5',
+    '1.5.19'
+]
+```
diff --git a/bower.json b/bower.json
new file mode 100644
index 0000000..2a7e964
--- /dev/null
+++ b/bower.json
@@ -0,0 +1,29 @@
+{
+  "name": "compare-versions",
+  "version": "3.1.0",
+  "description": "Compare semver version strings to find greater, equal or lesser.",
+  "main": "index.js",
+  "authors": [
+    "Ole Bjørn Michelsen <ole at michelsen.dk>"
+  ],
+  "moduleType": [
+    "amd",
+    "globals",
+    "node"
+  ],
+  "keywords": [
+    "semver",
+    "version",
+    "compare",
+    "browser",
+    "node"
+  ],
+  "license": "MIT",
+  "ignore": [
+    "**/.*",
+    "node_modules",
+    "bower_components",
+    "test",
+    "tests"
+  ]
+}
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..4b94a31
--- /dev/null
+++ b/index.js
@@ -0,0 +1,68 @@
+/* global define */
+(function (root, factory) {
+    /* istanbul ignore next */
+    if (typeof define === 'function' && define.amd) {
+        define([], factory);
+    } else if (typeof exports === 'object') {
+        module.exports = factory();
+    } else {
+        root.compareVersions = factory();
+    }
+}(this, function () {
+
+    var semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
+    var patch = /-([0-9A-Za-z-.]+)/;
+
+    function split(v) {
+        var temp = v.replace(/^v/, '').split('.');
+        var arr = temp.splice(0, 2);
+        arr.push(temp.join('.'));
+        return arr;
+    }
+
+    function tryParse(v) {
+        return isNaN(Number(v)) ? v : Number(v);
+    }
+
+    function validate(version) {
+        if (typeof version !== 'string') {
+            throw new TypeError('Invalid argument expected string');
+        }
+        if (!semver.test(version)) {
+            throw new Error('Invalid argument not valid semver');
+        }
+    }
+
+    return function compareVersions(v1, v2) {
+        [v1, v2].forEach(validate);
+
+        var s1 = split(v1);
+        var s2 = split(v2);
+
+        for (var i = 0; i < 3; i++) {
+            var n1 = parseInt(s1[i] || 0, 10);
+            var n2 = parseInt(s2[i] || 0, 10);
+
+            if (n1 > n2) return 1;
+            if (n2 > n1) return -1;
+        }
+
+        if ([s1[2], s2[2]].every(patch.test.bind(patch))) {
+            var p1 = patch.exec(s1[2])[1].split('.').map(tryParse);
+            var p2 = patch.exec(s2[2])[1].split('.').map(tryParse);
+
+            for (i = 0; i < Math.max(p1.length, p2.length); i++) {
+                if (p1[i] === undefined || typeof p2[i] === 'string' && typeof p1[i] === 'number') return -1;
+                if (p2[i] === undefined || typeof p1[i] === 'string' && typeof p2[i] === 'number') return 1;
+
+                if (p1[i] > p2[i]) return 1;
+                if (p2[i] > p1[i]) return -1;
+            }
+        } else if ([s1[2], s2[2]].some(patch.test.bind(patch))) {
+            return patch.test(s1[2]) ? -1 : 1;
+        }
+
+        return 0;
+    };
+
+}));
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..6a844d5
--- /dev/null
+++ b/package.json
@@ -0,0 +1,28 @@
+{
+  "name": "compare-versions",
+  "version": "3.1.0",
+  "description": "Compare semver version strings to find greater, equal or lesser.",
+  "main": "index.js",
+  "repository": {
+    "type": "git",
+    "url": "git at github.com:omichelsen/compare-versions.git"
+  },
+  "scripts": {
+    "test": "mocha",
+    "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/",
+    "coverage": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/"
+  },
+  "keywords": [
+    "semver",
+    "version",
+    "compare",
+    "browser",
+    "node"
+  ],
+  "author": "Ole Bjørn Michelsen <ole at michelsen.dk>",
+  "license": "MIT",
+  "devDependencies": {
+    "istanbul": "^0.4.3",
+    "mocha": "^3.0.2"
+  }
+}
diff --git a/test/compare.js b/test/compare.js
new file mode 100644
index 0000000..00f63bf
--- /dev/null
+++ b/test/compare.js
@@ -0,0 +1,99 @@
+var assert = require('assert');
+var compare = require('..');
+var cmp = {
+    '1': '>',
+    '0': '=',
+    '-1': '<'
+};
+
+describe('compare versions', function () {
+    it('should compare three-segment versions correctly', function () {
+        assert.equal(compare('10.1.8', '10.0.4'),  1);
+        assert.equal(compare('10.0.1', '10.0.1'),  0);
+        assert.equal(compare('10.1.1', '10.2.2'), -1);
+    });
+
+    it('should compare two-segment versions correctly', function () {
+        assert.equal(compare('10.8', '10.4'),  1);
+        assert.equal(compare('10.1', '10.1'),  0);
+        assert.equal(compare('10.1', '10.2'), -1);
+    });
+
+    it('should compare single-segment versions correctly', function () {
+        assert.equal(compare('10', '9'),  1);
+        assert.equal(compare('10', '10'),  0);
+        assert.equal(compare('9', '10'), -1);
+    });
+
+    it('should compare versions with different number of digits in same group', function () {
+        assert.equal(compare('11.0.10', '11.0.2'), 1);
+        assert.equal(compare('11.0.2', '11.0.10'), -1);
+    });
+
+    it('should compare versions with different number of digits in different groups', function () {
+        assert.equal(compare('11.1.10', '11.0'), 1);
+    });
+
+    it('should compare versions with different number of digits', function () {
+        assert.equal(compare('1.1.1', '1'), 1);
+        assert.equal(compare('1.0.0', '1'), 0);
+        assert.equal(compare('1.0', '1.4.1'), -1);
+    });
+
+    describe('pre-release versions', function () {
+        [
+            ['1.0.0-alpha.1', '1.0.0-alpha', 1],
+            ['1.0.0-alpha', '1.0.0-alpha.1', -1],
+            ['1.0.0-alpha.1', '1.0.0-alpha.beta', -1],
+            ['1.0.0-alpha.beta', '1.0.0-beta', -1],
+            ['1.0.0-beta', '1.0.0-beta.2', -1],
+            ['1.0.0-beta.2', '1.0.0-beta.11', -1],
+            ['1.0.0-beta.11', '1.0.0-rc.1', -1],
+            ['1.0.0-rc.1', '1.0.0', -1],
+            ['1.0.0-alpha', '1', -1]
+        ].forEach(function (data) {
+            it('should return ' + data[0] + ' ' + cmp[data[2]] + ' ' + data[1], function () {
+                assert.equal(compare(data[0], data[1]), data[2]);
+            });
+        });
+    });
+
+    it('should ignore build metadata', function () {
+        assert.equal(compare('1.4.0-build.3928', '1.4.0-build.3928+sha.a8d9d4f'), 0);
+        assert.equal(compare('1.4.0-build.3928+sha.b8dbdb0', '1.4.0-build.3928+sha.a8d9d4f'), 0);
+    });
+
+    it('should ignore leading `v`', function () {
+        assert.equal(compare('v1.0.0', '1.0.0'), 0);
+        assert.equal(compare('v1.0.0', 'v1.0.0'), 0);
+        assert.equal(compare('v1.0.0', 'v1.0.0'), 0);
+        assert.equal(compare('v1.0.0-alpha', '1.0.0-alpha'), 0);
+    });
+
+    it('should ignore leading `0`', function () {
+        [
+            ['01.0.0', '1.0.0', 0],
+            ['1.01.0', '1.01.0', 0],
+            ['1.0.03', '1.0.3', 0],
+            ['1.0.03-alpha', '1.0.3-alpha', 0],
+            ['v01.0.0', '1.0.0', 0],
+            ['v01.0.0', '2.0.0', -1],
+        ].forEach(function (data) {
+            assert.equal(compare(data[0], data[1]), data[2]);
+        });
+    });
+
+    it('should throw on invalid input', function () {
+        [
+            [42, /Invalid argument expected string/],
+            [{}, /Invalid argument expected string/],
+            [[], /Invalid argument expected string/],
+            [function () {}, /Invalid argument expected string/],
+            ['6.3.', /Invalid argument not valid semver/],
+        ].forEach(function (data) {
+            assert.throws(function () {
+                compare(data[0], data[0]);
+            }, data[1]);
+        });
+    });
+});
\ No newline at end of file
diff --git a/test/gt.js b/test/gt.js
new file mode 100644
index 0000000..a0b5e18
--- /dev/null
+++ b/test/gt.js
@@ -0,0 +1,57 @@
+var assert = require('assert');
+var compare = require('..');
+
+describe('greater than', function () {
+    it('should evaluate to greater than (1)', function () {
+        [
+            ['0.1.20', '0.1.5'],
+            ['0.6.1-1', '0.6.1-0'],
+            ['0.7.x', '0.6.0'],
+            ['0.7.x', '0.6.0-asdf'],
+            ['0.7.x', '0.6.2'],
+            ['0.7.x', '0.7.0-asdf'],
+            ['1', '0.0.0-beta'],
+            ['1', '0.2.3'],
+            ['1', '0.2.4'],
+            ['1', '1.0.0-0'],
+            ['1', '1.0.0-beta'],
+            ['1.0', '0.0.0'],
+            ['1.0', '0.1.0'],
+            ['1.0', '0.1.2'],
+            ['1.0.0', '0.0.0'],
+            ['1.0.0', '0.0.1'],
+            ['1.0.0', '0.2.3'],
+            ['1.0.0-beta.2', '1.0.0-beta.1'],
+            ['1.2.*', '1.1.3'],
+            ['1.2.*', '1.1.9999'],
+            ['1.2.2', '1.2.1'],
+            ['1.2.x', '1.0.0'],
+            ['1.2.x', '1.1.0'],
+            ['1.2.x', '1.1.3'],
+            ['2', '1.0.0'],
+            ['2', '1.0.0-beta'],
+            ['2', '1.9999.9999'],
+            ['2.*.*', '1.0.1'],
+            ['2.*.*', '1.1.3'],
+            ['2.0.0', '1.0.0'],
+            ['2.0.0', '1.1.1'],
+            ['2.0.0', '1.2.9'],
+            ['2.0.0', '1.9999.9999'],
+            ['2.3', '2.2.1'],
+            ['2.3', '2.2.2'],
+            ['2.4', '2.3.0'],
+            ['2.4', '2.3.5'],
+            ['2.x.x', '1.0.0'],
+            ['2.x.x', '1.1.3'],
+            ['3.2.1', '2.3.2'],
+            ['3.2.1', '3.2.0'],
+            ['v0.5.4-pre', '0.5.4-alpha'],
+            ['v3.2.1', 'v2.3.2']
+        ].forEach(function (tuple) {
+            var v1 = tuple[0];
+            var v2 = tuple[1];
+            var msg = 'compare(' + v1 + ', ' + v2 + ')';
+            assert.equal(compare(v1, v2), 1, msg);
+        });
+    });
+});
\ No newline at end of file
diff --git a/test/sort.js b/test/sort.js
new file mode 100644
index 0000000..d1e27ca
--- /dev/null
+++ b/test/sort.js
@@ -0,0 +1,60 @@
+var assert = require('assert');
+var compare = require('..');
+
+describe('sort versions', function () {
+    it('should sort versions', function () {
+        var versions = [
+            '1.2.3',
+            '4.11.6',
+            '4.2.0',
+            '1.5.19',
+            '1.5.5',
+            '4.1.3',
+            '2.3.1',
+            '10.5.5',
+            '11.3.0'
+        ];
+
+        assert.deepEqual(versions.sort(compare), [
+            '1.2.3',
+            '1.5.5',
+            '1.5.19',
+            '2.3.1',
+            '4.1.3',
+            '4.2.0',
+            '4.11.6',
+            '10.5.5',
+            '11.3.0'
+        ]);
+    });
+
+    it('should sort different digits', function () {
+        var versions = [
+            '1.0',
+            '1.0.0',
+            '1.0.1'
+        ];
+
+        assert.deepEqual(versions.sort(compare), [
+            '1.0',
+            '1.0.0',
+            '1.0.1'
+        ]);
+    });
+
+    it('should sort pre-release', function () {
+        var versions = [
+            '1.0.0',
+            '1.0.1',
+            '1.0.1-gamma',
+            '1.0.1-alpha'
+        ];
+
+        assert.deepEqual(versions.sort(compare), [
+            '1.0.0',
+            '1.0.1-alpha',
+            '1.0.1-gamma',
+            '1.0.1'
+        ]);
+    });
+});
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-compare-versions.git



More information about the Pkg-javascript-commits mailing list