Skip to content

Commit

Permalink
Object spread : less jQuery more ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S authored and XhmikosR committed Nov 8, 2017
1 parent 9deb1c6 commit 320ef6d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .babelrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
]
],
plugins: [
process.env.PLUGINS && 'transform-es2015-modules-strip'
process.env.PLUGINS && 'transform-es2015-modules-strip',
'@babel/proposal-object-rest-spread'
].filter(Boolean)
};
3 changes: 2 additions & 1 deletion build/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const plugins = [
externalHelpersWhitelist: [ // include only required helpers
'defineProperties',
'createClass',
'inheritsLoose'
'inheritsLoose',
'extends'
]
})
]
Expand Down
20 changes: 16 additions & 4 deletions js/src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,10 @@ const Carousel = (($) => {
// private

_getConfig(config) {
config = $.extend({}, Default, config)
config = {
...Default,
...config
}
Util.typeCheckConfig(NAME, config, DefaultType)
return config
}
Expand Down Expand Up @@ -428,10 +431,16 @@ const Carousel = (($) => {
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend({}, Default, $(this).data())
let _config = {
...Default,
...$(this).data()
}

if (typeof config === 'object') {
$.extend(_config, config)
_config = {
..._config,
...config
}
}

const action = typeof config === 'string' ? config : _config.slide
Expand Down Expand Up @@ -468,7 +477,10 @@ const Carousel = (($) => {
return
}

const config = $.extend({}, $(target).data(), $(this).data())
const config = {
...$(target).data(),
...$(this).data()
}
const slideIndex = this.getAttribute('data-slide-to')

if (slideIndex) {
Expand Down
16 changes: 9 additions & 7 deletions js/src/collapse.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ const Collapse = (($) => {
// private

_getConfig(config) {
config = $.extend({}, Default, config)
config = {
...Default,
...config
}
config.toggle = Boolean(config.toggle) // coerce string values
Util.typeCheckConfig(NAME, config, DefaultType)
return config
Expand Down Expand Up @@ -338,12 +341,11 @@ const Collapse = (($) => {
return this.each(function () {
const $this = $(this)
let data = $this.data(DATA_KEY)
const _config = $.extend(
{},
Default,
$this.data(),
typeof config === 'object' && config
)
const _config = {
...Default,
...$this.data(),
...typeof config === 'object' && config
}

if (!data && _config.toggle && /show|hide/.test(config)) {
_config.toggle = false
Expand Down
16 changes: 9 additions & 7 deletions js/src/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,11 @@ const Dropdown = (($) => {
}

_getConfig(config) {
config = $.extend(
{},
this.constructor.Default,
$(this._element).data(),
config
)
config = {
...this.constructor.Default,
...$(this._element).data(),
...config
}

Util.typeCheckConfig(
NAME,
Expand Down Expand Up @@ -262,7 +261,10 @@ const Dropdown = (($) => {
const offsetConf = {}
if (typeof this._config.offset === 'function') {
offsetConf.fn = (data) => {
data.offsets = $.extend({}, data.offsets, this._config.offset(data.offsets) || {})
data.offsets = {
...data.offsets,
...this._config.offset(data.offsets) || {}
}
return data
}
} else {
Expand Down
21 changes: 13 additions & 8 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ const Modal = (($) => {
// private

_getConfig(config) {
config = $.extend({}, Default, config)
config = {
...Default,
...config
}
Util.typeCheckConfig(NAME, config, DefaultType)
return config
}
Expand Down Expand Up @@ -506,12 +509,11 @@ const Modal = (($) => {
static _jQueryInterface(config, relatedTarget) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _config = $.extend(
{},
Modal.Default,
$(this).data(),
typeof config === 'object' && config
)
const _config = {
...Modal.Default,
...$(this).data(),
...typeof config === 'object' && config
}

if (!data) {
data = new Modal(this, _config)
Expand Down Expand Up @@ -547,7 +549,10 @@ const Modal = (($) => {
}

const config = $(target).data(DATA_KEY) ?
'toggle' : $.extend({}, $(target).data(), $(this).data())
'toggle' : {
...$(target).data(),
...$(this).data()
}

if (this.tagName === 'A' || this.tagName === 'AREA') {
event.preventDefault()
Expand Down
32 changes: 19 additions & 13 deletions js/src/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,25 @@ const Popover = (($) => {
const CLASS_PREFIX = 'bs-popover'
const BSCLS_PREFIX_REGEX = new RegExp(`(^|\\s)${CLASS_PREFIX}\\S+`, 'g')

const Default = $.extend({}, Tooltip.Default, {
placement : 'right',
trigger : 'click',
content : '',
template : '<div class="popover" role="tooltip">'
+ '<div class="arrow"></div>'
+ '<h3 class="popover-header"></h3>'
+ '<div class="popover-body"></div></div>'
})

const DefaultType = $.extend({}, Tooltip.DefaultType, {
content : '(string|element|function)'
})
const Default = {
...Tooltip.Default,
...{
placement : 'right',
trigger : 'click',
content : '',
template : '<div class="popover" role="tooltip">'
+ '<div class="arrow"></div>'
+ '<h3 class="popover-header"></h3>'
+ '<div class="popover-body"></div></div>'
}
}

const DefaultType = {
...Tooltip.DefaultType,
...{
content : '(string|element|function)'
}
}

const ClassName = {
FADE : 'fade',
Expand Down
5 changes: 4 additions & 1 deletion js/src/scrollspy.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ const ScrollSpy = (($) => {
// private

_getConfig(config) {
config = $.extend({}, Default, config)
config = {
...Default,
...config
}

if (typeof config.target !== 'string') {
let id = $(config.target).attr('id')
Expand Down
22 changes: 12 additions & 10 deletions js/src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,13 @@ const Tooltip = (($) => {
})

if (this.config.selector) {
this.config = $.extend({}, this.config, {
trigger : 'manual',
selector : ''
})
this.config = {
...this.config,
...{
trigger : 'manual',
selector : ''
}
}
} else {
this._fixTitle()
}
Expand Down Expand Up @@ -613,12 +616,11 @@ const Tooltip = (($) => {
}

_getConfig(config) {
config = $.extend(
{},
this.constructor.Default,
$(this.element).data(),
config
)
config = {
...this.constructor.Default,
...$(this.element).data(),
...config
}

if (typeof config.delay === 'number') {
config.delay = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"devDependencies": {
"@babel/cli": "7.0.0-beta.31",
"@babel/core": "7.0.0-beta.31",
"@babel/plugin-proposal-object-rest-spread": "7.0.0-beta.31",
"@babel/preset-env": "7.0.0-beta.31",
"autoprefixer": "^7.1.2",
"babel-eslint": "^8.0.2",
Expand Down

0 comments on commit 320ef6d

Please sign in to comment.