Skip to content
Open
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
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
export default function parseNodeVersion(version) {
var match = version.match(
const match = version.match(
/^v(\d{1,2})\.(\d{1,2})\.(\d{1,2})(?:-([0-9A-Za-z-.]+))?(?:\+([0-9A-Za-z-.]+))?$/,
); // eslint-disable-line max-len
if (!match) {
throw new Error("Unable to parse: " + version);
throw new Error(`Unable to parse: ${version}`);
}

var res = {
return {
major: parseInt(match[1], 10),
minor: parseInt(match[2], 10),
patch: parseInt(match[3], 10),
pre: match[4] || "",
build: match[5] || "",
pre: match[4] ?? "",
build: match[5] ?? "",
};

return res;
}
16 changes: 8 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import parseNodeVersion from "../index.js";

describe("parseNodeVersion", function () {
it("takes process.version and returns all numbers", function () {
var nodeVersion = parseNodeVersion(process.version);
const nodeVersion = parseNodeVersion(process.version);
assert.deepStrictEqual(typeof nodeVersion.major, "number");
assert.deepStrictEqual(typeof nodeVersion.minor, "number");
assert.deepStrictEqual(typeof nodeVersion.patch, "number");
});

it("takes any string formatted like a v8.8.8 and returns all numbers", function () {
var nodeVersion = parseNodeVersion("v8.8.8");
const nodeVersion = parseNodeVersion("v8.8.8");
assert.deepStrictEqual(nodeVersion.major, 8);
assert.deepStrictEqual(nodeVersion.minor, 8);
assert.deepStrictEqual(nodeVersion.patch, 8);
Expand Down Expand Up @@ -49,35 +49,35 @@ describe("parseNodeVersion", function () {
});

it("always returns strings if no pre-release or build metadata", function () {
var nodeVersion = parseNodeVersion(process.version);
const nodeVersion = parseNodeVersion(process.version);
assert.deepStrictEqual(typeof nodeVersion.pre, "string");
assert.deepStrictEqual(nodeVersion.pre, "");
assert.deepStrictEqual(typeof nodeVersion.build, "string");
assert.deepStrictEqual(nodeVersion.build, "");
});

it("matches pre-release label with single identifier", function () {
var nodeVersion = parseNodeVersion("v1.2.3-alpha");
const nodeVersion = parseNodeVersion("v1.2.3-alpha");
assert.deepStrictEqual(nodeVersion.pre, "alpha");
});

it("matches pre-release label with multiple identifiers", function () {
var nodeVersion = parseNodeVersion("v1.2.3-x.7.z.92");
const nodeVersion = parseNodeVersion("v1.2.3-x.7.z.92");
assert.deepStrictEqual(nodeVersion.pre, "x.7.z.92");
});

it("matches pre-release label with dashes", function () {
var nodeVersion = parseNodeVersion("v1.2.3-x-7-z.92");
const nodeVersion = parseNodeVersion("v1.2.3-x-7-z.92");
assert.deepStrictEqual(nodeVersion.pre, "x-7-z.92");
});

it("matches build metadata", function () {
var nodeVersion = parseNodeVersion("v10.15.0+0-b20181231T20014594");
const nodeVersion = parseNodeVersion("v10.15.0+0-b20181231T20014594");
assert.deepStrictEqual(nodeVersion.build, "0-b20181231T20014594");
});

it("matches pre-release label and build metadata", function () {
var nodeVersion = parseNodeVersion("v1.0.0-beta+exp.sha.5114f85");
const nodeVersion = parseNodeVersion("v1.0.0-beta+exp.sha.5114f85");
assert.deepStrictEqual(nodeVersion.pre, "beta");
assert.deepStrictEqual(nodeVersion.build, "exp.sha.5114f85");
});
Expand Down
Loading