Files
privacy.sexy/tests/unit/stubs/FunctionParameterCollectionStub.ts
undergroundwires 6a89c6224b 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.
2021-09-02 18:59:25 +01:00

28 lines
1.1 KiB
TypeScript

import { IFunctionParameter } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameter';
import { IFunctionParameterCollection } from '@/application/Parser/Script/Compiler/Function/Parameter/IFunctionParameterCollection';
import { FunctionParameterStub } from './FunctionParameterStub';
export class FunctionParameterCollectionStub implements IFunctionParameterCollection {
private parameters = new Array<IFunctionParameter>();
public addParameter(parameter: IFunctionParameter): void {
this.parameters.push(parameter);
}
public get all(): readonly IFunctionParameter[] {
return this.parameters;
}
public withParameterName(parameterName: string, isOptional = true) {
const parameter = new FunctionParameterStub()
.withName(parameterName)
.withOptionality(isOptional);
this.addParameter(parameter);
return this;
}
public withParameterNames(parameterNames: readonly string[], isOptional = true) {
for (const parameterName of parameterNames) {
this.withParameterName(parameterName, isOptional);
}
return this;
}
}