Skip to content

Commit eca5e2a

Browse files
committed
[feat] introduce prefixes for saved documents in config
1 parent bc7855d commit eca5e2a

4 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/config/Config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export namespace Config {
2323
}
2424
export interface ColumnMap {
2525
columns: Column[];
26+
prefix?: string;
2627
postfix?: string;
2728
}
2829
export interface AlgorithmColumns {

src/config/validateConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const checkSource = (source: Config.CoreConfiguration['source']) => {
118118
const checkDestination = (label: string, destination: Config.FileConfiguration['destination']) => {
119119
return (
120120
isObject(`[${label}]`, destination) ||
121+
isOptional(`[${label}].prefix?`, destination.prefix, isString) ||
121122
isOptional(`[${label}].postfix?`, destination.postfix, isString) ||
122123
checkColumns(`[${label}]`, destination.columns)
123124
);

src/encoding/base.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@ export abstract class EncoderBase {
8484
return this.outputPath;
8585
}
8686

87-
// internal helper to return a full name (with a timestamp according to the config)
88-
protected getOutputNameFor(baseFileName: string) {
89-
let fullName = `${baseFileName}${this.mapping.postfix}`;
90-
// TODO: add logic from config
91-
return formatName(fullName, new Date());
87+
// take a file path, split into filename, apply prefix/postfix, resolve subsitutions, return full path
88+
protected getOutputNameFor(filePath: string) {
89+
const fileParts = path.parse(filePath);
90+
const fileName = `${this.mapping.prefix ?? ''}${fileParts.name}${this.mapping.postfix ?? ''}`;
91+
const formatted = formatName(fileName, new Date());
92+
return path.join(fileParts.dir, formatted);
9293
}
9394

9495
protected withTemporaryFile(outputPath: string, pred: CallableFunction) {

tests/encoding/base.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { EncoderBase } from '@/encoding/base';
1818
import type { Config } from '@/config/Config';
1919

2020
const BASE_CFG: Config.ColumnMap = {
21+
prefix: "PREFIX_",
2122
postfix: '_POSTFIX',
2223
columns: [
2324
{ name: 'A', alias: 'col_a' },
@@ -48,12 +49,12 @@ function makeEncoderBase(cfg = BASE_CFG) {
4849

4950
test('EncoderBase::getOutputNameFor', () => {
5051
let e = makeEncoderBase(BASE_CFG);
51-
expect(e.test__getOutputNameFor('output')).toEqual('output_POSTFIX');
52+
expect(e.test__getOutputNameFor('output')).toEqual('PREFIX_output_POSTFIX');
5253

5354
BASE_CFG.postfix = '_PF_{{yyyy}}';
5455
e = makeEncoderBase(BASE_CFG);
5556
const d = new Date();
56-
expect(e.test__getOutputNameFor('output')).toEqual(`output_PF_${d.getFullYear()}`);
57+
expect(e.test__getOutputNameFor('output')).toEqual(`PREFIX_output_PF_${d.getFullYear()}`);
5758
});
5859

5960

0 commit comments

Comments
 (0)