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