Skip to content

Commit

Permalink
feat(kernel): add support for command suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Apr 12, 2019
1 parent 83877b5 commit 009a037
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/ace/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/ace/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"license": "MIT",
"dependencies": {
"fast-levenshtein": "^2.0.6",
"getopts": "^2.2.4",
"kleur": "^3.0.3",
"pad-right": "^0.2.2"
Expand Down
8 changes: 8 additions & 0 deletions packages/ace/src/Kernel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export class Kernel {
return this
}

/**
* Returns an array of command names suggestions for a given name.
*/
public getSuggestions (name: string, distance = 3): string[] {
const levenshtein = require('fast-levenshtein')
return Object.keys(this.commands).filter((commandName) => levenshtein.get(name, commandName) <= distance)
}

/**
* Register a global flag to be set on any command. The flag callback is
* executed before executing the registered command.
Expand Down
14 changes: 12 additions & 2 deletions packages/ace/test/kernel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,19 @@ test.group('Kernel | register', () => {
assert.throw(fn, 'Required argument {age} cannot come after optional argument {name}')
})

test('return null when unable to find command', (assert) => {
test('return command suggestions for a given string', (assert) => {
const kernel = new Kernel()
assert.isNull(kernel.find(['greet']))

class Install extends BaseCommand {
public static commandName = 'install'
}

class Greet extends BaseCommand {
public static commandName = 'greet'
}

kernel.register([Install, Greet])
assert.deepEqual(kernel.getSuggestions('itall'), ['install'])
})
})

Expand Down

0 comments on commit 009a037

Please sign in to comment.