UNPKG

1.04 kBJavaScriptView Raw
1import confirm from '@inquirer/confirm';
2import chalk from 'chalk';
3import { default as levenshtein } from 'fast-levenshtein';
4import { setTimeout } from 'node:timers/promises';
5const getConfirmation = async (suggestion) => {
6 const ac = new AbortController();
7 const { signal } = ac;
8 const confirmation = confirm({
9 default: true,
10 message: `Did you mean ${chalk.blueBright(suggestion)}?`,
11 theme: {
12 prefix: '',
13 style: {
14 message: (text) => chalk.reset(text),
15 },
16 },
17 });
18 setTimeout(10_000, 'timeout', { signal })
19 .catch(() => false)
20 .then(() => confirmation.cancel());
21 return confirmation.then((value) => {
22 ac.abort();
23 return value;
24 });
25};
26const closest = (target, possibilities) => possibilities
27 .map((id) => ({ distance: levenshtein.get(target, id, { useCollator: true }), id }))
28 .sort((a, b) => a.distance - b.distance)[0]?.id ?? '';
29export default {
30 closest,
31 getConfirmation,
32};