Skip to content

Commit e7d1537

Browse files
author
Kurt
committed
Use named capture groups for version string matching
Previously it was referencing the wrong group index (off by one), which is easy to do when relying on indices. Using capture groups here should make it less error prone (and also fixes this initial problem). Long term a small unit test should avoid this happening / regressing.
1 parent 43a69a5 commit e7d1537

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

common.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,13 @@ function versionLessThan(cur_ver, min_ver) {
179179
// 'dev' is `null` if `str` was not a dev version.
180180
// On failure, returns `null`.
181181
function parseVersion(str) {
182-
const match = /^(\d+)\.(\d+)\.(\d+)(?:-dev\.(\d+)\+[0-9a-f]*)?$/.exec(str);
182+
const match = /^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-dev\.(?<dev>\d+)\+[0-9a-f]*)?$/.exec(str);
183183
if (match === null) return null;
184184
return {
185-
major: parseInt(match[0]),
186-
minor: parseInt(match[1]),
187-
patch: parseInt(match[2]),
188-
dev: match[3] === null ? null : parseInt(match[3]),
185+
major: parseInt(match.groups['major']),
186+
minor: parseInt(match.groups['minor']),
187+
patch: parseInt(match.groups['patch']),
188+
dev: match.groups['dev'] === undefined ? null : parseInt(match.groups['dev']),
189189
};
190190
}
191191

0 commit comments

Comments
 (0)