1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-21 21:40:48 +00:00

util/index.js: minor refactoring (#35510)

* rename variables
* remove an unused variable
* be more explicit
* reuse variable
This commit is contained in:
XhmikosR 2021-12-10 07:48:04 +02:00 committed by GitHub
parent eaa801c899
commit 871c8bdd3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,12 +10,12 @@ const MILLISECONDS_MULTIPLIER = 1000
const TRANSITION_END = 'transitionend' const TRANSITION_END = 'transitionend'
// Shoutout AngusCroll (https://goo.gl/pxwQGp) // Shoutout AngusCroll (https://goo.gl/pxwQGp)
const toType = obj => { const toType = object => {
if (obj === null || obj === undefined) { if (object === null || object === undefined) {
return `${obj}` return `${object}`
} }
return Object.prototype.toString.call(obj).match(/\s([a-z]+)/i)[1].toLowerCase() return Object.prototype.toString.call(object).match(/\s([a-z]+)/i)[1].toLowerCase()
} }
/** /**
@ -34,22 +34,22 @@ const getSelector = element => {
let selector = element.getAttribute('data-bs-target') let selector = element.getAttribute('data-bs-target')
if (!selector || selector === '#') { if (!selector || selector === '#') {
let hrefAttr = element.getAttribute('href') let hrefAttribute = element.getAttribute('href')
// The only valid content that could double as a selector are IDs or classes, // The only valid content that could double as a selector are IDs or classes,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector, // so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid. // `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273 // See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttr || (!hrefAttr.includes('#') && !hrefAttr.startsWith('.'))) { if (!hrefAttribute || (!hrefAttribute.includes('#') && !hrefAttribute.startsWith('.'))) {
return null return null
} }
// Just in case some CMS puts out a full URL with the anchor appended // Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) { if (hrefAttribute.includes('#') && !hrefAttribute.startsWith('#')) {
hrefAttr = `#${hrefAttr.split('#')[1]}` hrefAttribute = `#${hrefAttribute.split('#')[1]}`
} }
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null selector = hrefAttribute && hrefAttribute !== '#' ? hrefAttribute.trim() : null
} }
return selector return selector
@ -98,26 +98,26 @@ const triggerTransitionEnd = element => {
element.dispatchEvent(new Event(TRANSITION_END)) element.dispatchEvent(new Event(TRANSITION_END))
} }
const isElement = obj => { const isElement = object => {
if (!obj || typeof obj !== 'object') { if (!object || typeof object !== 'object') {
return false return false
} }
if (typeof obj.jquery !== 'undefined') { if (typeof object.jquery !== 'undefined') {
obj = obj[0] object = object[0]
} }
return typeof obj.nodeType !== 'undefined' return typeof object.nodeType !== 'undefined'
} }
const getElement = obj => { const getElement = object => {
// it's a jQuery object or a node element // it's a jQuery object or a node element
if (isElement(obj)) { if (isElement(object)) {
return obj.jquery ? obj[0] : obj return object.jquery ? object[0] : object
} }
if (typeof obj === 'string' && obj.length > 0) { if (typeof object === 'string' && object.length > 0) {
return document.querySelector(obj) return document.querySelector(object)
} }
return null return null
@ -199,10 +199,8 @@ const reflow = element => {
} }
const getjQuery = () => { const getjQuery = () => {
const { jQuery } = window if (window.jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
return window.jQuery
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
return jQuery
} }
return null return null
@ -291,15 +289,15 @@ const executeAfterTransition = (callback, transitionElement, waitForTransition =
* @return {Element|elem} The proper element * @return {Element|elem} The proper element
*/ */
const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed) => { const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed) => {
const listLength = list.length
let index = list.indexOf(activeElement) let index = list.indexOf(activeElement)
// if the element does not exist in the list return an element depending on the direction and if cycle is allowed // if the element does not exist in the list return an element
// depending on the direction and if cycle is allowed
if (index === -1) { if (index === -1) {
return list[!shouldGetNext && isCycleAllowed ? list.length - 1 : 0] return !shouldGetNext && isCycleAllowed ? list[listLength - 1] : list[0]
} }
const listLength = list.length
index += shouldGetNext ? 1 : -1 index += shouldGetNext ? 1 : -1
if (isCycleAllowed) { if (isCycleAllowed) {