Skip to content

Commit 9e7cc42

Browse files
jamesgeorge007isaacs
authored andcommitted
chore: migrate to leven
Credit: @jamesgeorge007 PR-URL: #1071 Close: #1071 Reviewed-By: @isaacs EDIT(@isaacs): changed dependency in a separate commit
1 parent 354e4c0 commit 9e7cc42

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

lib/utils/did-you-mean.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
var meant = require('meant')
1+
const leven = require('leven')
22

3-
function didYouMean (scmd, commands) {
4-
var bestSimilarity = meant(scmd, commands).map(function (str) {
5-
return ' ' + str
6-
})
7-
8-
if (bestSimilarity.length === 0) return ''
9-
if (bestSimilarity.length === 1) {
10-
return '\nDid you mean this?\n' + bestSimilarity[0]
11-
} else {
12-
return ['\nDid you mean one of these?']
13-
.concat(bestSimilarity.slice(0, 3)).join('\n')
14-
}
3+
const didYouMean = (scmd, commands) => {
4+
const best = commands
5+
.filter(cmd => leven(scmd, cmd) < scmd.length * 0.4)
6+
.map(str => ` ${str}`)
7+
return best.length === 0 ? ''
8+
: best.length === 1 ? `\nDid you mean this?\n${best[0]}`
9+
: `\nDid you mean one of these?\n${best.slice(0, 3).join('\n')}`
1510
}
1611

1712
module.exports = didYouMean

test/lib/utils/did-you-mean.js

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

0 commit comments

Comments
 (0)