Skip to content

Commit

Permalink
Merge pull request #3 from l5oo00/master
Browse files Browse the repository at this point in the history
fix: compare versions of different lengths
  • Loading branch information
harttle committed May 17, 2019
2 parents a2ecf9d + c74580b commit 3862022
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ compare('1.1.1', '1.1.1'); // 0
compare('1.2.3', [1, 2, 3]); // 0
compare('1.2', 2); // -1
compare(3, [2, 3, 4]); // -1
compare('1.0.1', '1'); // 1
compare('1.1.', [1, 2, 3]); // Wrong Params.
compare('.1.1', [1, 2, 3]); // Wrong Params.

Expand Down
16 changes: 3 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,16 @@ define(function (require) {
var lenA = a.length;
var lenB = b.length;

for (var i = 0; i < Math.min(lenA, lenB); i++) {
var t = compare(a[i], b[i]);
for (var i = 0; i < Math.max(lenA, lenB); i++) {
var t = compare(a[i] || 0, b[i] || 0);

// 相同位置的数字不一致,则有大小结论
if (t !== 0) {
return t;
}
}

if (lenA === lenB) {
return 0;
}

var tmp = lenA > lenB ? a : b;
for (var j = i; j < Math.max(lenA, lenB); j++) {
if (tmp[j] === 0) {
return 0;
}
return compare(lenA, lenB);
}
return 0;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ define(function (require) {
if (this.isString(param)) {
return param.split('.').map(parseFloat);
}

return [];
}
};
});
6 changes: 6 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ define(['src/index'], function (compare) {
expect(compare(2, '1')).to.equal(1);
expect(compare([2], '1')).to.equal(1);
expect(compare([2, 3], 2)).to.equal(1);
expect(compare('1.1.0.1', 1.1)).to.equal(1);
expect(compare('1.0.1', '1')).to.equal(1);
});
it('should detect 0', function () {
expect(compare(1, 1)).to.equal(0);
Expand All @@ -15,6 +17,8 @@ define(['src/index'], function (compare) {
expect(compare([1, 2], '1.2')).to.equal(0);
expect(compare([2], 2)).to.equal(0);
expect(compare([8], [8, 0, 0])).to.equal(0);
expect(compare('1.1.0.0', 1.1)).to.equal(0);
expect(compare('1.0.0', '1')).to.equal(0);
});
it('should detect -1', function () {
expect(compare(1, 2)).to.equal(-1);
Expand All @@ -23,6 +27,8 @@ define(['src/index'], function (compare) {
expect(compare([1, 2], '1.4')).to.equal(-1);
expect(compare('1.2', 2)).to.equal(-1);
expect(compare('1.2', [1, 4])).to.equal(-1);
expect(compare('1', '1.0.1')).to.equal(-1);
expect(compare(1.1, '1.1.0.1')).to.equal(-1);
});
it('should detect Wrong Params.', function () {
expect(compare(1, '1.2.')).to.equal('Wrong Params.');
Expand Down
2 changes: 2 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define(function (require) {
var d = '.1.1';
var e = '1.2.3.';
var f = '3';
var g = {};
describe('test', function () {
it('should detect Number', function () {
expect(util.isNumber(a)).to.equal(true);
Expand Down Expand Up @@ -34,6 +35,7 @@ define(function (require) {
expect(util.toArray(a)).to.deep.equal([20]);
expect(util.toArray(b)).to.deep.equal([1, 2, 3]);
expect(util.toArray(c)).to.deep.equal([1, 0, 3]);
expect(util.toArray(g)).to.deep.equal([]);
});
});
});

0 comments on commit 3862022

Please sign in to comment.