Skip to content

Commit 15f82ae

Browse files
committed
Add getStringArray helper to reduce getter boilerplate
Consolidates the repeated Array.isArray/split pattern into a single private method used by all 7 array-type getters.
1 parent d2c5b97 commit 15f82ae

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

src/argv.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ export class Argv {
120120
}
121121
}
122122

123+
private getStringArray (key: string): string[] {
124+
const val = this.map.get(key) ?? [];
125+
return Array.isArray(val) ? val : val.split(" ");
126+
}
127+
123128
get cwd (): string {
124129
let cwd = this.map.get("cwd") ?? ".";
125130
assert(typeof cwd != "object", "--cwd option cannot be an array");
@@ -150,20 +155,11 @@ export class Argv {
150155
return (this.map.get("home") ?? process.env.HOME ?? "").replace(/\/$/, "");
151156
}
152157

153-
get volume (): string[] {
154-
const val = this.map.get("volume") ?? [];
155-
return Array.isArray(val) ? val : val.split(" ");
156-
}
158+
get volume (): string[] { return this.getStringArray("volume"); }
157159

158-
get network (): string[] {
159-
const val = this.map.get("network") ?? [];
160-
return Array.isArray(val) ? val : val.split(" ");
161-
}
160+
get network (): string[] { return this.getStringArray("network"); }
162161

163-
get extraHost (): string[] {
164-
const val = this.map.get("extraHost") ?? [];
165-
return Array.isArray(val) ? val : val.split(" ");
166-
}
162+
get extraHost (): string[] { return this.getStringArray("extraHost"); }
167163

168164
get caFile (): string | null {
169165
return this.map.get("caFile") ?? null;
@@ -181,31 +177,24 @@ export class Argv {
181177
return this.map.get("pullPolicy") ?? "if-not-present";
182178
}
183179

184-
get remoteVariables (): string[] {
185-
const val = this.map.get("remoteVariables") ?? [];
186-
return Array.isArray(val) ? val : val.split(" ");
187-
}
180+
get remoteVariables (): string[] { return this.getStringArray("remoteVariables"); }
188181

189182
get variable (): {[key: string]: string} {
190-
const pairs = this.map.get("variable") ?? [];
191183
const variables: {[key: string]: string} = {};
192-
(pairs).forEach((variablePair: string) => {
193-
const exec = /(?<key>\w*?)(=)(?<value>(.|\n|\r)*)/.exec(variablePair);
184+
for (const pair of this.getStringArray("variable")) {
185+
const exec = /(?<key>\w*?)(=)(?<value>(.|\n|\r)*)/.exec(pair);
194186
if (exec?.groups?.key) {
195-
variables[exec.groups.key] = exec?.groups?.value;
187+
variables[exec.groups.key] = exec.groups.value;
196188
}
197-
});
189+
}
198190
return variables;
199191
}
200192

201193
get unsetVariables (): string[] {
202194
return this.map.get("unsetVariable") ?? [];
203195
}
204196

205-
get manual (): string[] {
206-
const val = this.map.get("manual") ?? [];
207-
return Array.isArray(val) ? val : val.split(" ");
208-
}
197+
get manual (): string[] { return this.getStringArray("manual"); }
209198

210199
get job (): string[] {
211200
return this.map.get("job") ?? [];
@@ -236,10 +225,7 @@ export class Argv {
236225
return this.map.get("privileged") ?? false;
237226
}
238227

239-
get device (): string[] {
240-
const val = this.map.get("device") ?? [];
241-
return Array.isArray(val) ? val : val.split(" ");
242-
}
228+
get device (): string[] { return this.getStringArray("device"); }
243229

244230
get ulimit (): string | null {
245231
const ulimit = this.map.get("ulimit");

0 commit comments

Comments
 (0)