-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Compare versions properly, not as plain strings (#67)
* handle version range coming from browserslist * ensure no ranged version, to simplify the problem * sort for oldest version * simplify earlier, before the sort * compare as versions for compatibility * comparison tests * eol
- Loading branch information
1 parent
36c9224
commit 659e9da
Showing
8 changed files
with
114 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/eslint-plugin-ecmascript-compat/lib/compareVersions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = function compareVersions(a, b) { | ||
const aParts = a.split('.').map(Number); | ||
const bParts = b.split('.').map(Number); | ||
|
||
for (let i = 0; i < Math.max(aParts.length, bParts.length); i++) { | ||
if (aParts[i] === bParts[i]) { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} | ||
|
||
return aParts[i] < bParts[i] ? -1 : 1; | ||
} | ||
|
||
return 0; | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/eslint-plugin-ecmascript-compat/lib/compareVersions.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const compareVersions = require('./compareVersions'); | ||
|
||
test('equal', () => { | ||
expect(compareVersions('1.2.3', '1.2.3')).toBe(0); | ||
}); | ||
|
||
test('smaller', () => { | ||
expect(compareVersions('1.2.0', '1.2.3')).toBe(-1); | ||
expect(compareVersions('1.2', '1.3')).toBe(-1); | ||
}); | ||
|
||
test('larger', () => { | ||
expect(compareVersions('1.2.3', '1.2.0')).toBe(1); | ||
}); | ||
|
||
test('partial on one side', () => { | ||
expect(compareVersions('1.1', '1.2.3')).toBe(-1); | ||
expect(compareVersions('1.1.1', '1.2')).toBe(-1); | ||
}); | ||
|
||
test('when it matters to treat parts as numbers', () => { | ||
expect(compareVersions('9', '10')).toBe(-1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters