parsePascalName()

in src/processors/CodeGenNameConventions.js [185:237]


  parsePascalName(name: string) {
    if (!name) {
      return [];
    }
    if (!name.split) {
      return [String(name)];
    }
    if (name.charAt(0) !== name.charAt(0).toUpperCase()) {
      throw Error('not valid pascal casing name: ' + name);
    }
    const STATUS = {
      SEEN_WORD_START: 0, // seen an upper-case char after lower-case chars
      EXPECT_PASCAL_WORD: 1, // second char is lower-case in a word
      EXPECT_ALL_UPPER_WORD: 2, // second char is also upper case in a word
    };
    const parts = [];
    let indexStart = 0;
    let parseStatus = STATUS.SEEN_WORD_START; // assert charAt(0) is upper case
    for (let i = 1; i < name.length; ++i) {
      const isUpper = _isCharUpper(name.charAt(i));
      switch (parseStatus) {
        case STATUS.SEEN_WORD_START:
          parseStatus = isUpper
            ? STATUS.EXPECT_ALL_UPPER_WORD
            : STATUS.EXPECT_PASCAL_WORD;
          break;
        case STATUS.EXPECT_PASCAL_WORD:
          if (isUpper) {
            // The word terminates when we see the next upper-case letter
            parts.push(name.substring(indexStart, i).toLowerCase());
            indexStart = i;
            parseStatus = STATUS.SEEN_WORD_START;
          }
          break;
        case STATUS.EXPECT_ALL_UPPER_WORD:
          if (!isUpper) {
            // The word terminates when we see a lower-case letter
            parts.push(name.substring(indexStart, i - 1));
            indexStart = i - 1;
            parseStatus = STATUS.EXPECT_PASCAL_WORD;
          }
          break;
      }
    }

    const lastPart = name.substring(indexStart, name.length);
    if (parseStatus === STATUS.EXPECT_ALL_UPPER_WORD) {
      parts.push(lastPart);
    } else {
      parts.push(lastPart.toLowerCase());
    }
    return parts;
  },