Skip to content

Commit

Permalink
Use named regex capture groups
Browse files Browse the repository at this point in the history
Add eslint rule requiring named capture groups for better regex clarity.
  • Loading branch information
danrahn committed Mar 4, 2023
1 parent 5e8ffad commit 2f9326b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
{ "blankLine": "any", "prev": "*", "next": "case" },
{ "blankLine": "any", "prev": "*", "next": "default" }],
"prefer-const" : "error",
"prefer-named-capture-group" : "error",
"prefer-regex-literals" : "error",
"quote-props" : ["error", "as-needed"],
"quotes": ["error", "single", { "avoidEscape" : true, "allowTemplateLiterals" : true }],
Expand Down
8 changes: 4 additions & 4 deletions Client/Script/PlexClientState.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ class PlexClientStateManager {
async search(query) {
let regexp = undefined;
// Not a perfect test, but close enough
const match = /^\/(.+)\/(g?i?d?y?)$/.exec(query);
const match = /^\/(?<regex>.+)\/(?<modifiers>g?i?d?y?)$/.exec(query);
if (match) {
regexp = new RegExp(match[1], match[2]);
regexp = new RegExp(match.groups.regex, match.groups.modifiers);
}

// For movies, also try matching any year that's present.
let queryYear = /\b(1[8-9]\d{2}|20\d{2})\b/.exec(query);
if (queryYear) { queryYear = queryYear[1]; }
let queryYear = /\b(?<year>1[8-9]\d{2}|20\d{2})\b/.exec(query);
if (queryYear) { queryYear = queryYear.groups.year; }

// Ignore non-word characters to improve matching if there are spacing or quote mismatches.
// Don't use \W though, since that also clears out unicode characters. Rather than import
Expand Down
16 changes: 8 additions & 8 deletions Client/Script/VersionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class Version {
* * `alpha`
* * `beta`
* * `rc.0`, where 0 is any number with 1 or more digits */
static #versionRegex = /^v?(\d+)\.(\d+)\.(\d+)(?:-(alpha|beta|rc\.(\d+)))?/;
static #versionRegex = /^v?(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?:-(?<type>alpha|beta|rc\.(?<rcVersion>\d+)))?/;

/**
* Compare two `Version`s, ordering from smallest to largest
Expand Down Expand Up @@ -272,11 +272,11 @@ class Version {
return;
}

this.major = parseInt(parts[1]);
this.minor = parseInt(parts[2]);
this.patch = parseInt(parts[3]);
if (parts[4]) {
const partLower = parts[4].toLowerCase();
this.major = parseInt(parts.groups.major);
this.minor = parseInt(parts.groups.minor);
this.patch = parseInt(parts.groups.patch);
if (parts.groups.type) {
const partLower = parts.groups.type.toLowerCase();
switch (partLower) {
case 'alpha':
this.releaseTypeInfo.type = PrereleaseType.Alpha;
Expand All @@ -286,11 +286,11 @@ class Version {
break;
default:
Log.assert(
partLower.startsWith('rc') && parts[5],
partLower.startsWith('rc') && parts.groups.rcVersion,
`Version: Expected rc with a valid rc number if not alpha or beta, found ${partLower}.`);

this.releaseTypeInfo.type = PrereleaseType.ReleaseCandidate;
this.releaseTypeInfo.rcVersion = parseInt(parts[5]);
this.releaseTypeInfo.rcVersion = parseInt(parts.groups.rcVersion);
break;
}
}
Expand Down
10 changes: 5 additions & 5 deletions Client/Script/inc/Animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,12 @@ function Color(r, g, b, a) {
}

// Assume rgb string
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(r);
const result = /^#?(?<red>[a-f\d]{2})(?<green>[a-f\d]{2})(?<blue>[a-f\d]{2})(?<alpha>[a-f\d]{2})?$/i.exec(r);

this.r = parse(result[1], 16);
this.g = parse(result[2], 16);
this.b = parse(result[3], 16);
this.a = result[4] ? (parse(result[4], 16) / 255): 1;
this.r = parse(result.groups.red, 16);
this.g = parse(result.groups.green, 16);
this.b = parse(result.groups.blue, 16);
this.a = result.groups.alpha ? (parse(result.groups.alpha, 16) / 255) : 1;
} else {
if (g === undefined) {
// Hacky to keep the trailing parenthesis, but parseInt/Float figures it out
Expand Down
8 changes: 4 additions & 4 deletions Shared/ConsoleLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,10 @@ class ConsoleLog {
* @param {string} logString
* @param {number} levelDefault The default level if we are unable to parse the logString. */
setFromString(logString, levelDefault=ConsoleLog.Level.Info) {
const match = /(trace)?(dark)?(extreme|tmi|verbose|info|warn|error|critical)?/i.exec(logString);
this.setTrace(match[1] ? 1 : 0);
this.setDarkConsole(match[2] ? 1 : 0);
let level = match[3] ? ConsoleLog.#logStrings.indexOf(match[3].toUpperCase()) : ConsoleLog.Level.Invalid;
const match = /(?<t>trace)?(?<d>dark)?(?<l>extreme|tmi|verbose|info|warn|error|critical)?/i.exec(logString);
this.setTrace(match.groups.t ? 1 : 0);
this.setDarkConsole(match.groups.d ? 1 : 0);
let level = match.groups.l ? ConsoleLog.#logStrings.indexOf(match.groups.l.toUpperCase()) : ConsoleLog.Level.Invalid;
if (level == ConsoleLog.Level.Invalid) {
console.warn(
`[WARN][${ConsoleLog.#getTimestring()}] ` +
Expand Down
4 changes: 2 additions & 2 deletions Shared/MarkerBreakdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class MarkerBreakdown {
case MarkerType.Credits:
return delta << CreditsShift;
default:
// Unexpected, but just log an error and treat it as an intro.
Log.error(`Invalid marker type ${markerType}`);
// Silently fall back to an intro
Log.error(`Invalid marker type "${markerType}"`);
return delta;
}
}
Expand Down

0 comments on commit 2f9326b

Please sign in to comment.