Welcome to mirror list, hosted at ThFree Co, Russian Federation.

suggestions.js « utils « diffs « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a272f7f32571f7849055cf6d7c57f967b86f7ec1 (plain)
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
function removeEmptyProperties(dict) {
  const noBlanks = Object.entries(dict).reduce((final, [key, value]) => {
    const upd = { ...final };

    // The number 0 shouldn't be falsey when we're printing variables
    if (value || value === 0) {
      upd[key] = value;
    }

    return upd;
  }, {});

  return noBlanks;
}

export function computeSuggestionCommitMessage({ message, values = {} } = {}) {
  const noEmpties = removeEmptyProperties(values);
  const matchPhrases = Object.keys(noEmpties)
    .map((key) => `%{${key}}`)
    .join('|');
  const replacementExpression = new RegExp(`(${matchPhrases})`, 'gm');

  return message.replace(replacementExpression, (match) => {
    const key = match.replace(/(^%{|}$)/gm, '');

    return noEmpties[key];
  });
}