Skip to content

Commit

Permalink
chore: migrate to leven
Browse files Browse the repository at this point in the history
Credit: @jamesgeorge007
PR-URL: #1071
Close: #1071
Reviewed-By: @isaacs

EDIT(@isaacs): changed dependency in a separate commit
  • Loading branch information
jamesgeorge007 authored and isaacs committed Aug 18, 2020
1 parent 354e4c0 commit 9e7cc42
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
21 changes: 8 additions & 13 deletions lib/utils/did-you-mean.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
var meant = require('meant')
const leven = require('leven')

function didYouMean (scmd, commands) {
var bestSimilarity = meant(scmd, commands).map(function (str) {
return ' ' + str
})

if (bestSimilarity.length === 0) return ''
if (bestSimilarity.length === 1) {
return '\nDid you mean this?\n' + bestSimilarity[0]
} else {
return ['\nDid you mean one of these?']
.concat(bestSimilarity.slice(0, 3)).join('\n')
}
const didYouMean = (scmd, commands) => {
const best = commands
.filter(cmd => leven(scmd, cmd) < scmd.length * 0.4)
.map(str => ` ${str}`)
return best.length === 0 ? ''
: best.length === 1 ? `\nDid you mean this?\n${best[0]}`
: `\nDid you mean one of these?\n${best.slice(0, 3).join('\n')}`
}

module.exports = didYouMean
7 changes: 7 additions & 0 deletions test/lib/utils/did-you-mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const t = require('tap')
const dym = require('../../../lib/utils/did-you-mean.js')
t.equal(dym('asdfa', ['asdf', 'asfd', 'adfs', 'safd', 'foobarbaz', 'foobar']),
'\nDid you mean this?\n asdf')
t.equal(dym('asdfa', ['asdf', 'sdfa', 'foo', 'bar', 'fdsa']),
'\nDid you mean one of these?\n asdf\n sdfa')
t.equal(dym('asdfa', ['install', 'list', 'test']), '')

0 comments on commit 9e7cc42

Please sign in to comment.