- Unify test data for nonexistence of an object/string and collection. - Introduce more test through adding missing test data to existing tests. - Improve logic for checking absence of values to match tests. - Add missing tests for absent value validation. - Update documentation to include shared test functionality.
32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
import { IPipeFactory, PipeFactory } from './PipeFactory';
|
|
import { IPipelineCompiler } from './IPipelineCompiler';
|
|
|
|
export class PipelineCompiler implements IPipelineCompiler {
|
|
constructor(private readonly factory: IPipeFactory = new PipeFactory()) { }
|
|
|
|
public compile(value: string, pipeline: string): string {
|
|
ensureValidArguments(value, pipeline);
|
|
const pipeNames = extractPipeNames(pipeline);
|
|
const pipes = pipeNames.map((pipeName) => this.factory.get(pipeName));
|
|
return pipes.reduce((previousValue, pipe) => {
|
|
return pipe.apply(previousValue);
|
|
}, value);
|
|
}
|
|
}
|
|
|
|
function extractPipeNames(pipeline: string): string[] {
|
|
return pipeline
|
|
.trim()
|
|
.split('|')
|
|
.slice(1)
|
|
.map((p) => p.trim());
|
|
}
|
|
|
|
function ensureValidArguments(value: string, pipeline: string) {
|
|
if (!value) { throw new Error('missing value'); }
|
|
if (!pipeline) { throw new Error('missing pipeline'); }
|
|
if (!pipeline.trimStart().startsWith('|')) {
|
|
throw new Error('pipeline does not start with pipe');
|
|
}
|
|
}
|