Utility.ts 2.84 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

/**
 * Module: TYPO3/CMS/Backend/Utility
 */
class Utility {
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  /**
   * Splits a string by a given delimiter and trims the values
   *
   * @param {string} delimiter
   * @param {string} string
   * @return Array<string>
   */
  public static trimExplode(delimiter: string, string: string): Array<string> {
    return string.split(delimiter).map((item) => item.trim()).filter((item) => item !== '');
  }

  /**
   * Splits a string by a given delimiter and converts the values to integer
   *
   * @param {string} delimiter
   * @param {string} string
   * @param {boolean} excludeZeroValues
   * @return Array<number>
   */
  public static intExplode(delimiter: string, string: string, excludeZeroValues: boolean = false): Array<number> {
    return string.split(delimiter).map((item) => parseInt(item, 10)).filter((item) => !isNaN(item) || excludeZeroValues && item === 0);
  }

41
42
43
44
45
46
47
48
49
  /**
   * Checks if a given number is really a number
   *
   * Taken from:
   * http://dl.dropbox.com/u/35146/js/tests/isNumber.html
   *
   * @param {number} value
   * @returns {boolean}
   */
50
  public static isNumber(value: number): boolean {
51
52
53
54
55
56
57
58
59
60
    return !isNaN(parseFloat(value.toString())) && isFinite(value);
  }

  /**
   * Gets a parameter from a given url
   *
   * @param {string} url
   * @param {string} parameter
   * @returns {string}
   */
61
  public static getParameterFromUrl(url: string, parameter: string): string {
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
    if (typeof url.split !== 'function') {
      return '';
    }
    const parts = url.split('?');
    let value = '';

    if (parts.length >= 2) {
      const queryString = parts.join('?');

      const prefix = encodeURIComponent(parameter) + '=';
      const parameters = queryString.split(/[&;]/g);
      for (let i = parameters.length; i-- > 0; ) {
        if (parameters[i].lastIndexOf(prefix, 0) !== -1) {
          value = parameters[i].split('=')[1];
          break;
        }
      }
    }

    return value;
  }

  /**
   * Updates a parameter inside of given url
   *
   * @param {string} url
   * @param {string} key
   * @param {string} value
   * @returns {string}
   */
92
  public static updateQueryStringParameter(url: string, key: string, value: string): string {
93
94
95
96
97
98
99
100
101
102
    const re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
    const separator = url.indexOf('?') !== -1 ? '&' : '?';

    if (url.match(re)) {
      return url.replace(re, '$1' + key + '=' + value + '$2');
    }
    return url + separator + key + '=' + value;
  }
}

103
export = Utility;