Imagine having a piece of code structured like this:
export async function execute(conf: Record<string, string>, path: string, params: Array<string>) {
const cmd = params[1];
const commandOption = params.slice(2)
switch(cmd){
case "param_1":
return await function1(path, conf, commandOption)
break;
case "param_2":
return await function2(path, conf, commandOption)
break;
case "param_3":
return await function3(path, conf, commandOption)
break;
default:
console.log("command not entered")
break;
}
}
async function function1(path: string, conf: Record<string, string>, params: Array<string>) {
...
}
async function function2(path: string, conf: Record<string, string>, params: Array<string>) {
...
}
..
The script defines the parameters with user input values and has similar functions with identical parameters. Is there a way to streamline this process and reduce redundancy by introducing an alias for all 3 parameters?
This could look something like:
alias allParameters = (path, conf, commandOption)
or even
alias allParametersFunc = (conf: Record<string, string>, path: string, params: Array<string>)
export async function execute(allParametersFunc) {
const cmd = params[1];
const commandOption = params.slice(2)
switch(cmd){
case "param_1":
return await function1(allParameters)
break;
case "param_2":
return await function2(allParameters)
break;
case "param_3":
return await function3(allParameters)
break;
default:
console.log("command not entered")
break;
}
}
async function function1(allParametersFunc) {
...
}
async function function2(allParametersFunc) {
...
}
...
Using objects might be one approach, but this method feels more intuitive akin to a yaml anchor.