From f5347bceb704ea828aeea852b14ddef139977441 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Fri, 3 Nov 2017 15:38:01 +0100 Subject: [PATCH] Object spread : less jQuery more ES6 --- .babelrc.js | 6 ++++-- build/rollup.config.js | 3 ++- js/src/carousel.js | 20 ++++++++++++++++---- js/src/collapse.js | 16 +++++++++------- js/src/dropdown.js | 16 +++++++++------- js/src/modal.js | 21 +++++++++++++-------- js/src/popover.js | 32 +++++++++++++++++++------------- js/src/scrollspy.js | 5 ++++- js/src/tooltip.js | 22 ++++++++++++---------- package-lock.json | 36 ++++++++++++++++++------------------ 10 files changed, 106 insertions(+), 71 deletions(-) diff --git a/.babelrc.js b/.babelrc.js index 693f6f8a3a4f..d313d8f61cc9 100644 --- a/.babelrc.js +++ b/.babelrc.js @@ -5,11 +5,13 @@ module.exports = { { loose: true, modules: false, - exclude: ['transform-typeof-symbol'] + exclude: ['transform-typeof-symbol'], + shippedProposals: true } ] ], plugins: [ - process.env.PLUGINS && 'transform-es2015-modules-strip' + process.env.PLUGINS && 'transform-es2015-modules-strip', + '@babel/proposal-object-rest-spread' ].filter(Boolean) }; diff --git a/build/rollup.config.js b/build/rollup.config.js index 44ffac71002d..99bf637c0d89 100644 --- a/build/rollup.config.js +++ b/build/rollup.config.js @@ -15,7 +15,8 @@ const plugins = [ externalHelpersWhitelist: [ // include only required helpers 'defineProperties', 'createClass', - 'inheritsLoose' + 'inheritsLoose', + 'extends' ] }) ] diff --git a/js/src/carousel.js b/js/src/carousel.js index 10ed2203edeb..ea443f189f06 100644 --- a/js/src/carousel.js +++ b/js/src/carousel.js @@ -223,7 +223,10 @@ const Carousel = (($) => { // private _getConfig(config) { - config = $.extend({}, Default, config) + config = { + ...Default, + ...config + } Util.typeCheckConfig(NAME, config, DefaultType) return config } @@ -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 @@ -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) { diff --git a/js/src/collapse.js b/js/src/collapse.js index f907aec54d82..1456294f43e1 100644 --- a/js/src/collapse.js +++ b/js/src/collapse.js @@ -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 @@ -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 diff --git a/js/src/dropdown.js b/js/src/dropdown.js index 57ee10ebdddf..8affedc6ce9e 100644 --- a/js/src/dropdown.js +++ b/js/src/dropdown.js @@ -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, @@ -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 { diff --git a/js/src/modal.js b/js/src/modal.js index 95565aabcceb..be3105fa150c 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -227,7 +227,10 @@ const Modal = (($) => { // private _getConfig(config) { - config = $.extend({}, Default, config) + config = { + ...Default, + ...config + } Util.typeCheckConfig(NAME, config, DefaultType) return config } @@ -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) @@ -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() diff --git a/js/src/popover.js b/js/src/popover.js index 5534f4441246..8beec963a2b0 100644 --- a/js/src/popover.js +++ b/js/src/popover.js @@ -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 : '' - }) - - const DefaultType = $.extend({}, Tooltip.DefaultType, { - content : '(string|element|function)' - }) + const Default = { + ...Tooltip.Default, + ...{ + placement : 'right', + trigger : 'click', + content : '', + template : '' + } + } + + const DefaultType = { + ...Tooltip.DefaultType, + ...{ + content : '(string|element|function)' + } + } const ClassName = { FADE : 'fade', diff --git a/js/src/scrollspy.js b/js/src/scrollspy.js index 3a13d954ac57..fd5197e383fd 100644 --- a/js/src/scrollspy.js +++ b/js/src/scrollspy.js @@ -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') diff --git a/js/src/tooltip.js b/js/src/tooltip.js index 7cefd0be6aa1..002dea429835 100644 --- a/js/src/tooltip.js +++ b/js/src/tooltip.js @@ -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() } @@ -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 = { diff --git a/package-lock.json b/package-lock.json index 7aa840df4e45..e3c2a6ede3ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -580,7 +580,7 @@ "@babel/plugin-transform-template-literals": "7.0.0-beta.31", "@babel/plugin-transform-typeof-symbol": "7.0.0-beta.31", "@babel/plugin-transform-unicode-regex": "7.0.0-beta.31", - "browserslist": "2.7.0", + "browserslist": "2.8.0", "invariant": "2.2.2", "semver": "5.4.1" } @@ -862,8 +862,8 @@ "integrity": "sha512-C9yv/UF3X+eJTi/zvfxuyfxmLibYrntpF3qoJYrMeQwgUJOZrZvpJiMG2FMQ3qnhWtF/be4pYONBBw95ZGe3vA==", "dev": true, "requires": { - "browserslist": "2.7.0", - "caniuse-lite": "1.0.30000758", + "browserslist": "2.8.0", + "caniuse-lite": "1.0.30000760", "normalize-range": "0.1.2", "num2fraction": "1.2.2", "postcss": "6.0.14", @@ -1111,12 +1111,12 @@ } }, "browserslist": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-2.7.0.tgz", - "integrity": "sha512-s34mrlczJsfbJu//mz/m9zlOy/S6tiP6El1u8iC1gTfEnzKXvxo8RAoCxS/MmojB7rd7bnfYzvKQNHykWaUWWw==", + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-2.8.0.tgz", + "integrity": "sha512-iiWHM1Et6Q4TQpB7Ar6pxuM3TNMXasVJY4Y/oh3q38EwR3Z+IdZ9MyVf7PI4MJFB4xpwMcZgs9bEUnPG2E3TCA==", "dev": true, "requires": { - "caniuse-lite": "1.0.30000758", + "caniuse-lite": "1.0.30000760", "electron-to-chromium": "1.3.27" } }, @@ -1199,15 +1199,15 @@ } }, "caniuse-db": { - "version": "1.0.30000758", - "resolved": "https://registry.npmjs.org/caniuse-db/-/caniuse-db-1.0.30000758.tgz", - "integrity": "sha1-ojViexki6Hi2MWSULJkbhN6SyBA=", + "version": "1.0.30000760", + "resolved": "https://registry.npmjs.org/caniuse-db/-/caniuse-db-1.0.30000760.tgz", + "integrity": "sha1-PqKUc+t4psywny63Osnh3r/sUo0=", "dev": true }, "caniuse-lite": { - "version": "1.0.30000758", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000758.tgz", - "integrity": "sha1-4mEUAHZlEEnPaJHtS8ZJtcjCbGk=", + "version": "1.0.30000760", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30000760.tgz", + "integrity": "sha1-7HIDlXQvHH7IlH/W3SYE53qPmP8=", "dev": true }, "capture-stack-trace": { @@ -1885,8 +1885,8 @@ "dev": true, "requires": { "babel-runtime": "6.26.0", - "browserslist": "2.7.0", - "caniuse-db": "1.0.30000758", + "browserslist": "2.8.0", + "caniuse-db": "1.0.30000760", "requireindex": "1.1.0" } }, @@ -6511,9 +6511,9 @@ } }, "rollup": { - "version": "0.50.0", - "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.50.0.tgz", - "integrity": "sha512-7RqCBQ9iwsOBPkjYgoIaeUij606mSkDMExP0NT7QDI3bqkHYQHrQ83uoNIXwPcQm/vP2VbsUz3kiyZZ1qPlLTQ==", + "version": "0.50.1", + "resolved": "https://registry.npmjs.org/rollup/-/rollup-0.50.1.tgz", + "integrity": "sha512-XwrnqjSTk+yR8GbP6hiJuVe83MVmBw/gm4P3qP34A10fRXvv6ppl0ZUg1+Pj1tIZSR/aw5ZaILLEiVxwXIAdAw==", "dev": true }, "rollup-plugin-babel": {