Skip to content

Commit 4f87c76

Browse files
authored
Add Params parse helper methods (#444)
- Move parsing to separate helper methods to main the _copyFromSearchParams method more readable. - Add Params.prototype._parseXXX helper methods
1 parent 1552769 commit 4f87c76

File tree

1 file changed

+88
-69
lines changed

1 file changed

+88
-69
lines changed

resources/params.mjs

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -43,88 +43,107 @@ class Params {
4343
}
4444

4545
_copyFromSearchParams(searchParams) {
46-
if (searchParams.has("viewport")) {
47-
const viewportParam = searchParams.get("viewport");
48-
const [width, height] = viewportParam.split("x");
49-
this.viewport.width = this._parseInt(width, "viewport.width");
50-
this.viewport.height = this._parseInt(height, "viewport.height");
51-
if (this.viewport.width < 800 || this.viewport.height < 600)
52-
throw new Error(`Invalid viewport param: ${viewportParam}`);
53-
searchParams.delete("viewport");
54-
}
55-
this.startAutomatically = searchParams.has("startAutomatically");
56-
searchParams.delete("startAutomatically");
57-
if (searchParams.has("iterationCount")) {
58-
this.iterationCount = this._parseInt(searchParams.get("iterationCount"), "iterationCount");
59-
if (this.iterationCount < 1)
60-
throw new Error(`Invalid iterationCount param: '${this.iterationCount}', must be > 1.`);
61-
searchParams.delete("iterationCount");
62-
}
46+
this.viewport = this._parseViewport(searchParams);
47+
this.startAutomatically = this._parseBooleanParam(searchParams, "startAutomatically");
48+
this.iterationCount = this._parseIntParam(searchParams, "iterationCount", 1);
49+
this.suites = this._parseSuites(searchParams);
50+
this.tags = this._parseTags(searchParams);
51+
this.developerMode = this._parseBooleanParam(searchParams, "developerMode");
52+
this.useWarmupSuite = this._parseBooleanParam(searchParams, "useWarmupSuite");
53+
this.waitBeforeSync = this._parseIntParam(searchParams, "waitBeforeSync", 0);
54+
this.warmupBeforeSync = this._parseIntParam(searchParams, "warmupBeforeSync", 0);
55+
this.measurementMethod = this._parseMeasurementMethod(searchParams);
56+
this.shuffleSeed = this._parseShuffleSeed(searchParams);
57+
58+
const unused = Array.from(searchParams.keys());
59+
if (unused.length > 0)
60+
console.error("Got unused search params", unused);
61+
}
62+
63+
_parseBooleanParam(searchParams, paramKey) {
64+
if (!searchParams.has(paramKey))
65+
return false;
66+
searchParams.delete(paramKey);
67+
return true;
68+
}
69+
70+
_parseIntParam(searchParams, paramKey, minValue) {
71+
if (!searchParams.has(paramKey))
72+
return defaultParams[paramKey];
73+
74+
const parsedValue = this._parseInt(searchParams.get(paramKey), "waitBeforeSync");
75+
if (parsedValue < minValue)
76+
throw new Error(`Invalid ${paramKey} param: '${parsedValue}', value must be >= ${minValue}.`);
77+
searchParams.delete(paramKey);
78+
return parsedValue;
79+
}
80+
81+
_parseViewport(searchParams) {
82+
if (!searchParams.has("viewport"))
83+
return defaultParams.viewport;
84+
const viewportParam = searchParams.get("viewport");
85+
const [width, height] = viewportParam.split("x");
86+
const viewport = {
87+
width: this._parseInt(width, "viewport.width"),
88+
height: this._parseInt(height, "viewport.height"),
89+
};
90+
if (this.viewport.width < 800 || this.viewport.height < 600)
91+
throw new Error(`Invalid viewport param: ${viewportParam}`);
92+
searchParams.delete("viewport");
93+
return viewport;
94+
}
95+
96+
_parseSuites(searchParams) {
6397
if (searchParams.has("suite") || searchParams.has("suites")) {
6498
if (searchParams.has("suite") && searchParams.has("suites"))
6599
throw new Error("Params 'suite' and 'suites' can not be used together.");
66100
const value = searchParams.get("suite") || searchParams.get("suites");
67-
this.suites = value.split(",");
68-
if (this.suites.length === 0)
101+
const suites = value.split(",");
102+
if (suites.length === 0)
69103
throw new Error("No suites selected");
70104
searchParams.delete("suite");
71105
searchParams.delete("suites");
106+
return suites;
72107
}
108+
return defaultParams.suites;
109+
}
73110

74-
if (searchParams.has("tags")) {
75-
if (this.suites.length)
76-
throw new Error("'suites' and 'tags' cannot be used together.");
77-
this.tags = searchParams.get("tags").split(",");
78-
searchParams.delete("tags");
79-
}
80-
81-
this.developerMode = searchParams.has("developerMode");
82-
searchParams.delete("developerMode");
83-
84-
if (searchParams.has("useWarmupSuite")) {
85-
this.useWarmupSuite = true;
86-
searchParams.delete("useWarmupSuite");
87-
}
88-
89-
if (searchParams.has("waitBeforeSync")) {
90-
this.waitBeforeSync = this._parseInt(searchParams.get("waitBeforeSync"), "waitBeforeSync");
91-
if (this.waitBeforeSync < 0)
92-
throw new Error(`Invalid waitBeforeSync param: '${this.waitBeforeSync}', must be >= 0.`);
93-
searchParams.delete("waitBeforeSync");
94-
}
95-
96-
if (searchParams.has("warmupBeforeSync")) {
97-
this.warmupBeforeSync = this._parseInt(searchParams.get("warmupBeforeSync"), "warmupBeforeSync");
98-
if (this.warmupBeforeSync < 0)
99-
throw new Error(`Invalid warmupBeforeSync param: '${this.warmupBeforeSync}', must be >= 0.`);
100-
searchParams.delete("warmupBeforeSync");
101-
}
111+
_parseTags() {
112+
if (!searchParams.has("tags"))
113+
return defaultParams.tags;
114+
if (this.suites.length)
115+
throw new Error("'suites' and 'tags' cannot be used together.");
116+
const tags = searchParams.get("tags").split(",");
117+
searchParams.delete("tags");
118+
return tags;
119+
}
102120

103-
if (searchParams.has("measurementMethod")) {
104-
this.measurementMethod = searchParams.get("measurementMethod");
105-
if (this.measurementMethod !== "timer" && this.measurementMethod !== "raf")
106-
throw new Error(`Invalid measurement method: '${this.measurementMethod}', must be either 'raf' or 'timer'.`);
107-
searchParams.delete("measurementMethod");
108-
}
121+
_parseMeasurementMethod(searchParams) {
122+
if (!searchParams.has("measurementMethod"))
123+
return defaultParams.measurementMethod;
124+
const measurementMethod = searchParams.get("measurementMethod");
125+
if (measurementMethod !== "timer" && measurementMethod !== "raf")
126+
throw new Error(`Invalid measurement method: '${measurementMethod}', must be either 'raf' or 'timer'.`);
127+
searchParams.delete("measurementMethod");
128+
return measurementMethod;
129+
}
109130

110-
if (searchParams.has("shuffleSeed")) {
111-
this.shuffleSeed = searchParams.get("shuffleSeed");
112-
if (this.shuffleSeed !== "off") {
113-
if (this.shuffleSeed === "generate") {
114-
this.shuffleSeed = Math.floor((Math.random() * 1) << 16);
115-
console.log(`Generated a random suite order seed: ${this.shuffleSeed}`);
116-
} else {
117-
this.shuffleSeed = parseInt(this.shuffleSeed);
118-
}
119-
if (!Number.isInteger(this.shuffleSeed))
120-
throw new Error(`Invalid shuffle seed: '${this.shuffleSeed}', must be either 'off', 'generate' or an integer.`);
131+
_parseShuffleSeed(searchParams) {
132+
if (!searchParams.has("shuffleSeed"))
133+
return defaultParams.shuffleSeed;
134+
let shuffleSeed = searchParams.get("shuffleSeed");
135+
if (shuffleSeed !== "off") {
136+
if (shuffleSeed === "generate") {
137+
shuffleSeed = Math.floor((Math.random() * 1) << 16);
138+
console.log(`Generated a random suite order seed: ${shuffleSeed}`);
139+
} else {
140+
shuffleSeed = parseInt(shuffleSeed);
121141
}
122-
searchParams.delete("shuffleSeed");
142+
if (!Number.isInteger(shuffleSeed))
143+
throw new Error(`Invalid shuffle seed: '${shuffleSeed}', must be either 'off', 'generate' or an integer.`);
123144
}
124-
125-
const unused = Array.from(searchParams.keys());
126-
if (unused.length > 0)
127-
console.error("Got unused search params", unused);
145+
searchParams.delete("shuffleSeed");
146+
return shuffleSeed;
128147
}
129148

130149
toSearchParams() {

0 commit comments

Comments
 (0)