All files / app/assets/javascripts/notes/stores utils.js

52% Statements 13/25
0% Branches 0/8
66.66% Functions 6/9
42.1% Lines 8/19

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            39x   82x   39x                                         39x   39x   39x 6x   39x            
import { trimFirstCharOfLineContent } from '~/diffs/store/utils'; // eslint-disable-line import/no-deprecated
import createGqClient, { fetchPolicies } from '~/lib/graphql';
import AjaxCache from '~/lib/utils/ajax_cache';
import { sprintf, __ } from '~/locale';
 
// factory function because global flag makes RegExp stateful
const createQuickActionsRegex = () => /^\/\w+.*$/gm;
 
export const findNoteObjectById = (notes, id) => notes.filter((n) => n.id === id)[0];
 
export const getQuickActionText = (note) => {
  let text = __('Applying command');
  const quickActions = AjaxCache.get(gl.GfmAutoComplete.dataSources.commands) || [];
 
  const executedCommands = quickActions.filter((command) => {
    const commandRegex = new RegExp(`/${command.name}`);
    return commandRegex.test(note);
  });
 
  if (executedCommands && executedCommands.length) {
    if (executedCommands.length > 1) {
      text = __('Applying multiple commands');
    } else {
      const commandDescription = executedCommands[0].description.toLowerCase();
      text = sprintf(__('Applying command to %{commandDescription}'), { commandDescription });
    }
  }
 
  return text;
};
 
export const hasQuickActions = (note) => createQuickActionsRegex().test(note);
 
export const stripQuickActions = (note) => note.replace(createQuickActionsRegex(), '').trim();
 
export const prepareDiffLines = (diffLines) =>
  diffLines.map((line) => ({ ...trimFirstCharOfLineContent(line) })); // eslint-disable-line import/no-deprecated
 
export const gqClient = createGqClient(
  {},
  {
    fetchPolicy: fetchPolicies.NO_CACHE,
  },
);