Add optionality for parameters
This commit allows for parameters that does not require any arguments to be provided in function calls. It changes collection syntax where parameters are list of objects instead of primitive strings. A parameter has now 'name' and 'optional' properties. 'name' is required and used in same way as older strings as parameter definitions. 'Optional' property is optional, 'false' is the default behavior if undefined. It also adds additional validation to restrict parameter names to alphanumeric strings to have a clear syntax in expressions.
This commit is contained in:
39
tests/unit/stubs/FunctionCallArgumentCollectionStub.ts
Normal file
39
tests/unit/stubs/FunctionCallArgumentCollectionStub.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
// tslint:disable-next-line:max-line-length
|
||||
import { IFunctionCallArgument } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgument';
|
||||
import { IFunctionCallArgumentCollection } from '@/application/Parser/Script/Compiler/FunctionCall/Argument/IFunctionCallArgumentCollection';
|
||||
import { FunctionCallArgumentStub } from './FunctionCallArgumentStub';
|
||||
|
||||
export class FunctionCallArgumentCollectionStub implements IFunctionCallArgumentCollection {
|
||||
private args = new Array<IFunctionCallArgument>();
|
||||
|
||||
public withArgument(parameterName: string, argumentValue: string) {
|
||||
const arg = new FunctionCallArgumentStub()
|
||||
.withParameterName(parameterName)
|
||||
.withArgumentValue(argumentValue);
|
||||
this.addArgument(arg);
|
||||
return this;
|
||||
}
|
||||
public withArguments(args: { readonly [index: string]: string }) {
|
||||
for (const parameterName of Object.keys(args)) {
|
||||
const parameterValue = args[parameterName];
|
||||
this.withArgument(parameterName, parameterValue);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
public hasArgument(parameterName: string): boolean {
|
||||
return this.args.some((a) => a.parameterName === parameterName);
|
||||
}
|
||||
public addArgument(argument: IFunctionCallArgument): void {
|
||||
this.args.push(argument);
|
||||
}
|
||||
public getAllParameterNames(): string[] {
|
||||
return this.args.map((a) => a.parameterName);
|
||||
}
|
||||
public getArgument(parameterName: string): IFunctionCallArgument {
|
||||
const arg = this.args.find((a) => a.parameterName === parameterName);
|
||||
if (!arg) {
|
||||
throw new Error(`no argument exists for parameter "${parameterName}"`);
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user