Passed
Pull Request — master (#57)
by
unknown
11:34 queued 08:49
created

resources/bootstrap/js/util.js   B

Complexity

Total Complexity 45
Complexity/F 2.25

Size

Lines of Code 184
Function Count 20

Duplication

Duplicated Lines 5
Ratio 2.72 %

Importance

Changes 0
Metric Value
eloc 104
dl 5
loc 184
rs 8.8
c 0
b 0
f 0
wmc 45
mnd 25
bc 25
fnc 20
bpm 1.25
cpm 2.25
noi 6

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complexity

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like resources/bootstrap/js/util.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/*!
2
  * Bootstrap util.js v4.6.1 (https://getbootstrap.com/)
3
  * Copyright 2011-2021 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery')) :
8
  typeof define === 'function' && define.amd ? define(['jquery'], factory) :
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Util = factory(global.jQuery));
10
})(this, (function ($) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
16
  /**
17
   * --------------------------------------------------------------------------
18
   * Bootstrap (v4.6.1): util.js
19
   * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
20
   * --------------------------------------------------------------------------
21
   */
22
  /**
23
   * Private TransitionEnd Helpers
24
   */
25
26
  var TRANSITION_END = 'transitionend';
27
  var MAX_UID = 1000000;
28
  var MILLISECONDS_MULTIPLIER = 1000; // Shoutout AngusCroll (https://goo.gl/pxwQGp)
29
30
  function toType(obj) {
31
    if (obj === null || typeof obj === 'undefined') {
32
      return "" + obj;
33
    }
34
35
    return {}.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase();
36
  }
37
38
  function getSpecialTransitionEndEvent() {
39
    return {
40
      bindType: TRANSITION_END,
41
      delegateType: TRANSITION_END,
42
      handle: function handle(event) {
43
        if ($__default["default"](event.target).is(this)) {
44
          return event.handleObj.handler.apply(this, arguments); // eslint-disable-line prefer-rest-params
45
        }
46
47
        return undefined;
48
      }
49
    };
50
  }
51
52
  function transitionEndEmulator(duration) {
53
    var _this = this;
54
55
    var called = false;
56
    $__default["default"](this).one(Util.TRANSITION_END, function () {
57
      called = true;
58
    });
59
    setTimeout(function () {
60
      if (!called) {
61
        Util.triggerTransitionEnd(_this);
62
      }
63
    }, duration);
64
    return this;
65
  }
66
67
  function setTransitionEndSupport() {
68
    $__default["default"].fn.emulateTransitionEnd = transitionEndEmulator;
69
    $__default["default"].event.special[Util.TRANSITION_END] = getSpecialTransitionEndEvent();
70
  }
71
  /**
72
   * Public Util API
73
   */
74
75
76
  var Util = {
77
    TRANSITION_END: 'bsTransitionEnd',
78
    getUID: function getUID(prefix) {
79
      do {
80
        // eslint-disable-next-line no-bitwise
81
        prefix += ~~(Math.random() * MAX_UID); // "~~" acts like a faster Math.floor() here
82
      } while (document.getElementById(prefix));
83
84
      return prefix;
85
    },
86
    getSelectorFromElement: function getSelectorFromElement(element) {
87
      var selector = element.getAttribute('data-target');
88
89
      if (!selector || selector === '#') {
90
        var hrefAttr = element.getAttribute('href');
91
        selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : '';
92
      }
93
94
      try {
95
        return document.querySelector(selector) ? selector : null;
96
      } catch (_) {
97
        return null;
98
      }
99
    },
100
    getTransitionDurationFromElement: function getTransitionDurationFromElement(element) {
101
      if (!element) {
102
        return 0;
103
      } // Get transition-duration of the element
104
105
106
      var transitionDuration = $__default["default"](element).css('transition-duration');
107
      var transitionDelay = $__default["default"](element).css('transition-delay');
108
      var floatTransitionDuration = parseFloat(transitionDuration);
109
      var floatTransitionDelay = parseFloat(transitionDelay); // Return 0 if element or transition duration is not found
110
111
      if (!floatTransitionDuration && !floatTransitionDelay) {
112
        return 0;
113
      } // If multiple durations are defined, take the first
114
115
116
      transitionDuration = transitionDuration.split(',')[0];
117
      transitionDelay = transitionDelay.split(',')[0];
118
      return (parseFloat(transitionDuration) + parseFloat(transitionDelay)) * MILLISECONDS_MULTIPLIER;
119
    },
120
    reflow: function reflow(element) {
121
      return element.offsetHeight;
122
    },
123
    triggerTransitionEnd: function triggerTransitionEnd(element) {
124
      $__default["default"](element).trigger(TRANSITION_END);
125
    },
126
    supportsTransitionEnd: function supportsTransitionEnd() {
127
      return Boolean(TRANSITION_END);
128
    },
129
    isElement: function isElement(obj) {
130
      return (obj[0] || obj).nodeType;
131
    },
132
    typeCheckConfig: function typeCheckConfig(componentName, config, configTypes) {
133
      for (var property in configTypes) {
134
        if (Object.prototype.hasOwnProperty.call(configTypes, property)) {
135
          var expectedTypes = configTypes[property];
136
          var value = config[property];
137
          var valueType = value && Util.isElement(value) ? 'element' : toType(value);
138
139
          if (!new RegExp(expectedTypes).test(valueType)) {
140
            throw new Error(componentName.toUpperCase() + ": " + ("Option \"" + property + "\" provided type \"" + valueType + "\" ") + ("but expected type \"" + expectedTypes + "\"."));
141
          }
142
        }
143
      }
144
    },
145
    findShadowRoot: function findShadowRoot(element) {
146
      if (!document.documentElement.attachShadow) {
147
        return null;
148
      } // Can find the shadow root otherwise it'll return the document
149
150
151
      if (typeof element.getRootNode === 'function') {
152
        var root = element.getRootNode();
153
        return root instanceof ShadowRoot ? root : null;
154
      }
155
156
      if (element instanceof ShadowRoot) {
157
        return element;
158
      } // when we don't find a shadow root
159
160
161
      if (!element.parentNode) {
162
        return null;
163
      }
164
165
      return Util.findShadowRoot(element.parentNode);
166
    },
167
    jQueryDetection: function jQueryDetection() {
168
      if (typeof $__default["default"] === 'undefined') {
169
        throw new TypeError('Bootstrap\'s JavaScript requires jQuery. jQuery must be included before Bootstrap\'s JavaScript.');
170
      }
171
172
      var version = $__default["default"].fn.jquery.split(' ')[0].split('.');
173
      var minMajor = 1;
174
      var ltMajor = 2;
175
      var minMinor = 9;
176
      var minPatch = 1;
177
      var maxMajor = 4;
178
179
      if (version[0] < ltMajor && version[1] < minMinor || version[0] === minMajor && version[1] === minMinor && version[2] < minPatch || version[0] >= maxMajor) {
180
        throw new Error('Bootstrap\'s JavaScript requires at least jQuery v1.9.1 but less than v4.0.0');
181
      }
182
    }
183
  };
184
  Util.jQueryDetection();
185
  setTransitionEndSupport();
186
187
  return Util;
188
189
}));
190
//# sourceMappingURL=util.js.map
191