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'
// Shoutout AngusCroll (https://goo.gl/pxwQGp)
const toType = obj => {
if (obj === null || obj === undefined) {
return `${obj}`
const toType = object => {
if (object === null || object === undefined) {
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')
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,
// so everything starting with `#` or `.`. If a "real" URL is used as the selector,
// `document.querySelector` will rightfully complain it is invalid.
// See https://github.com/twbs/bootstrap/issues/32273
if (!hrefAttr || (!hrefAttr.includes('#') && !hrefAttr.startsWith('.'))) {
if (!hrefAttribute || (!hrefAttribute.includes('#') && !hrefAttribute.startsWith('.'))) {
return null
}
// Just in case some CMS puts out a full URL with the anchor appended
if (hrefAttr.includes('#') && !hrefAttr.startsWith('#')) {
hrefAttr = `#${hrefAttr.split('#')[1]}`
if (hrefAttribute.includes('#') && !hrefAttribute.startsWith('#')) {
hrefAttribute = `#${hrefAttribute.split('#')[1]}`
}
selector = hrefAttr && hrefAttr !== '#' ? hrefAttr.trim() : null
selector = hrefAttribute && hrefAttribute !== '#' ? hrefAttribute.trim() : null
}
return selector
@ -98,26 +98,26 @@ const triggerTransitionEnd = element => {
element.dispatchEvent(new Event(TRANSITION_END))
}
const isElement = obj => {
if (!obj || typeof obj !== 'object') {
const isElement = object => {
if (!object || typeof object !== 'object') {
return false
}
if (typeof obj.jquery !== 'undefined') {
obj = obj[0]
if (typeof object.jquery !== 'undefined') {
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
if (isElement(obj)) {
return obj.jquery ? obj[0] : obj
if (isElement(object)) {
return object.jquery ? object[0] : object
}
if (typeof obj === 'string' && obj.length > 0) {
return document.querySelector(obj)
if (typeof object === 'string' && object.length > 0) {
return document.querySelector(object)
}
return null
@ -199,10 +199,8 @@ const reflow = element => {
}
const getjQuery = () => {
const { jQuery } = window
if (jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
return jQuery
if (window.jQuery && !document.body.hasAttribute('data-bs-no-jquery')) {
return window.jQuery
}
return null
@ -291,15 +289,15 @@ const executeAfterTransition = (callback, transitionElement, waitForTransition =
* @return {Element|elem} The proper element
*/
const getNextActiveElement = (list, activeElement, shouldGetNext, isCycleAllowed) => {
const listLength = list.length
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) {
return list[!shouldGetNext && isCycleAllowed ? list.length - 1 : 0]
return !shouldGetNext && isCycleAllowed ? list[listLength - 1] : list[0]
}
const listLength = list.length
index += shouldGetNext ? 1 : -1
if (isCycleAllowed) {