export function getCommandName()

in powershell/internal/name-inferrer.ts [248:347]


export function getCommandName(operationId: string, onMessage: (message: Message) => void): Array<{ noun?: string; verb: string; variant: string }> {
  const opIdValues = operationId.split('_', 2);

  // OperationId can be specified without '_' (Underscore), Verb will retrieved by the below logic for non-approved verbs.
  let cmdNoun = length(opIdValues) === 2 ? getSingularizedValue(opIdValues[0]) : '';
  let cmdVerb = length(opIdValues) === 2 ? opIdValues[1] : getSingularizedValue(operationId);
  let cmdVerbs: Array<string> = [cmdVerb];
  const variant = operationId;

  if (!Object
    .keys(cmdVerbMapGetVerb)
    .map(v => v.toLowerCase())
    .includes(cmdVerb.toLowerCase())) {

    const unapprovedVerb = cmdVerb;
    onMessage({ Channel: Channel.Information, Text: `Operation '${operationId}': Verb '${unapprovedVerb}' not an approved verb.` });

    if (Object
      .keys(cmdVerbMapCustom)
      .map(v => v.toLowerCase())
      .includes(cmdVerb.toLowerCase())) {
      // This condition happens when there aren't any suffixes
      cmdVerbs = mapVerb(cmdVerb);
      for (const v of cmdVerbs) {
        onMessage({ Channel: Channel.Information, Text: `Operation '${operationId}': Using verb '${v}' in place of '${unapprovedVerb}'.` });
      }
    } else {
      // This condition happens in cases like: CreateSuffix, CreateOrUpdateSuffix
      let longestVerbMatch: string | null = null;
      let currentVerbCandidate = '';
      let firstWord = '';
      let firstWordStarted = false;
      let buildFirstWord = false;
      let firstWordEnd = -1;
      let verbMatchEnd = -1;
      for (let i = 0; i < length(unapprovedVerb); ++i) {
        // Add the start condition of the first word so that the end condition is easier
        if (!firstWordStarted) {
          firstWordStarted = true;
          buildFirstWord = true;
        } else if (buildFirstWord && (unapprovedVerb.charCodeAt(i) >= 65) && (unapprovedVerb.charCodeAt(i) <= 90)) {
          // Stop building the first word when we encounter another capital letter
          buildFirstWord = false;
          firstWordEnd = i;
        }

        if (buildFirstWord) {
          firstWord += unapprovedVerb.charAt(i);
        }

        currentVerbCandidate += unapprovedVerb.charAt(i);
        if (existsVerb(currentVerbCandidate)) {
          // The latest verb match is also the longest verb match
          longestVerbMatch = currentVerbCandidate;
          verbMatchEnd = i + 1;
        }
      }

      const beginningOfSuffix = longestVerbMatch ? verbMatchEnd : firstWordEnd;
      cmdVerb = longestVerbMatch ? longestVerbMatch : firstWord;

      if (Object
        .keys(cmdVerbMapCustom)
        .map(v => v.toLowerCase())
        .includes(cmdVerb.toLowerCase())) {
        cmdVerbs = mapVerb(cmdVerb);
      } else {
        cmdVerbs = [cmdVerb];
      }

      if (-1 !== beginningOfSuffix) {
        // This is still empty when a verb match is found that is the entire string, but it might not be worth checking for that case and skipping the below operation
        const cmdNounSuffix = unapprovedVerb.substring(beginningOfSuffix);
        // Add command noun suffix only when the current noun doesn't contain it or vice-versa.
        if (!cmdNoun) {
          cmdNoun = pascalCase([cmdNounSuffix]);
        } else if (!cmdNounSuffix.toLowerCase().startsWith('by')) {
          if (
            !cmdNoun.toLowerCase().includes(cmdNounSuffix.toLowerCase()) &&
            !cmdNounSuffix.toLowerCase().includes(cmdNoun.toLowerCase())) {
            cmdNoun += pascalCase([cmdNounSuffix]);
          } else if (cmdNounSuffix.toLowerCase().includes(cmdNoun.toLowerCase())) {
            cmdNoun = cmdNounSuffix;
          }
        }
      }
    }
  }
  // Singularize command noun
  if (cmdNoun) {
    cmdNoun = getSingularizedValue(cmdNoun);
  }

  return cmdVerbs.map(v => {
    let verb = pascalCase([v]);
    if (!cmdNoun) { verb = getSingularizedValue(verb); }
    onMessage({ Channel: Channel.Debug, Text: `Operation '${operationId}': Using noun '${cmdNoun}' and verb '${verb}'.` });
    return { noun: cmdNoun, verb, variant };
  });
}