-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from thebruce/move-to-semver
Move toSemver to its own file.
- Loading branch information
Showing
2 changed files
with
60 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
'use strict'; | ||
|
||
const semver = require('semver'); | ||
/** | ||
* Helper method to convert a version string to a semver string for comparison. | ||
* | ||
* @param {string} string | ||
* The string to convert. | ||
* | ||
* @return {string} | ||
* A semver string from string. | ||
* | ||
* @throws {Error} | ||
* Thrown if the string could not be converted to a semver. | ||
*/ | ||
const toSemver = (string) => { | ||
if (semver.valid(string)) { | ||
return string; | ||
} | ||
|
||
// Try cleaning the string just in case. | ||
const cleanString = semver.clean(string); | ||
if (semver.valid(cleanString)) { | ||
return cleanString; | ||
} | ||
|
||
// Do we even have a number? | ||
if (!string.match(/\d+/)) { | ||
throw new Error(`Unable to compare version string: ${string}`); | ||
} | ||
|
||
// Special case for bare numbers. | ||
if (string.match(/^\d+$/)) { | ||
return toSemver(`${string}.0.0`); | ||
} | ||
|
||
// Split the version string on dots, and try to convert to a semver. | ||
const versionFrags = string.split(/(\d+)\./).filter(frag => frag !== '' && frag.match(/^\d/)); | ||
|
||
if (versionFrags.length === 0) { | ||
return toSemver(string.replace(/(\d+)/, '$1.0.0')); | ||
} | ||
else if (versionFrags.length === 2) { | ||
let stillCollecting = true; | ||
const replace = versionFrags.join('.').split(/([^\d]+)/).filter((frag) => { | ||
if (!stillCollecting) { | ||
return false; | ||
} | ||
|
||
stillCollecting = (frag === '.' || frag.match(/^\d+$/)); | ||
return stillCollecting; | ||
}).join(''); | ||
return toSemver(string.replace(replace, `${replace}.0`)); | ||
} | ||
|
||
throw new Error(`Unable to compare version string: ${string}`); | ||
}; | ||
|
||
module.exports = toSemver; |