Skip to content

Commit e7ac211

Browse files
committed
fix: add support for value variable from QuickAdd API
1 parent 7f95f29 commit e7ac211

File tree

1 file changed

+26
-45
lines changed

1 file changed

+26
-45
lines changed

src/formatters/formatter.ts

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ export abstract class Formatter {
4343
const offsetIsInt = NUMBER_REGEX.test(offsetString);
4444
if (offsetIsInt) offset = parseInt(offsetString);
4545
}
46-
output = this.replacer(
47-
output,
48-
DATE_REGEX,
49-
getDate({ offset: offset })
50-
);
46+
output = this.replacer(output, DATE_REGEX, getDate({ offset: offset }));
5147
}
5248

5349
while (DATE_REGEX_FORMATTED.test(output)) {
@@ -66,7 +62,7 @@ export abstract class Formatter {
6662
output = this.replacer(
6763
output,
6864
DATE_REGEX_FORMATTED,
69-
getDate({ format, offset: offset })
65+
getDate({ format, offset: offset }),
7066
);
7167
}
7268

@@ -80,11 +76,7 @@ export abstract class Formatter {
8076
const timeMatch = TIME_REGEX.exec(output);
8177
if (!timeMatch) throw new Error("unable to parse time");
8278

83-
output = this.replacer(
84-
output,
85-
TIME_REGEX,
86-
getDate({ format: "HH:mm" })
87-
)
79+
output = this.replacer(output, TIME_REGEX, getDate({ format: "HH:mm" }));
8880
}
8981

9082
while (TIME_REGEX_FORMATTED.test(output)) {
@@ -93,23 +85,21 @@ export abstract class Formatter {
9385

9486
const format = timeMatch[1];
9587

96-
output = this.replacer(
97-
output,
98-
TIME_REGEX_FORMATTED,
99-
getDate({ format })
100-
)
88+
output = this.replacer(output, TIME_REGEX_FORMATTED, getDate({ format }));
10189
}
10290

10391
return output;
10492
}
10593

106-
protected abstract promptForValue(
107-
header?: string
108-
): Promise<string> | string;
94+
protected abstract promptForValue(header?: string): Promise<string> | string;
10995

11096
protected async replaceValueInString(input: string): Promise<string> {
11197
let output: string = input;
11298

99+
if (this.variables.has("value")) {
100+
this.value = this.variables.get("value") as string;
101+
}
102+
113103
while (NAME_VALUE_REGEX.test(output)) {
114104
if (!this.value) this.value = await this.promptForValue();
115105

@@ -119,7 +109,6 @@ export abstract class Formatter {
119109
return output;
120110
}
121111

122-
123112
protected async replaceSelectedInString(input: string): Promise<string> {
124113
let output: string = input;
125114

@@ -134,7 +123,7 @@ export abstract class Formatter {
134123

135124
// eslint-disable-next-line @typescript-eslint/require-await
136125
protected async replaceLinkToCurrentFileInString(
137-
input: string
126+
input: string,
138127
): Promise<string> {
139128
const currentFilePathLink = this.getCurrentFileLink();
140129
let output = input;
@@ -147,7 +136,7 @@ export abstract class Formatter {
147136
output = this.replacer(
148137
output,
149138
LINK_TO_CURRENT_FILE_REGEX,
150-
currentFilePathLink
139+
currentFilePathLink,
151140
);
152141

153142
return output;
@@ -171,19 +160,19 @@ export abstract class Formatter {
171160
if (suggestedValues.length === 1)
172161
this.variables.set(
173162
variableName,
174-
await this.promptForVariable(variableName)
163+
await this.promptForVariable(variableName),
175164
);
176165
else
177166
this.variables.set(
178167
variableName,
179-
await this.suggestForValue(suggestedValues)
168+
await this.suggestForValue(suggestedValues),
180169
);
181170
}
182171

183172
output = this.replacer(
184173
output,
185174
VARIABLE_REGEX,
186-
this.getVariableValue(variableName)
175+
this.getVariableValue(variableName),
187176
);
188177
} else {
189178
break;
@@ -206,14 +195,14 @@ export abstract class Formatter {
206195
if (!this.getVariableValue(variableName)) {
207196
this.variables.set(
208197
variableName,
209-
await this.suggestForField(variableName)
198+
await this.suggestForField(variableName),
210199
);
211200
}
212201

213202
output = this.replacer(
214203
output,
215204
FIELD_VAR_REGEX,
216-
this.getVariableValue(variableName)
205+
this.getVariableValue(variableName),
217206
);
218207
} else {
219208
break;
@@ -249,7 +238,7 @@ export abstract class Formatter {
249238
output = this.replacer(
250239
output,
251240
MACRO_REGEX,
252-
macroOutput ? macroOutput.toString() : ""
241+
macroOutput ? macroOutput.toString() : "",
253242
);
254243
}
255244

@@ -259,7 +248,7 @@ export abstract class Formatter {
259248
protected abstract getVariableValue(variableName: string): string;
260249

261250
protected abstract suggestForValue(
262-
suggestedValues: string[]
251+
suggestedValues: string[],
263252
): Promise<string> | string;
264253

265254
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -279,16 +268,12 @@ export abstract class Formatter {
279268
if (!this.variables.get(variableName)) {
280269
this.variables.set(
281270
variableName,
282-
await this.promptForVariable(variableName)
271+
await this.promptForVariable(variableName),
283272
);
284273

285274
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
286275
const nld = this.getNaturalLanguageDates();
287-
if (
288-
!nld ||
289-
!nld.parseDate ||
290-
typeof nld.parseDate !== "function"
291-
)
276+
if (!nld || !nld.parseDate || typeof nld.parseDate !== "function")
292277
continue;
293278

294279
const parseAttempt = (
@@ -300,20 +285,20 @@ export abstract class Formatter {
300285
if (parseAttempt)
301286
this.variables.set(
302287
variableName,
303-
parseAttempt.moment.format(dateFormat)
288+
parseAttempt.moment.format(dateFormat),
304289
);
305290
else
306291
throw new Error(
307292
`unable to parse date variable ${this.variables.get(
308-
variableName
309-
)}`
293+
variableName,
294+
)}`,
310295
);
311296
}
312297

313298
output = this.replacer(
314299
output,
315300
DATE_VARIABLE_REGEX,
316-
this.variables.get(variableName) as string // literally setting it above / throwing error if not set
301+
this.variables.get(variableName) as string, // literally setting it above / throwing error if not set
317302
);
318303
} else {
319304
break;
@@ -359,15 +344,11 @@ export abstract class Formatter {
359344
// eslint-disable-next-line @typescript-eslint/no-explicit-any
360345
protected abstract getNaturalLanguageDates(): any;
361346

362-
protected abstract getMacroValue(
363-
macroName: string
364-
): Promise<string> | string;
347+
protected abstract getMacroValue(macroName: string): Promise<string> | string;
365348

366349
protected abstract promptForVariable(variableName: string): Promise<string>;
367350

368-
protected abstract getTemplateContent(
369-
templatePath: string
370-
): Promise<string>;
351+
protected abstract getTemplateContent(templatePath: string): Promise<string>;
371352

372353
protected abstract getSelectedText(): Promise<string>;
373354
}

0 commit comments

Comments
 (0)