All files / app/assets/javascripts/snippets/utils blob.js

96.42% Statements 27/28
100% Branches 10/10
91.66% Functions 11/12
96% Lines 24/25

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                        93x   70x             23x             5x 115x 23x             92x 6x               86x                 5x 65x 95x 3x             65x 115x 115x   65x     5x 18x 17x 33x 33x           1x     5x                        
import { uniqueId } from 'lodash';
import { SNIPPET_MARK_BLOBS_CONTENT, SNIPPET_MEASURE_BLOBS_CONTENT } from '~/performance/constants';
import { performanceMarkAndMeasure } from '~/performance/utils';
import { VISIBILITY_LEVELS_INTEGER_TO_STRING } from '~/visibility_level/constants';
import {
  SNIPPET_BLOB_ACTION_CREATE,
  SNIPPET_BLOB_ACTION_UPDATE,
  SNIPPET_BLOB_ACTION_MOVE,
  SNIPPET_BLOB_ACTION_DELETE,
  SNIPPET_VISIBILITY,
} from '../constants';
 
const createLocalId = () => uniqueId('blob_local_');
 
export const decorateBlob = (blob) => ({
  ...blob,
  id: createLocalId(),
  isLoaded: false,
  content: '',
});
 
export const createBlob = () => ({
  id: createLocalId(),
  content: '',
  path: '',
  isLoaded: true,
});
 
const diff = ({ content, path }, origBlob) => {
  if (!origBlob) {
    return {
      action: SNIPPET_BLOB_ACTION_CREATE,
      previousPath: path,
      content,
      filePath: path,
    };
  }
  if (origBlob.path !== path || origBlob.content !== content) {
    return {
      action: origBlob.path === path ? SNIPPET_BLOB_ACTION_UPDATE : SNIPPET_BLOB_ACTION_MOVE,
      previousPath: origBlob.path,
      content,
      filePath: path,
    };
  }
 
  return null;
};
 
/**
 * This function returns an array of diff actions (to be sent to the BE) based on the current vs. original blobs
 *
 * @param {Object} blobs
 * @param {Object} origBlobs
 */
export const diffAll = (blobs, origBlobs) => {
  const deletedEntries = Object.values(origBlobs)
    .filter((x) => !blobs[x.id])
    .map(({ path, content }) => ({
      action: SNIPPET_BLOB_ACTION_DELETE,
      previousPath: path,
      filePath: path,
      content,
    }));
 
  const newEntries = Object.values(blobs)
    .map((blob) => diff(blob, origBlobs[blob.id]))
    .filter((x) => x);
 
  return [...deletedEntries, ...newEntries];
};
 
export const defaultSnippetVisibilityLevels = (arr) => {
  if (Array.isArray(arr)) {
    return arr.map((l) => {
      const translatedLevel = VISIBILITY_LEVELS_INTEGER_TO_STRING[l];
      return {
        value: translatedLevel,
        ...SNIPPET_VISIBILITY[translatedLevel],
      };
    });
  }
  return [];
};
 
export const markBlobPerformance = () => {
  performanceMarkAndMeasure({
    mark: SNIPPET_MARK_BLOBS_CONTENT,
    measures: [
      {
        name: SNIPPET_MEASURE_BLOBS_CONTENT,
        start: undefined,
        end: SNIPPET_MARK_BLOBS_CONTENT,
      },
    ],
  });
};