-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Credit: @jamesgeorge007 PR-URL: #1071 Close: #1071 Reviewed-By: @isaacs EDIT(@isaacs): changed dependency in a separate commit
- Loading branch information
1 parent
354e4c0
commit 9e7cc42
Showing
2 changed files
with
15 additions
and
13 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
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 |
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,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']), '') |