diff options
author | XhmikosR <xhmikosr@gmail.com> | 2021-07-30 09:28:51 +0300 |
---|---|---|
committer | XhmikosR <xhmikosr@gmail.com> | 2021-10-05 19:52:11 +0300 |
commit | 666fe596bf4629777f995dd79046b1db632ffdfb (patch) | |
tree | 9e14fcf9c5d325e668d75d2b313242e16e7fbec3 /js/src | |
parent | 2b4d0d166b58cabfb0384a2081d84e51df84e37f (diff) |
Enable `unicorn/no-array-for-each` rule
Diffstat (limited to 'js/src')
-rw-r--r-- | js/src/base-component.js | 4 | ||||
-rw-r--r-- | js/src/carousel.js | 4 | ||||
-rw-r--r-- | js/src/collapse.js | 30 | ||||
-rw-r--r-- | js/src/dom/event-handler.js | 16 | ||||
-rw-r--r-- | js/src/dom/manipulator.js | 13 | ||||
-rw-r--r-- | js/src/dropdown.js | 13 | ||||
-rw-r--r-- | js/src/modal.js | 5 | ||||
-rw-r--r-- | js/src/offcanvas.js | 8 | ||||
-rw-r--r-- | js/src/scrollspy.js | 51 | ||||
-rw-r--r-- | js/src/tab.js | 5 | ||||
-rw-r--r-- | js/src/tooltip.js | 22 | ||||
-rw-r--r-- | js/src/util/index.js | 8 | ||||
-rw-r--r-- | js/src/util/sanitizer.js | 4 | ||||
-rw-r--r-- | js/src/util/scrollbar.js | 4 |
14 files changed, 101 insertions, 86 deletions
diff --git a/js/src/base-component.js b/js/src/base-component.js index cc6abd121d..0a1c173578 100644 --- a/js/src/base-component.js +++ b/js/src/base-component.js @@ -36,9 +36,9 @@ class BaseComponent { Data.remove(this._element, this.constructor.DATA_KEY) EventHandler.off(this._element, this.constructor.EVENT_KEY) - Object.getOwnPropertyNames(this).forEach(propertyName => { + for (const propertyName of Object.getOwnPropertyNames(this)) { this[propertyName] = null - }) + } } _queueCallback(callback, element, isAnimated = true) { diff --git a/js/src/carousel.js b/js/src/carousel.js index 322ad46a09..3f49ded408 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -304,9 +304,9 @@ class Carousel extends BaseComponent { } } - SelectorEngine.find(SELECTOR_ITEM_IMG, this._element).forEach(itemImg => { + for (const itemImg of SelectorEngine.find(SELECTOR_ITEM_IMG, this._element)) { EventHandler.on(itemImg, EVENT_DRAG_START, event => event.preventDefault()) - }) + } if (this._pointerEvent) { EventHandler.on(this._element, EVENT_POINTERDOWN, event => start(event)) diff --git a/js/src/collapse.js b/js/src/collapse.js index dd42bc7e78..b7f200d566 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -127,7 +127,8 @@ class Collapse extends BaseComponent { if (this._config.parent) { const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent) - actives = SelectorEngine.find(SELECTOR_ACTIVES, this._config.parent).filter(elem => !children.includes(elem)) // remove children if greater depth + // remove children if greater depth + actives = SelectorEngine.find(SELECTOR_ACTIVES, this._config.parent).filter(elem => !children.includes(elem)) } const container = SelectorEngine.findOne(this._selector) @@ -145,7 +146,7 @@ class Collapse extends BaseComponent { return } - actives.forEach(elemActive => { + for (const elemActive of actives) { if (container !== elemActive) { Collapse.getOrCreateInstance(elemActive, { toggle: false }).hide() } @@ -153,7 +154,7 @@ class Collapse extends BaseComponent { if (!activesData) { Data.set(elemActive, DATA_KEY, null) } - }) + } const dimension = this._getDimension() @@ -252,14 +253,15 @@ class Collapse extends BaseComponent { } const children = SelectorEngine.find(CLASS_NAME_DEEPER_CHILDREN, this._config.parent) - SelectorEngine.find(SELECTOR_DATA_TOGGLE, this._config.parent).filter(elem => !children.includes(elem)) - .forEach(element => { - const selected = getElementFromSelector(element) + const elements = SelectorEngine.find(SELECTOR_DATA_TOGGLE, this._config.parent).filter(elem => !children.includes(elem)) - if (selected) { - this._addAriaAndCollapsedClass([element], this._isShown(selected)) - } - }) + for (const element of elements) { + const selected = getElementFromSelector(element) + + if (selected) { + this._addAriaAndCollapsedClass([element], this._isShown(selected)) + } + } } _addAriaAndCollapsedClass(triggerArray, isOpen) { @@ -267,7 +269,7 @@ class Collapse extends BaseComponent { return } - triggerArray.forEach(elem => { + for (const elem of triggerArray) { if (isOpen) { elem.classList.remove(CLASS_NAME_COLLAPSED) } else { @@ -275,7 +277,7 @@ class Collapse extends BaseComponent { } elem.setAttribute('aria-expanded', isOpen) - }) + } } // Static @@ -315,9 +317,9 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function ( const selector = getSelectorFromElement(this) const selectorElements = SelectorEngine.find(selector) - selectorElements.forEach(element => { + for (const element of selectorElements) { Collapse.getOrCreateInstance(element, { toggle: false }).toggle() - }) + } }) /** diff --git a/js/src/dom/event-handler.js b/js/src/dom/event-handler.js index 2aa687bb13..e2fdbd52cf 100644 --- a/js/src/dom/event-handler.js +++ b/js/src/dom/event-handler.js @@ -221,13 +221,13 @@ function removeHandler(element, events, typeEvent, handler, delegationSelector) function removeNamespacedHandlers(element, events, typeEvent, namespace) { const storeElementEvent = events[typeEvent] || {} - Object.keys(storeElementEvent).forEach(handlerKey => { + for (const handlerKey of Object.keys(storeElementEvent)) { if (handlerKey.includes(namespace)) { const event = storeElementEvent[handlerKey] removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector) } - }) + } } function getTypeEvent(event) { @@ -266,13 +266,13 @@ const EventHandler = { } if (isNamespace) { - Object.keys(events).forEach(elementEvent => { + for (const elementEvent of Object.keys(events)) { removeNamespacedHandlers(element, events, elementEvent, originalTypeEvent.slice(1)) - }) + } } const storeElementEvent = events[typeEvent] || {} - Object.keys(storeElementEvent).forEach(keyHandlers => { + for (const keyHandlers of Object.keys(storeElementEvent)) { const handlerKey = keyHandlers.replace(stripUidRegex, '') if (!inNamespace || originalTypeEvent.includes(handlerKey)) { @@ -280,7 +280,7 @@ const EventHandler = { removeHandler(element, events, typeEvent, event.originalHandler, event.delegationSelector) } - }) + } }, trigger(element, event, args) { @@ -320,13 +320,13 @@ const EventHandler = { // merge custom information in our event if (typeof args !== 'undefined') { - Object.keys(args).forEach(key => { + for (const key of Object.keys(args)) { Object.defineProperty(evt, key, { get() { return args[key] } }) - }) + } } if (defaultPrevented) { diff --git a/js/src/dom/manipulator.js b/js/src/dom/manipulator.js index 11c4e9d39e..219727d7ce 100644 --- a/js/src/dom/manipulator.js +++ b/js/src/dom/manipulator.js @@ -44,14 +44,13 @@ const Manipulator = { } const attributes = {} + const bsKeys = Object.keys(element.dataset).filter(key => key.startsWith('bs')) - Object.keys(element.dataset) - .filter(key => key.startsWith('bs')) - .forEach(key => { - let pureKey = key.replace(/^bs/, '') - pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length) - attributes[pureKey] = normalizeData(element.dataset[key]) - }) + for (const key of bsKeys) { + let pureKey = key.replace(/^bs/, '') + pureKey = pureKey.charAt(0).toLowerCase() + pureKey.slice(1, pureKey.length) + attributes[pureKey] = normalizeData(element.dataset[key]) + } return attributes }, diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 335abaf05e..59809cf5d2 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -151,10 +151,10 @@ class Dropdown extends BaseComponent { // empty mouseover listeners to the body's immediate children; // only needed because of broken event delegation on iOS // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html - if ('ontouchstart' in document.documentElement && - !parent.closest(SELECTOR_NAVBAR_NAV)) { - [].concat(...document.body.children) - .forEach(elem => EventHandler.on(elem, 'mouseover', noop)) + if ('ontouchstart' in document.documentElement && !parent.closest(SELECTOR_NAVBAR_NAV)) { + for (const elem of [].concat(...document.body.children)) { + EventHandler.on(elem, 'mouseover', noop) + } } this._element.focus() @@ -203,8 +203,9 @@ class Dropdown extends BaseComponent { // If this is a touch-enabled device we remove the extra // empty mouseover listeners we added for iOS support if ('ontouchstart' in document.documentElement) { - [].concat(...document.body.children) - .forEach(elem => EventHandler.off(elem, 'mouseover', noop)) + for (const elem of [].concat(...document.body.children)) { + EventHandler.off(elem, 'mouseover', noop) + } } if (this._popper) { diff --git a/js/src/modal.js b/js/src/modal.js index ec67f1fa1b..b0ee089dee 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -175,8 +175,9 @@ class Modal extends BaseComponent { } dispose() { - [window, this._dialog] - .forEach(htmlElement => EventHandler.off(htmlElement, EVENT_KEY)) + for (const htmlElement of [window, this._dialog]) { + EventHandler.off(htmlElement, EVENT_KEY) + } this._backdrop.dispose() this._focustrap.deactivate() diff --git a/js/src/offcanvas.js b/js/src/offcanvas.js index 4fb2b2d9b2..28fd49f062 100644 --- a/js/src/offcanvas.js +++ b/js/src/offcanvas.js @@ -256,9 +256,11 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function ( data.toggle(this) }) -EventHandler.on(window, EVENT_LOAD_DATA_API, () => - SelectorEngine.find(OPEN_SELECTOR).forEach(el => Offcanvas.getOrCreateInstance(el).show()) -) +EventHandler.on(window, EVENT_LOAD_DATA_API, () => { + for (const el of SelectorEngine.find(OPEN_SELECTOR)) { + Offcanvas.getOrCreateInstance(el).show() + } +}) enableDismissTrigger(Offcanvas) /** diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 900d245c64..df9a14e220 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -111,7 +111,7 @@ class ScrollSpy extends BaseComponent { const targets = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target) - targets.map(element => { + for (const item of targets.map(element => { const targetSelector = getSelectorFromElement(element) const target = targetSelector ? SelectorEngine.findOne(targetSelector) : null @@ -128,11 +128,10 @@ class ScrollSpy extends BaseComponent { return null }) .filter(item => item) - .sort((a, b) => a[0] - b[0]) - .forEach(item => { - this._offsets.push(item[0]) - this._targets.push(item[1]) - }) + .sort((a, b) => a[0] - b[0])) { + this._offsets.push(item[0]) + this._targets.push(item[1]) + } } dispose() { @@ -226,20 +225,20 @@ class ScrollSpy extends BaseComponent { SelectorEngine.findOne(SELECTOR_DROPDOWN_TOGGLE, link.closest(SELECTOR_DROPDOWN)) .classList.add(CLASS_NAME_ACTIVE) } else { - SelectorEngine.parents(link, SELECTOR_NAV_LIST_GROUP) - .forEach(listGroup => { - // Set triggered links parents as active - // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor - SelectorEngine.prev(listGroup, `${SELECTOR_NAV_LINKS}, ${SELECTOR_LIST_ITEMS}`) - .forEach(item => item.classList.add(CLASS_NAME_ACTIVE)) - - // Handle special case when .nav-link is inside .nav-item - SelectorEngine.prev(listGroup, SELECTOR_NAV_ITEMS) - .forEach(navItem => { - SelectorEngine.children(navItem, SELECTOR_NAV_LINKS) - .forEach(item => item.classList.add(CLASS_NAME_ACTIVE)) - }) - }) + for (const listGroup of SelectorEngine.parents(link, SELECTOR_NAV_LIST_GROUP)) { + // Set triggered links parents as active + // With both <ul> and <nav> markup a parent is the previous sibling of any nav ancestor + for (const item of SelectorEngine.prev(listGroup, `${SELECTOR_NAV_LINKS}, ${SELECTOR_LIST_ITEMS}`)) { + item.classList.add(CLASS_NAME_ACTIVE) + } + + // Handle special case when .nav-link is inside .nav-item + for (const navItem of SelectorEngine.prev(listGroup, SELECTOR_NAV_ITEMS)) { + for (const item of SelectorEngine.children(navItem, SELECTOR_NAV_LINKS)) { + item.classList.add(CLASS_NAME_ACTIVE) + } + } + } } EventHandler.trigger(this._scrollElement, EVENT_ACTIVATE, { @@ -248,9 +247,12 @@ class ScrollSpy extends BaseComponent { } _clear() { - SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target) + const activeNodes = SelectorEngine.find(SELECTOR_LINK_ITEMS, this._config.target) .filter(node => node.classList.contains(CLASS_NAME_ACTIVE)) - .forEach(node => node.classList.remove(CLASS_NAME_ACTIVE)) + + for (const node of activeNodes) { + node.classList.remove(CLASS_NAME_ACTIVE) + } } // Static @@ -279,8 +281,9 @@ class ScrollSpy extends BaseComponent { */ EventHandler.on(window, EVENT_LOAD_DATA_API, () => { - SelectorEngine.find(SELECTOR_DATA_SPY) - .forEach(spy => new ScrollSpy(spy)) + for (const spy of SelectorEngine.find(SELECTOR_DATA_SPY)) { + new ScrollSpy(spy) // eslint-disable-line no-new + } }) /** diff --git a/js/src/tab.js b/js/src/tab.js index 581162c504..dd62df5055 100644 --- a/js/src/tab.js +++ b/js/src/tab.js @@ -164,8 +164,9 @@ class Tab extends BaseComponent { const dropdownElement = element.closest(SELECTOR_DROPDOWN) if (dropdownElement) { - SelectorEngine.find(SELECTOR_DROPDOWN_TOGGLE, dropdownElement) - .forEach(dropdown => dropdown.classList.add(CLASS_NAME_ACTIVE)) + for (const dropdown of SelectorEngine.find(SELECTOR_DROPDOWN_TOGGLE, dropdownElement)) { + dropdown.classList.add(CLASS_NAME_ACTIVE) + } } element.setAttribute('aria-expanded', true) diff --git a/js/src/tooltip.js b/js/src/tooltip.js index a26b8ada62..afd17da53c 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -285,9 +285,9 @@ class Tooltip extends BaseComponent { // only needed because of broken event delegation on iOS // https://www.quirksmode.org/blog/archives/2014/02/mouse_event_bub.html if ('ontouchstart' in document.documentElement) { - [].concat(...document.body.children).forEach(element => { + for (const element of [].concat(...document.body.children)) { EventHandler.on(element, 'mouseover', noop) - }) + } } const complete = () => { @@ -337,8 +337,9 @@ class Tooltip extends BaseComponent { // If this is a touch-enabled device we remove the extra // empty mouseover listeners we added for iOS support if ('ontouchstart' in document.documentElement) { - [].concat(...document.body.children) - .forEach(element => EventHandler.off(element, 'mouseover', noop)) + for (const element of [].concat(...document.body.children)) { + EventHandler.off(element, 'mouseover', noop) + } } this._activeTrigger[TRIGGER_CLICK] = false @@ -527,7 +528,7 @@ class Tooltip extends BaseComponent { _setListeners() { const triggers = this._config.trigger.split(' ') - triggers.forEach(trigger => { + for (const trigger of triggers) { if (trigger === 'click') { EventHandler.on(this._element, this.constructor.Event.CLICK, this._config.selector, event => this.toggle(event)) } else if (trigger !== TRIGGER_MANUAL) { @@ -541,7 +542,7 @@ class Tooltip extends BaseComponent { EventHandler.on(this._element, eventIn, this._config.selector, event => this._enter(event)) EventHandler.on(this._element, eventOut, this._config.selector, event => this._leave(event)) } - }) + } this._hideModalHandler = () => { if (this._element) { @@ -648,11 +649,11 @@ class Tooltip extends BaseComponent { _getConfig(config) { const dataAttributes = Manipulator.getDataAttributes(this._element) - Object.keys(dataAttributes).forEach(dataAttr => { + for (const dataAttr of Object.keys(dataAttributes)) { if (DISALLOWED_ATTRIBUTES.has(dataAttr)) { delete dataAttributes[dataAttr] } - }) + } config = { ...this.constructor.Default, @@ -706,8 +707,9 @@ class Tooltip extends BaseComponent { const basicClassPrefixRegex = new RegExp(`(^|\\s)${this._getBasicClassPrefix()}\\S+`, 'g') const tabClass = tip.getAttribute('class').match(basicClassPrefixRegex) if (tabClass !== null && tabClass.length > 0) { - tabClass.map(token => token.trim()) - .forEach(tClass => tip.classList.remove(tClass)) + for (const tClass of tabClass.map(token => token.trim())) { + tip.classList.remove(tClass) + } } } diff --git a/js/src/util/index.js b/js/src/util/index.js index b994963018..9cee979dfc 100644 --- a/js/src/util/index.js +++ b/js/src/util/index.js @@ -125,7 +125,7 @@ const getElement = obj => { } const typeCheckConfig = (componentName, config, configTypes) => { - Object.keys(configTypes).forEach(property => { + for (const property of Object.keys(configTypes)) { const expectedTypes = configTypes[property] const value = config[property] const valueType = value && isElement(value) ? 'element' : toType(value) @@ -135,7 +135,7 @@ const typeCheckConfig = (componentName, config, configTypes) => { `${componentName.toUpperCase()}: Option "${property}" provided type "${valueType}" but expected type "${expectedTypes}".` ) } - }) + } } const isVisible = element => { @@ -217,7 +217,9 @@ const onDOMContentLoaded = callback => { // add listener on the first call when the document is in loading state if (!DOMContentLoadedCallbacks.length) { document.addEventListener('DOMContentLoaded', () => { - DOMContentLoadedCallbacks.forEach(callback => callback()) + for (const callback of DOMContentLoadedCallbacks) { + callback() + } }) } diff --git a/js/src/util/sanitizer.js b/js/src/util/sanitizer.js index c02a4eb906..e27961e9a3 100644 --- a/js/src/util/sanitizer.js +++ b/js/src/util/sanitizer.js @@ -114,11 +114,11 @@ export function sanitizeHtml(unsafeHtml, allowList, sanitizeFn) { const attributeList = [].concat(...element.attributes) const allowedAttributes = [].concat(allowList['*'] || [], allowList[elementName] || []) - attributeList.forEach(attribute => { + for (const attribute of attributeList) { if (!allowedAttribute(attribute, allowedAttributes)) { element.removeAttribute(attribute.nodeName) } - }) + } } return createdDocument.body.innerHTML diff --git a/js/src/util/scrollbar.js b/js/src/util/scrollbar.js index 2d5d0ffa65..f9a2d992dd 100644 --- a/js/src/util/scrollbar.js +++ b/js/src/util/scrollbar.js @@ -85,7 +85,9 @@ class ScrollBarHelper { if (isElement(selector)) { callBack(selector) } else { - SelectorEngine.find(selector, this._element).forEach(callBack) + for (const sel of SelectorEngine.find(selector, this._element)) { + callBack(sel) + } } } |