@optique/core 1.2.0-dev.2246 → 1.2.0-dev.2250
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/suggestion.cjs +36 -18
- package/dist/suggestion.d.cts +20 -5
- package/dist/suggestion.d.ts +20 -5
- package/dist/suggestion.js +36 -19
- package/dist/valueparser.cjs +254 -266
- package/dist/valueparser.js +256 -268
- package/package.json +2 -2
package/dist/suggestion.cjs
CHANGED
|
@@ -251,14 +251,44 @@ function createErrorWithSuggestions(baseError, invalidInput, usage, type = "both
|
|
|
251
251
|
] : baseError;
|
|
252
252
|
}
|
|
253
253
|
/**
|
|
254
|
+
* Appends a "Did you mean …?" hint to a base message from a pre-filtered list
|
|
255
|
+
* of suggestion strings.
|
|
256
|
+
*
|
|
257
|
+
* Unlike {@link appendValueHint}, this function accepts suggestions that have
|
|
258
|
+
* already been selected by the caller—it only handles formatting and
|
|
259
|
+
* appending. Useful when the caller provides its own distance logic (e.g.,
|
|
260
|
+
* the custom-function form of `choice()`'s `suggest` option).
|
|
261
|
+
*
|
|
262
|
+
* @param base The base error message.
|
|
263
|
+
* @param suggestions The already-filtered suggestion strings to display.
|
|
264
|
+
* @returns `base` with a "Did you mean …?" line appended, or `base` unchanged
|
|
265
|
+
* when `suggestions` is empty.
|
|
266
|
+
* @since 1.2.0
|
|
267
|
+
*/
|
|
268
|
+
function appendValueSuggestions(base, suggestions) {
|
|
269
|
+
if (suggestions.length === 0) return base;
|
|
270
|
+
let suggestionMsg;
|
|
271
|
+
if (suggestions.length === 1) suggestionMsg = require_message.message`Did you mean ${require_message.value(suggestions[0])}?`;
|
|
272
|
+
else {
|
|
273
|
+
const parts = [require_message.text("Did you mean one of these?")];
|
|
274
|
+
for (const suggestion of suggestions) parts.push(require_message.lineBreak(), require_message.text(" "), require_message.value(suggestion));
|
|
275
|
+
suggestionMsg = parts;
|
|
276
|
+
}
|
|
277
|
+
return base.length > 0 ? [
|
|
278
|
+
...base,
|
|
279
|
+
require_message.lineBreak(),
|
|
280
|
+
require_message.lineBreak(),
|
|
281
|
+
...suggestionMsg
|
|
282
|
+
] : suggestionMsg;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
254
285
|
* Appends a "Did you mean …?" hint to a base error message when the input
|
|
255
286
|
* is close to one of the candidate values.
|
|
256
287
|
*
|
|
257
288
|
* This helper is meant for closed-set value parsers (e.g. `choice()`) that
|
|
258
289
|
* want to surface near-match suggestions without re-implementing distance
|
|
259
|
-
* logic themselves. It wraps {@link findSimilar} and
|
|
260
|
-
*
|
|
261
|
-
* when no candidates are close enough.
|
|
290
|
+
* logic themselves. It wraps {@link findSimilar} and returns the `base`
|
|
291
|
+
* message unchanged when no candidates are close enough.
|
|
262
292
|
*
|
|
263
293
|
* @param base The base error message to display.
|
|
264
294
|
* @param input The invalid input the user typed.
|
|
@@ -272,7 +302,7 @@ function createErrorWithSuggestions(baseError, invalidInput, usage, type = "both
|
|
|
272
302
|
* ```typescript
|
|
273
303
|
* const base = message`Invalid color: ${input}.`;
|
|
274
304
|
* appendValueHint(base, input, ["red", "green", "blue"]);
|
|
275
|
-
* // → "Invalid color: redd.\n\nDid you mean
|
|
305
|
+
* // → "Invalid color: redd.\n\nDid you mean \"red\"?"
|
|
276
306
|
* ```
|
|
277
307
|
*
|
|
278
308
|
* @since 1.2.0
|
|
@@ -283,20 +313,7 @@ function appendValueHint(base, input, candidates, options) {
|
|
|
283
313
|
maxDistanceRatio: DEFAULT_FIND_SIMILAR_OPTIONS.maxDistanceRatio,
|
|
284
314
|
maxSuggestions: options.maxSuggestions ?? DEFAULT_FIND_SIMILAR_OPTIONS.maxSuggestions
|
|
285
315
|
} : void 0);
|
|
286
|
-
|
|
287
|
-
let suggestionMsg;
|
|
288
|
-
if (suggestions.length === 1) suggestionMsg = require_message.message`Did you mean ${require_message.value(suggestions[0])}?`;
|
|
289
|
-
else {
|
|
290
|
-
const parts = [require_message.text("Did you mean one of these?")];
|
|
291
|
-
for (const suggestion of suggestions) parts.push(require_message.text("\n "), require_message.value(suggestion));
|
|
292
|
-
suggestionMsg = parts;
|
|
293
|
-
}
|
|
294
|
-
return base.length > 0 ? [
|
|
295
|
-
...base,
|
|
296
|
-
require_message.lineBreak(),
|
|
297
|
-
require_message.lineBreak(),
|
|
298
|
-
...suggestionMsg
|
|
299
|
-
] : suggestionMsg;
|
|
316
|
+
return appendValueSuggestions(base, suggestions);
|
|
300
317
|
}
|
|
301
318
|
/**
|
|
302
319
|
* Creates a unique key for a suggestion to enable deduplication.
|
|
@@ -362,6 +379,7 @@ function deduplicateSuggestions(suggestions) {
|
|
|
362
379
|
//#endregion
|
|
363
380
|
exports.DEFAULT_FIND_SIMILAR_OPTIONS = DEFAULT_FIND_SIMILAR_OPTIONS;
|
|
364
381
|
exports.appendValueHint = appendValueHint;
|
|
382
|
+
exports.appendValueSuggestions = appendValueSuggestions;
|
|
365
383
|
exports.createErrorWithSuggestions = createErrorWithSuggestions;
|
|
366
384
|
exports.createSuggestionMessage = createSuggestionMessage;
|
|
367
385
|
exports.deduplicateSuggestions = deduplicateSuggestions;
|
package/dist/suggestion.d.cts
CHANGED
|
@@ -154,15 +154,30 @@ declare function expandCommandAliasSuggestions(usage: Usage, suggestions: readon
|
|
|
154
154
|
* @since 0.7.0
|
|
155
155
|
*/
|
|
156
156
|
declare function createErrorWithSuggestions(baseError: Message, invalidInput: string, usage: Usage, type?: "option" | "command" | "both", customFormatter?: (suggestions: readonly string[]) => Message): Message;
|
|
157
|
+
/**
|
|
158
|
+
* Appends a "Did you mean …?" hint to a base message from a pre-filtered list
|
|
159
|
+
* of suggestion strings.
|
|
160
|
+
*
|
|
161
|
+
* Unlike {@link appendValueHint}, this function accepts suggestions that have
|
|
162
|
+
* already been selected by the caller—it only handles formatting and
|
|
163
|
+
* appending. Useful when the caller provides its own distance logic (e.g.,
|
|
164
|
+
* the custom-function form of `choice()`'s `suggest` option).
|
|
165
|
+
*
|
|
166
|
+
* @param base The base error message.
|
|
167
|
+
* @param suggestions The already-filtered suggestion strings to display.
|
|
168
|
+
* @returns `base` with a "Did you mean …?" line appended, or `base` unchanged
|
|
169
|
+
* when `suggestions` is empty.
|
|
170
|
+
* @since 1.2.0
|
|
171
|
+
*/
|
|
172
|
+
declare function appendValueSuggestions(base: Message, suggestions: readonly string[]): Message;
|
|
157
173
|
/**
|
|
158
174
|
* Appends a "Did you mean …?" hint to a base error message when the input
|
|
159
175
|
* is close to one of the candidate values.
|
|
160
176
|
*
|
|
161
177
|
* This helper is meant for closed-set value parsers (e.g. `choice()`) that
|
|
162
178
|
* want to surface near-match suggestions without re-implementing distance
|
|
163
|
-
* logic themselves. It wraps {@link findSimilar} and
|
|
164
|
-
*
|
|
165
|
-
* when no candidates are close enough.
|
|
179
|
+
* logic themselves. It wraps {@link findSimilar} and returns the `base`
|
|
180
|
+
* message unchanged when no candidates are close enough.
|
|
166
181
|
*
|
|
167
182
|
* @param base The base error message to display.
|
|
168
183
|
* @param input The invalid input the user typed.
|
|
@@ -176,7 +191,7 @@ declare function createErrorWithSuggestions(baseError: Message, invalidInput: st
|
|
|
176
191
|
* ```typescript
|
|
177
192
|
* const base = message`Invalid color: ${input}.`;
|
|
178
193
|
* appendValueHint(base, input, ["red", "green", "blue"]);
|
|
179
|
-
* // → "Invalid color: redd.\n\nDid you mean
|
|
194
|
+
* // → "Invalid color: redd.\n\nDid you mean \"red\"?"
|
|
180
195
|
* ```
|
|
181
196
|
*
|
|
182
197
|
* @since 1.2.0
|
|
@@ -213,4 +228,4 @@ declare function appendValueHint(base: Message, input: string, candidates: reado
|
|
|
213
228
|
*/
|
|
214
229
|
declare function deduplicateSuggestions(suggestions: readonly Suggestion[]): Suggestion[];
|
|
215
230
|
//#endregion
|
|
216
|
-
export { DEFAULT_FIND_SIMILAR_OPTIONS, FindSimilarOptions, appendValueHint, createErrorWithSuggestions, createSuggestionMessage, deduplicateSuggestions, expandCommandAliasSuggestions, findSimilar, levenshteinDistance };
|
|
231
|
+
export { DEFAULT_FIND_SIMILAR_OPTIONS, FindSimilarOptions, appendValueHint, appendValueSuggestions, createErrorWithSuggestions, createSuggestionMessage, deduplicateSuggestions, expandCommandAliasSuggestions, findSimilar, levenshteinDistance };
|
package/dist/suggestion.d.ts
CHANGED
|
@@ -154,15 +154,30 @@ declare function expandCommandAliasSuggestions(usage: Usage, suggestions: readon
|
|
|
154
154
|
* @since 0.7.0
|
|
155
155
|
*/
|
|
156
156
|
declare function createErrorWithSuggestions(baseError: Message, invalidInput: string, usage: Usage, type?: "option" | "command" | "both", customFormatter?: (suggestions: readonly string[]) => Message): Message;
|
|
157
|
+
/**
|
|
158
|
+
* Appends a "Did you mean …?" hint to a base message from a pre-filtered list
|
|
159
|
+
* of suggestion strings.
|
|
160
|
+
*
|
|
161
|
+
* Unlike {@link appendValueHint}, this function accepts suggestions that have
|
|
162
|
+
* already been selected by the caller—it only handles formatting and
|
|
163
|
+
* appending. Useful when the caller provides its own distance logic (e.g.,
|
|
164
|
+
* the custom-function form of `choice()`'s `suggest` option).
|
|
165
|
+
*
|
|
166
|
+
* @param base The base error message.
|
|
167
|
+
* @param suggestions The already-filtered suggestion strings to display.
|
|
168
|
+
* @returns `base` with a "Did you mean …?" line appended, or `base` unchanged
|
|
169
|
+
* when `suggestions` is empty.
|
|
170
|
+
* @since 1.2.0
|
|
171
|
+
*/
|
|
172
|
+
declare function appendValueSuggestions(base: Message, suggestions: readonly string[]): Message;
|
|
157
173
|
/**
|
|
158
174
|
* Appends a "Did you mean …?" hint to a base error message when the input
|
|
159
175
|
* is close to one of the candidate values.
|
|
160
176
|
*
|
|
161
177
|
* This helper is meant for closed-set value parsers (e.g. `choice()`) that
|
|
162
178
|
* want to surface near-match suggestions without re-implementing distance
|
|
163
|
-
* logic themselves. It wraps {@link findSimilar} and
|
|
164
|
-
*
|
|
165
|
-
* when no candidates are close enough.
|
|
179
|
+
* logic themselves. It wraps {@link findSimilar} and returns the `base`
|
|
180
|
+
* message unchanged when no candidates are close enough.
|
|
166
181
|
*
|
|
167
182
|
* @param base The base error message to display.
|
|
168
183
|
* @param input The invalid input the user typed.
|
|
@@ -176,7 +191,7 @@ declare function createErrorWithSuggestions(baseError: Message, invalidInput: st
|
|
|
176
191
|
* ```typescript
|
|
177
192
|
* const base = message`Invalid color: ${input}.`;
|
|
178
193
|
* appendValueHint(base, input, ["red", "green", "blue"]);
|
|
179
|
-
* // → "Invalid color: redd.\n\nDid you mean
|
|
194
|
+
* // → "Invalid color: redd.\n\nDid you mean \"red\"?"
|
|
180
195
|
* ```
|
|
181
196
|
*
|
|
182
197
|
* @since 1.2.0
|
|
@@ -213,4 +228,4 @@ declare function appendValueHint(base: Message, input: string, candidates: reado
|
|
|
213
228
|
*/
|
|
214
229
|
declare function deduplicateSuggestions(suggestions: readonly Suggestion[]): Suggestion[];
|
|
215
230
|
//#endregion
|
|
216
|
-
export { DEFAULT_FIND_SIMILAR_OPTIONS, FindSimilarOptions, appendValueHint, createErrorWithSuggestions, createSuggestionMessage, deduplicateSuggestions, expandCommandAliasSuggestions, findSimilar, levenshteinDistance };
|
|
231
|
+
export { DEFAULT_FIND_SIMILAR_OPTIONS, FindSimilarOptions, appendValueHint, appendValueSuggestions, createErrorWithSuggestions, createSuggestionMessage, deduplicateSuggestions, expandCommandAliasSuggestions, findSimilar, levenshteinDistance };
|
package/dist/suggestion.js
CHANGED
|
@@ -251,14 +251,44 @@ function createErrorWithSuggestions(baseError, invalidInput, usage, type = "both
|
|
|
251
251
|
] : baseError;
|
|
252
252
|
}
|
|
253
253
|
/**
|
|
254
|
+
* Appends a "Did you mean …?" hint to a base message from a pre-filtered list
|
|
255
|
+
* of suggestion strings.
|
|
256
|
+
*
|
|
257
|
+
* Unlike {@link appendValueHint}, this function accepts suggestions that have
|
|
258
|
+
* already been selected by the caller—it only handles formatting and
|
|
259
|
+
* appending. Useful when the caller provides its own distance logic (e.g.,
|
|
260
|
+
* the custom-function form of `choice()`'s `suggest` option).
|
|
261
|
+
*
|
|
262
|
+
* @param base The base error message.
|
|
263
|
+
* @param suggestions The already-filtered suggestion strings to display.
|
|
264
|
+
* @returns `base` with a "Did you mean …?" line appended, or `base` unchanged
|
|
265
|
+
* when `suggestions` is empty.
|
|
266
|
+
* @since 1.2.0
|
|
267
|
+
*/
|
|
268
|
+
function appendValueSuggestions(base, suggestions) {
|
|
269
|
+
if (suggestions.length === 0) return base;
|
|
270
|
+
let suggestionMsg;
|
|
271
|
+
if (suggestions.length === 1) suggestionMsg = message`Did you mean ${value(suggestions[0])}?`;
|
|
272
|
+
else {
|
|
273
|
+
const parts = [text("Did you mean one of these?")];
|
|
274
|
+
for (const suggestion of suggestions) parts.push(lineBreak(), text(" "), value(suggestion));
|
|
275
|
+
suggestionMsg = parts;
|
|
276
|
+
}
|
|
277
|
+
return base.length > 0 ? [
|
|
278
|
+
...base,
|
|
279
|
+
lineBreak(),
|
|
280
|
+
lineBreak(),
|
|
281
|
+
...suggestionMsg
|
|
282
|
+
] : suggestionMsg;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
254
285
|
* Appends a "Did you mean …?" hint to a base error message when the input
|
|
255
286
|
* is close to one of the candidate values.
|
|
256
287
|
*
|
|
257
288
|
* This helper is meant for closed-set value parsers (e.g. `choice()`) that
|
|
258
289
|
* want to surface near-match suggestions without re-implementing distance
|
|
259
|
-
* logic themselves. It wraps {@link findSimilar} and
|
|
260
|
-
*
|
|
261
|
-
* when no candidates are close enough.
|
|
290
|
+
* logic themselves. It wraps {@link findSimilar} and returns the `base`
|
|
291
|
+
* message unchanged when no candidates are close enough.
|
|
262
292
|
*
|
|
263
293
|
* @param base The base error message to display.
|
|
264
294
|
* @param input The invalid input the user typed.
|
|
@@ -272,7 +302,7 @@ function createErrorWithSuggestions(baseError, invalidInput, usage, type = "both
|
|
|
272
302
|
* ```typescript
|
|
273
303
|
* const base = message`Invalid color: ${input}.`;
|
|
274
304
|
* appendValueHint(base, input, ["red", "green", "blue"]);
|
|
275
|
-
* // → "Invalid color: redd.\n\nDid you mean
|
|
305
|
+
* // → "Invalid color: redd.\n\nDid you mean \"red\"?"
|
|
276
306
|
* ```
|
|
277
307
|
*
|
|
278
308
|
* @since 1.2.0
|
|
@@ -283,20 +313,7 @@ function appendValueHint(base, input, candidates, options) {
|
|
|
283
313
|
maxDistanceRatio: DEFAULT_FIND_SIMILAR_OPTIONS.maxDistanceRatio,
|
|
284
314
|
maxSuggestions: options.maxSuggestions ?? DEFAULT_FIND_SIMILAR_OPTIONS.maxSuggestions
|
|
285
315
|
} : void 0);
|
|
286
|
-
|
|
287
|
-
let suggestionMsg;
|
|
288
|
-
if (suggestions.length === 1) suggestionMsg = message`Did you mean ${value(suggestions[0])}?`;
|
|
289
|
-
else {
|
|
290
|
-
const parts = [text("Did you mean one of these?")];
|
|
291
|
-
for (const suggestion of suggestions) parts.push(text("\n "), value(suggestion));
|
|
292
|
-
suggestionMsg = parts;
|
|
293
|
-
}
|
|
294
|
-
return base.length > 0 ? [
|
|
295
|
-
...base,
|
|
296
|
-
lineBreak(),
|
|
297
|
-
lineBreak(),
|
|
298
|
-
...suggestionMsg
|
|
299
|
-
] : suggestionMsg;
|
|
316
|
+
return appendValueSuggestions(base, suggestions);
|
|
300
317
|
}
|
|
301
318
|
/**
|
|
302
319
|
* Creates a unique key for a suggestion to enable deduplication.
|
|
@@ -360,4 +377,4 @@ function deduplicateSuggestions(suggestions) {
|
|
|
360
377
|
}
|
|
361
378
|
|
|
362
379
|
//#endregion
|
|
363
|
-
export { DEFAULT_FIND_SIMILAR_OPTIONS, appendValueHint, createErrorWithSuggestions, createSuggestionMessage, deduplicateSuggestions, expandCommandAliasSuggestions, findSimilar, levenshteinDistance };
|
|
380
|
+
export { DEFAULT_FIND_SIMILAR_OPTIONS, appendValueHint, appendValueSuggestions, createErrorWithSuggestions, createSuggestionMessage, deduplicateSuggestions, expandCommandAliasSuggestions, findSimilar, levenshteinDistance };
|