Skip to content

Commit 8287323

Browse files
committed
refactor: centralize VALUE key handling
1 parent 346395d commit 8287323

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/formatters/fileNameDisplayFormatter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
getCurrentFileNamePreview,
1313
DateFormatPreviewGenerator
1414
} from "./helpers/previewHelpers";
15-
import { VALUE_LABEL_KEY_DELIMITER } from "../utils/valueSyntax";
15+
import { getValueVariableBaseName } from "../utils/valueSyntax";
1616

1717
import type QuickAdd from "../main";
1818

@@ -74,8 +74,7 @@ export class FileNameDisplayFormatter extends Formatter {
7474
protected getVariableValue(variableName: string): string {
7575
const stored = this.variables.get(variableName);
7676
if (typeof stored === "string") return stored;
77-
const baseName =
78-
variableName.split(VALUE_LABEL_KEY_DELIMITER)[0] ?? variableName;
77+
const baseName = getValueVariableBaseName(variableName);
7978
return getVariableExample(baseName);
8079
}
8180

src/formatters/formatDisplayFormatter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
getCurrentFileNamePreview,
1515
DateFormatPreviewGenerator
1616
} from "./helpers/previewHelpers";
17-
import { VALUE_LABEL_KEY_DELIMITER } from "../utils/valueSyntax";
17+
import { getValueVariableBaseName } from "../utils/valueSyntax";
1818

1919
export class FormatDisplayFormatter extends Formatter {
2020
constructor(
@@ -77,8 +77,7 @@ export class FormatDisplayFormatter extends Formatter {
7777
protected getVariableValue(variableName: string): string {
7878
const stored = this.variables.get(variableName);
7979
if (typeof stored === "string") return stored;
80-
const baseName =
81-
variableName.split(VALUE_LABEL_KEY_DELIMITER)[0] ?? variableName;
80+
const baseName = getValueVariableBaseName(variableName);
8281
return getVariableExample(baseName);
8382
}
8483

src/utils/valueSyntax.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export function buildValueVariableKey(
2424
: variableName;
2525
}
2626

27+
export function getValueVariableBaseName(variableKey: string): string {
28+
const delimiterIndex = variableKey.indexOf(VALUE_LABEL_KEY_DELIMITER);
29+
if (delimiterIndex === -1) return variableKey;
30+
return variableKey.slice(0, delimiterIndex);
31+
}
32+
2733
type ParsedOptions = {
2834
label?: string;
2935
defaultValue: string;

0 commit comments

Comments
 (0)