version-clean
Version:
Turn a dirty version (such as v1, =1.0.x, >1.0 <2) into a clean version (1.0)
46 lines (45 loc) • 1.75 kB
JavaScript
;
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = versionClean;
/**
* Turn a dirty version (such as `v1`, `=1.0.x`, `>1.0 <2`) into a clean version (1.0)
* @param dirtyVersion The dirty version to clean
* @returns The clean version string if possible, otherwise an empty string
*/
function versionClean(dirtyVersion) {
var e_1, _a;
var version = String(dirtyVersion);
// replace everything that isn't relevant with a space, this ensures 1<2 becomes 1 2 rather than 12
version = version.replace(/[^0-9.]+/g, ' ');
// replace duplicate dots and trailing dots
version = version.replace(/\.\.+/g, '.').replace(/\. |\.$/g, ' ');
// return the first section that is relevant
var parts = version.split(/ +/);
try {
for (var parts_1 = __values(parts), parts_1_1 = parts_1.next(); !parts_1_1.done; parts_1_1 = parts_1.next()) {
var part = parts_1_1.value;
if (part)
return part.trim();
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (parts_1_1 && !parts_1_1.done && (_a = parts_1.return)) _a.call(parts_1);
}
finally { if (e_1) throw e_1.error; }
}
// failed to parse
return '';
}