Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec66bc3
commit c025388
Showing
6 changed files
with
137 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import confirm from '@inquirer/confirm' | ||
import chalk from 'chalk' | ||
import {default as levenshtein} from 'fast-levenshtein' | ||
import {setTimeout} from 'node:timers/promises' | ||
|
||
const getConfirmation = async (suggestion: string): Promise<boolean> => { | ||
const confirmation = confirm({ | ||
default: true, | ||
message: `Did you mean ${chalk.blueBright(suggestion)}?`, | ||
theme: { | ||
prefix: '', | ||
style: { | ||
message: (text: string) => chalk.reset(text), | ||
}, | ||
}, | ||
}) | ||
|
||
const defaultValue = setTimeout(10_000).then(() => { | ||
confirmation.cancel() | ||
return false | ||
}) | ||
|
||
return Promise.race([defaultValue, confirmation]) | ||
} | ||
|
||
const closest = (target: string, possibilities: string[]): string => | ||
possibilities | ||
.map((id) => ({distance: levenshtein.get(target, id, {useCollator: true}), id})) | ||
.sort((a, b) => a.distance - b.distance)[0]?.id ?? '' | ||
|
||
export default { | ||
closest, | ||
getConfirmation, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
import {expect} from 'chai' | ||
|
||
import {closest} from '../src/index.js' | ||
import utils from '../src/utils.js' | ||
|
||
describe('closest', () => { | ||
const possibilities = ['abc', 'def', 'ghi', 'jkl', 'jlk'] | ||
it('exact match', () => { | ||
expect(closest('abc', possibilities)).to.equal('abc') | ||
expect(utils.closest('abc', possibilities)).to.equal('abc') | ||
}) | ||
|
||
it('case mistake', () => { | ||
expect(closest('aBc', possibilities)).to.equal('abc') | ||
expect(utils.closest('aBc', possibilities)).to.equal('abc') | ||
}) | ||
|
||
it('case match one letter', () => { | ||
expect(closest('aZZ', possibilities)).to.equal('abc') | ||
expect(utils.closest('aZZ', possibilities)).to.equal('abc') | ||
}) | ||
|
||
it('one letter different mistake', () => { | ||
expect(closest('ggi', possibilities)).to.equal('ghi') | ||
expect(utils.closest('ggi', possibilities)).to.equal('ghi') | ||
}) | ||
|
||
it('two letter different mistake', () => { | ||
expect(closest('gki', possibilities)).to.equal('ghi') | ||
expect(utils.closest('gki', possibilities)).to.equal('ghi') | ||
}) | ||
|
||
it('extra letter', () => { | ||
expect(closest('gkui', possibilities)).to.equal('ghi') | ||
expect(utils.closest('gkui', possibilities)).to.equal('ghi') | ||
}) | ||
|
||
it('two letter different mistake with close neighbor', () => { | ||
expect(closest('jpp', possibilities)).to.equal('jkl') | ||
expect(utils.closest('jpp', possibilities)).to.equal('jkl') | ||
}) | ||
|
||
it('no possibilities gives empty string', () => { | ||
expect(closest('jpp', [])).to.equal('') | ||
expect(utils.closest('jpp', [])).to.equal('') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters