Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move toSemver to its own file. #62

Merged
merged 1 commit into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 1 addition & 55 deletions lib/compare.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,7 @@
'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}`);
};
const toSemver = require('./toSemver');

/**
* Compares two version strings by first converting them to semvers.
Expand Down
59 changes: 59 additions & 0 deletions lib/toSemver.js
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;