From ae0d2d59b0bc29608c8ef5ea1b9793333ecae749 Mon Sep 17 00:00:00 2001 From: Matthew Bergman Date: Mon, 23 Nov 2026 20:38:22 -0500 Subject: [PATCH] Improve implementation with newer ES features (#14) --- index.js | 12 +++++------- test/index.js | 16 ++++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 4b5142c..f91cfb1 100644 --- a/index.js +++ b/index.js @@ -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; } diff --git a/test/index.js b/test/index.js index 7db30ee..0cff8ba 100644 --- a/test/index.js +++ b/test/index.js @@ -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); @@ -49,7 +49,7 @@ 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"); @@ -57,27 +57,27 @@ describe("parseNodeVersion", function () { }); 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"); });