# All files / composer/utils StringUtils.js

33.33% Statements 13/39
0% Branches 0/13
10% Functions 1/10
40.63% Lines 13/32

    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
    7911x

    11x

    11x

    11x

    11x

    11x
    11x

    11x
    11x

    11x

    11x
    4x
    4x

     const getHumanReadableSize = (bytes) => {
      const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
      if (bytes === 0) return 'n/a';

      const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
      if (i === 0) return `${bytes} ${sizes[i]}`;

      return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
    };

    const getHumanReadableTime = (unformattedSeconds) => {
      let minutes = Math.floor(unformattedSeconds / 60);
      let seconds = unformattedSeconds - (minutes * 60);

      if (seconds < 10) seconds = `0${seconds}`;
      if (minutes < 10) minutes = `0${minutes}`;

      return `${minutes}:${seconds}`;
    };

    const getFileTypeFromPath = (path) => {
      const fileNameParts = path.split('.');
      return fileNameParts[fileNameParts.length - 1];
    };

    /**
     * Return a random id. If an array of existing ids is passed, it'll ensure
     * the returned id is unique.
     */
    const generateUniqueId = (existingIds = []) => {
      let id;

      do {
        id = Math.floor(Math.random() * Math.pow(10, 10)).toString(36);
      } while (existingIds.includes(id));

      return id;
    };

    const getAbsoluteUrl = (link) => {
      if (!link.includes('http://') && !link.includes('https://')) {
        link = `http://${link}`;
      }
      return link;
    };

    /**
     * Take advantage of the String iterator working with full unicode symbols rather than
     * surrogate pairs to make unicode-aware computations.
     *
     * getUnicodeAwareLength(str) returns the unicode-aware length of a string.
     * makeUnicodeAwareIndexUnaware(str, i) when given a unicode-aware index in a string,
     * returns its unicode-unaware counterpart.
     */
    const getUnicodeAwareLength = (str) => [...str].length;
    const makeUnicodeAwareIndexUnaware = (str, i) => Array.from(str).slice(0, i).join('').length;

    /**
     * Escape parens in a string. Useful for CSS style tags, where external resources
     * linked using `url()` have parens acting as special characters.
     */
    const cachedParensRegex = /(\(|\))/g;
    const escapeParens = (str) => str.replace(cachedParensRegex, '\\$1');

    const getDomainFromUrl = (url) => (
      url.replace(/(?:(?:https?|ftp):\/\/)?(?:www\.)?/, '').split(/[/?#]/)[0]
    );

    const removeLinkFromErrorMessageText = (text, linkClass) => {
      const rx = new RegExp(`<a class="${linkClass}".*?<\/a>`)
      return text.replace(rx, '');
    };

    export {
      getHumanReadableSize, getHumanReadableTime, getFileTypeFromPath, escapeParens,
      getAbsoluteUrl, generateUniqueId, getUnicodeAwareLength, makeUnicodeAwareIndexUnaware,
      getDomainFromUrl, removeLinkFromErrorMessageText,
    };