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

utils.js « runner « ci « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1ca0a9e86b5cbbb7c1691f5fbc67db691db43197 (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
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
import { formatNumber } from '~/locale';
import { RUNNER_JOB_COUNT_LIMIT } from './constants';

/**
 * Formats a job count, limited to a max number
 *
 * @param {Number} jobCount
 * @returns Formatted string
 */
export const formatJobCount = (jobCount) => {
  if (typeof jobCount !== 'number') {
    return '';
  }
  if (jobCount > RUNNER_JOB_COUNT_LIMIT) {
    return `${formatNumber(RUNNER_JOB_COUNT_LIMIT)}+`;
  }
  return formatNumber(jobCount);
};

/**
 * Returns a GlTable fields with a given key and label
 *
 * @param {Object} options
 * @returns Field object to add to GlTable fields
 */
export const tableField = ({ key, label = '', thClasses = [], ...options }) => {
  return {
    key,
    label,
    thClass: thClasses,
    tdAttr: {
      'data-testid': `td-${key}`,
    },
    ...options,
  };
};

/**
 * Returns variables for a GraphQL query that uses keyset
 * pagination.
 *
 * https://docs.gitlab.com/ee/development/graphql_guide/pagination.html#keyset-pagination
 *
 * @param {Object} pagination - Contains before, after, page
 * @param {Number} pageSize
 * @returns Variables
 */
export const getPaginationVariables = (pagination, pageSize = 10) => {
  const { before, after } = pagination;

  // first + after: Next page
  // Get the first N items after item X
  if (after) {
    return {
      after,
      first: pageSize,
    };
  }

  // last + before: Prev page
  // Get the first N items before item X, when you click on Prev
  if (before) {
    return {
      before,
      last: pageSize,
    };
  }

  // first page
  // Get the first N items
  return { first: pageSize };
};

/**
 * Turns a server-provided interval integer represented as a string into an
 * integer that the frontend can use.
 *
 * @param {String} interval - String to convert
 * @returns Parsed integer
 */
export const parseInterval = (interval) => {
  return typeof interval === 'string' ? parseInt(interval, 10) : null;
};