All files utils.ts

95.08% Statements 58/61
73.43% Branches 47/64
100% Functions 11/11
96.42% Lines 54/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136  1x 9x 9x 6x 6x   9x     1x                                           1x 8x 8x     8x   8x 14x 14x 9x 9x 9x 9x         8x                         10x 1x   10x 1x   9x 9x 2x   7x       273x 1x   272x 272x                       4x 4x 4x 16x 256x 256x 16x 16x     16x       16x 8x   16x     4x       4x 1x 5x 8x 5x 5x 2x   1x   3x   4x            
 
export const getVersion = (ver: string = '') => {
  let currentVersion = ''
  ver.replace(/([v|V]\d(\.\d+){0,2})/i, (str) => {
    currentVersion = str
    return str
  })
  return currentVersion
}
 
export const defaultTypes = {
  type: '๐Ÿ†Ž',
  feat: '๐ŸŒŸ',
  style: '๐ŸŽจ',
  chore: '๐Ÿ’„',
  doc: '๐Ÿ“–',
  docs: '๐Ÿ“–',
  build: '๐Ÿงฏ',
  fix: '๐Ÿž',
  test: 'โ›‘',
  refactor: '๐Ÿ',
  website: '๐ŸŒ',
  revert: '๐Ÿ”™',
  clean: '๐Ÿ’Š',
  perf: '๐Ÿ“ˆ',
  ci: '๐Ÿ’ข',
  __unknown__: '๐Ÿ“„'
}
 
/**
 * Parse custom emojis
 */
export const parseCustomEmojis = (customEmoji: string, defaultTypes: Record<string, string>) => {
  const customEmojiData = customEmoji.split(',');
  const parsedEmojis: Record<string, string> = { ...defaultTypes };
 
  // A more inclusive emoji regex that matches compound characters (including variation selectors and zero-width joiners)
  const emojiRegex = /([\p{Emoji_Presentation}\p{Emoji}\uFE0F\u200D]+)/gu;
 
  customEmojiData.forEach((item) => {
    const emojiIconMatch = item.match(emojiRegex);
    if (emojiIconMatch && emojiIconMatch[0]) {
      const emoji = emojiIconMatch[0];
      const typeName = item.replace(emoji, '').trim();
      Eif (typeName) {
        parsedEmojis[typeName] = emoji;
      }
    }
  });
 
  return { ...parsedEmojis };
};
 
export type FormatStringCommit = {
  regExp?: string;
  shortHash?: string;
  originalMarkdown?: boolean;
  filterAuthor?: string;
  hash?: string;
  login?: string;
}
 
export function formatStringCommit(commit = '', repoName = '', { regExp, shortHash, originalMarkdown, filterAuthor, hash, login = '' }: FormatStringCommit) {
  if (filterAuthor && (new RegExp(filterAuthor)).test(login)) {
    login = '';
  }
  if (regExp && (new RegExp(regExp).test(commit))) {
    return '';
  }
  login = login.replace(/\[bot\]/, '-bot');
  if (originalMarkdown) {
    return `${commit} ${shortHash} ${login ? `@${login}`: ''}`;
  }
  return `${commit} [\`${shortHash}\`](http://github.com/${repoName}/commit/${hash})${login ? ` @${login}`: ''}`;
}
 
export function getRegExp(type = '', commit = '') {
  if (!type) {
    return false; // ๅฆ‚ๆžœๆฒกๆœ‰ไผ ้€’ type๏ผŒๅˆ™็›ดๆŽฅ่ฟ”ๅ›ž false๏ผŒๆˆ–่€…ไฝ ๅฏไปฅๆ นๆฎ้œ€ๆฑ‚ๆŠ›ๅ‡บ้”™่ฏฏ
  }
  const typeLowerCase = type.trim().toLocaleLowerCase();
  return (new RegExp(`^(${typeLowerCase}\\s+[\\s(|:])|(${typeLowerCase}[(|:])`)).test(commit.trim().toLocaleLowerCase());
}
 
type Options = {
  types: typeof defaultTypes;
  category?: Partial<Record<keyof typeof defaultTypes, string[]>>;
  showEmoji: boolean;
  removeType: boolean;
  template: string;
}
 
export function getCommitLog(log: string[], options = {} as Options) {
  const { types, category = {}, showEmoji, template, removeType = false } = options;
  Eif (!Array.isArray(category['__unknown__'])) category['__unknown__'] = [];
  log = log.map((commit) => {
    (Object.keys(types || {}) as Array<keyof typeof defaultTypes>).forEach((name) => {
      if (!category[name]) category[name] = [];
      if (getRegExp(name, commit)) {
        commit = showEmoji ? `- ${types[name]} ${commit}` : `- ${commit}`;
        category[name]!.push(commit);
      }
    });
    Iif (!/^-\s/.test(commit) && commit) {
      commit = showEmoji ? `- ${types['__unknown__']} ${commit}` : `- ${commit}`;
      category['__unknown__']!.push(commit);
    }
    if (removeType) {
      commit = commit.replace(/(^-\s+?\w+(\s+)?\((.*?)\):\s+)|(^-\s+?\w+(\s+)?:\s+)/, '- ');
    }
    return commit
  }).filter(Boolean);
 
  let changelogContent = '';
  /**
   * https://github.com/jaywcjlove/changelog-generator/issues/111#issuecomment-1594085749
   */
  if (template && typeof template === 'string') {
    changelogContent = template.replace(/\{\{(.*?)\}\}/g, (string, replaceValue) => {
      const [typeString = '', emptyString] = (replaceValue || '').split('||');
      const arr = typeString.replace(/\s/g, '').split(',').map((name: keyof typeof types) => category[name] || []).flat().filter(Boolean);
      Iif (arr.length === 0 && emptyString) return emptyString;
      if (arr.length > 0) return arr.join('\n');
      return string;
    });
    changelogContent = changelogContent.replace(/##(.*?)\n+\{\{(.*?)\}\}(\s+)?(\n+)?/g, '');
  } else {
    changelogContent = log.join('\n');
  }
  return {
    changelog: log,
    category,
    changelogContent,
  }
}