Skip to content

Commit

Permalink
Boost execute function, being able to handle arguments (#36652)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoSot authored Oct 7, 2022
1 parent 708a3a0 commit 4cb046a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
3 changes: 2 additions & 1 deletion js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as Popper from '@popperjs/core'
import {
defineJQueryPlugin,
execute,
getElement,
getNextActiveElement,
isDisabled,
Expand Down Expand Up @@ -319,7 +320,7 @@ class Dropdown extends BaseComponent {

return {
...defaultBsPopperConfig,
...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
...execute(this._config.popperConfig, [defaultBsPopperConfig])
}
}

Expand Down
10 changes: 4 additions & 6 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import * as Popper from '@popperjs/core'
import { defineJQueryPlugin, findShadowRoot, getElement, getUID, isRTL, noop } from './util/index'
import { defineJQueryPlugin, execute, findShadowRoot, getElement, getUID, isRTL, noop } from './util/index'
import { DefaultAllowlist } from './util/sanitizer'
import EventHandler from './dom/event-handler'
import Manipulator from './dom/manipulator'
Expand Down Expand Up @@ -370,9 +370,7 @@ class Tooltip extends BaseComponent {
}

_createPopper(tip) {
const placement = typeof this._config.placement === 'function' ?
this._config.placement.call(this, tip, this._element) :
this._config.placement
const placement = execute(this._config.placement, [this, tip, this._element])
const attachment = AttachmentMap[placement.toUpperCase()]
return Popper.createPopper(this._element, tip, this._getPopperConfig(attachment))
}
Expand All @@ -392,7 +390,7 @@ class Tooltip extends BaseComponent {
}

_resolvePossibleFunction(arg) {
return typeof arg === 'function' ? arg.call(this._element) : arg
return execute(arg, [this._element])
}

_getPopperConfig(attachment) {
Expand Down Expand Up @@ -438,7 +436,7 @@ class Tooltip extends BaseComponent {

return {
...defaultBsPopperConfig,
...(typeof this._config.popperConfig === 'function' ? this._config.popperConfig(defaultBsPopperConfig) : this._config.popperConfig)
...execute(this._config.popperConfig, [defaultBsPopperConfig])
}
}

Expand Down
6 changes: 2 additions & 4 deletions js/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,8 @@ const defineJQueryPlugin = plugin => {
})
}

const execute = callback => {
if (typeof callback === 'function') {
callback()
}
const execute = (possibleCallback, args = [], defaultValue = possibleCallback) => {
return typeof possibleCallback === 'function' ? possibleCallback(...args) : defaultValue
}

const executeAfterTransition = (callback, transitionElement, waitForTransition = true) => {
Expand Down
4 changes: 2 additions & 2 deletions js/src/util/template-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

import { DefaultAllowlist, sanitizeHtml } from './sanitizer'
import { getElement, isElement } from '../util/index'
import { execute, getElement, isElement } from '../util/index'
import SelectorEngine from '../dom/selector-engine'
import Config from './config'

Expand Down Expand Up @@ -143,7 +143,7 @@ class TemplateFactory extends Config {
}

_resolvePossibleFunction(arg) {
return typeof arg === 'function' ? arg(this) : arg
return execute(arg, [this])
}

_putElementInTemplate(element, templateElement) {
Expand Down
19 changes: 19 additions & 0 deletions js/tests/unit/util/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,25 @@ describe('Util', () => {
Util.execute(spy)
expect(spy).toHaveBeenCalled()
})

it('should execute if arg is function & return the result', () => {
const functionFoo = (num1, num2 = 10) => num1 + num2
const resultFoo = Util.execute(functionFoo, [4, 5])
expect(resultFoo).toBe(9)

const resultFoo1 = Util.execute(functionFoo, [4])
expect(resultFoo1).toBe(14)

const functionBar = () => 'foo'
const resultBar = Util.execute(functionBar)
expect(resultBar).toBe('foo')
})

it('should not execute if arg is not function & return default argument', () => {
const foo = 'bar'
expect(Util.execute(foo)).toBe('bar')
expect(Util.execute(foo, [], 4)).toBe(4)
})
})

describe('executeAfterTransition', () => {
Expand Down

0 comments on commit 4cb046a

Please sign in to comment.