Skip to content

Commit

Permalink
chore: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Mar 1, 2022
1 parent 6f0232a commit dec69c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Options, Plugin as IPlugin} from '../interfaces/plugin'
import {Config as IConfig, ArchTypes, PlatformTypes, LoadOptions} from '../interfaces/config'
import {Command, Hook, Hooks, PJSON, Topic} from '../interfaces'
import * as Plugin from './plugin'
import {Debug, compact, loadJSON, collectUsableParts, getCommandIdPermutations} from './util'
import {Debug, compact, loadJSON, collectUsableIds, getCommandIdPermutations} from './util'
import {isProd} from '../util'
import ModuleLoader from '../module-loader'
import {getHelpFlagAdditions} from '../help/util'
Expand All @@ -35,7 +35,7 @@ class PermutationIndex extends Map<string, Set<string>> {

public add(permutation: string, commandId: string): void {
this.validPermutations.set(permutation, commandId)
for (const part of collectUsableParts([permutation])) {
for (const part of collectUsableIds([permutation])) {
if (this.has(part)) {
this.set(part, this.get(part).add(commandId))
} else {
Expand Down Expand Up @@ -428,7 +428,7 @@ export class Config implements IConfig {
* @returns string[]
*/
collectUsableIds(): string[] {
return collectUsableParts(this.commandIDs)
return collectUsableIds(this.commandIDs)
}

/**
Expand Down
21 changes: 20 additions & 1 deletion src/config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,26 @@ export function getCommandIdPermutations(commandId: string): string[] {
return getPerumtations(commandId.split(':')).flatMap(c => c.join(':'))
}

export function collectUsableParts(items: string[]): string[] {
/**
* Return an array of ids that represent all the usable combinations that a user could enter.
*
* For example, if the command ids are:
* - foo:bar:baz
* - one:two:three
* Then the usable ids would be:
* - foo
* - foo:bar
* - foo:bar:baz
* - one
* - one:two
* - one:two:three
*
* This allows us to determine which parts of the argv array belong to the command id whenever the topicSeparator is a space.
*
* @param items string[]
* @returns string[]
*/
export function collectUsableIds(items: string[]): string[] {
const final: string[] = []
for (const item of items) {
const parts = item.split(':')
Expand Down

0 comments on commit dec69c4

Please sign in to comment.