Skip to content

Commit d9a9672

Browse files
authored
chore: minor refactors in code optimization for better minification (#5258)
1 parent d57a643 commit d9a9672

File tree

22 files changed

+90
-56
lines changed

22 files changed

+90
-56
lines changed

src/components/aspect/aspect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Vue from '../../utils/vue'
2+
import { mathAbs } from '../../utils/math'
23
import { toFloat } from '../../utils/number'
34
import normalizeSlotMixin from '../../mixins/normalize-slot'
45

@@ -37,7 +38,7 @@ export const BAspect = /*#__PURE__*/ Vue.extend({
3738
} else {
3839
ratio = toFloat(aspect) || 1
3940
}
40-
return `${100 / Math.abs(ratio)}%`
41+
return `${100 / mathAbs(ratio)}%`
4142
}
4243
},
4344
render(h) {

src/components/calendar/calendar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import { requestAF } from '../../utils/dom'
2525
import { isArray, isFunction, isPlainObject, isString } from '../../utils/inspect'
2626
import { isLocaleRTL } from '../../utils/locale'
27+
import { mathMax } from '../../utils/math'
2728
import { toInteger } from '../../utils/number'
2829
import { toString } from '../../utils/string'
2930
import idMixin from '../../mixins/id'
@@ -287,7 +288,7 @@ export const BCalendar = Vue.extend({
287288
},
288289
computedWeekStarts() {
289290
// `startWeekday` is a prop (constrained to `0` through `6`)
290-
return Math.max(toInteger(this.startWeekday, 0), 0) % 7
291+
return mathMax(toInteger(this.startWeekday, 0), 0) % 7
291292
},
292293
computedLocale() {
293294
// Returns the resolved locale used by the calendar

src/components/carousel/carousel.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { selectAll, reflow, addClass, removeClass, setAttr } from '../../utils/d
77
import { isBrowser, hasTouchSupport, hasPointerEventSupport } from '../../utils/env'
88
import { EVENT_OPTIONS_NO_CAPTURE, eventOn, eventOff } from '../../utils/events'
99
import { isUndefined } from '../../utils/inspect'
10+
import { mathAbs, mathFloor, mathMax, mathMin } from '../../utils/math'
1011
import { toInteger } from '../../utils/number'
1112
import idMixin from '../../mixins/id'
1213
import normalizeSlotMixin from '../../mixins/normalize-slot'
@@ -235,7 +236,7 @@ export const BCarousel = /*#__PURE__*/ Vue.extend({
235236
const noWrap = this.noWrap
236237
const numSlides = this.numSlides
237238
// Make sure we have an integer (you never know!)
238-
slide = Math.floor(slide)
239+
slide = mathFloor(slide)
239240
// Don't do anything if nothing to slide to
240241
if (numSlides === 0) {
241242
return
@@ -295,7 +296,7 @@ export const BCarousel = /*#__PURE__*/ Vue.extend({
295296
}
296297
// Don't start if no interval, or less than 2 slides
297298
if (this.interval && this.numSlides > 1) {
298-
this._intervalId = setInterval(this.next, Math.max(1000, this.interval))
299+
this._intervalId = setInterval(this.next, mathMax(1000, this.interval))
299300
}
300301
},
301302
// Restart auto rotate slides when focus/hover leaves the carousel
@@ -392,7 +393,7 @@ export const BCarousel = /*#__PURE__*/ Vue.extend({
392393
this.slides = selectAll('.carousel-item', this.$refs.inner)
393394
const numSlides = this.slides.length
394395
// Keep slide number in range
395-
const index = Math.max(0, Math.min(Math.floor(this.index), numSlides - 1))
396+
const index = mathMax(0, mathMin(mathFloor(this.index), numSlides - 1))
396397
this.slides.forEach((slide, idx) => {
397398
const n = idx + 1
398399
if (idx === index) {
@@ -425,7 +426,7 @@ export const BCarousel = /*#__PURE__*/ Vue.extend({
425426
},
426427
/* istanbul ignore next */
427428
handleSwipe() /* istanbul ignore next: JSDOM doesn't support touch events */ {
428-
const absDeltaX = Math.abs(this.touchDeltaX)
429+
const absDeltaX = mathAbs(this.touchDeltaX)
429430
if (absDeltaX <= SWIPE_THRESHOLD) {
430431
return
431432
}
@@ -477,7 +478,7 @@ export const BCarousel = /*#__PURE__*/ Vue.extend({
477478
}
478479
this._touchTimeout = setTimeout(
479480
this.start,
480-
TOUCH_EVENT_COMPAT_WAIT + Math.max(1000, this.interval)
481+
TOUCH_EVENT_COMPAT_WAIT + mathMax(1000, this.interval)
481482
)
482483
}
483484
},

src/components/form-rating/form-rating.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { arrayIncludes, concat } from '../../utils/array'
33
import { getComponentConfig } from '../../utils/config'
44
import { isNull } from '../../utils/inspect'
55
import { isLocaleRTL } from '../../utils/locale'
6+
import { mathMax, mathMin } from '../../utils/math'
67
import { toInteger, toFloat } from '../../utils/number'
78
import { toString } from '../../utils/string'
89
import identity from '../../utils/identity'
@@ -90,9 +91,9 @@ const BVFormRatingStar = Vue.extend({
9091
})
9192

9293
// --- Utility methods ---
93-
const computeStars = stars => Math.max(MIN_STARS, toInteger(stars, DEFAULT_STARS))
94+
const computeStars = stars => mathMax(MIN_STARS, toInteger(stars, DEFAULT_STARS))
9495

95-
const clampValue = (value, min, max) => Math.max(Math.min(value, max), min)
96+
const clampValue = (value, min, max) => mathMax(mathMin(value, max), min)
9697

9798
// --- BFormRating ---
9899
// @vue/component

src/components/form-spinbutton/form-spinbutton.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getComponentConfig } from '../../utils/config'
44
import { eventOnOff } from '../../utils/events'
55
import { isFunction, isNull } from '../../utils/inspect'
66
import { isLocaleRTL } from '../../utils/locale'
7+
import { mathFloor, mathMax, mathPow, mathRound } from '../../utils/math'
78
import { toFloat, toInteger } from '../../utils/number'
89
import { toString } from '../../utils/string'
910
import identity from '../../utils/identity'
@@ -161,7 +162,7 @@ export const BFormSpinbutton = /*#__PURE__*/ Vue.extend({
161162
const max = toFloat(this.max, DEFAULT_MAX)
162163
const step = this.computedStep
163164
const min = this.computedMin
164-
return Math.floor((max - min) / step) * step + min
165+
return mathFloor((max - min) / step) * step + min
165166
},
166167
computedDelay() {
167168
const delay = toInteger(this.repeatDelay, 0)
@@ -172,18 +173,18 @@ export const BFormSpinbutton = /*#__PURE__*/ Vue.extend({
172173
return interval > 0 ? interval : DEFAULT_REPEAT_INTERVAL
173174
},
174175
computedThreshold() {
175-
return Math.max(toInteger(this.repeatThreshold, DEFAULT_REPEAT_THRESHOLD), 1)
176+
return mathMax(toInteger(this.repeatThreshold, DEFAULT_REPEAT_THRESHOLD), 1)
176177
},
177178
computedStepMultiplier() {
178-
return Math.max(toInteger(this.repeatStepMultiplier, DEFAULT_REPEAT_MULTIPLIER), 1)
179+
return mathMax(toInteger(this.repeatStepMultiplier, DEFAULT_REPEAT_MULTIPLIER), 1)
179180
},
180181
computedPrecision() {
181182
// Quick and dirty way to get the number of decimals
182183
const step = this.computedStep
183-
return Math.floor(step) === step ? 0 : (step.toString().split('.')[1] || '').length
184+
return mathFloor(step) === step ? 0 : (step.toString().split('.')[1] || '').length
184185
},
185186
computedMultiplier() {
186-
return Math.pow(10, this.computedPrecision || 0)
187+
return mathPow(10, this.computedPrecision || 0)
187188
},
188189
valueAsFixed() {
189190
const value = this.localValue
@@ -274,9 +275,9 @@ export const BFormSpinbutton = /*#__PURE__*/ Vue.extend({
274275
const multiplier = this.computedMultiplier
275276
const wrap = this.wrap
276277
// We ensure that the value steps like a native input
277-
value = Math.round((value - min) / step) * step + min + step
278+
value = mathRound((value - min) / step) * step + min + step
278279
// We ensure that precision is maintained (decimals)
279-
value = Math.round(value * multiplier) / multiplier
280+
value = mathRound(value * multiplier) / multiplier
280281
// Handle if wrapping is enabled
281282
this.localValue =
282283
value > max ? (wrap ? min : max) : value < min ? (wrap ? max : min) : value

src/components/form-textarea/form-textarea.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import formValidityMixin from '../../mixins/form-validity'
1010
import listenOnRootMixin from '../../mixins/listen-on-root'
1111
import { getCS, isVisible, requestAF } from '../../utils/dom'
1212
import { isNull } from '../../utils/inspect'
13+
import { mathCeil, mathMax, mathMin } from '../../utils/math'
1314
import { toInteger, toFloat } from '../../utils/number'
1415

1516
// @vue/component
@@ -79,10 +80,10 @@ export const BFormTextarea = /*#__PURE__*/ Vue.extend({
7980
// Ensure rows is at least 2 and positive (2 is the native textarea value)
8081
// A value of 1 can cause issues in some browsers, and most browsers
8182
// only support 2 as the smallest value
82-
return Math.max(toInteger(this.rows, 2), 2)
83+
return mathMax(toInteger(this.rows, 2), 2)
8384
},
8485
computedMaxRows() {
85-
return Math.max(this.computedMinRows, toInteger(this.maxRows, 0))
86+
return mathMax(this.computedMinRows, toInteger(this.maxRows, 0))
8687
},
8788
computedRows() {
8889
// This is used to set the attribute 'rows' on the textarea
@@ -150,11 +151,11 @@ export const BFormTextarea = /*#__PURE__*/ Vue.extend({
150151
el.style.height = oldHeight
151152

152153
// Calculate content height in 'rows' (scrollHeight includes padding but not border)
153-
const contentRows = Math.max((scrollHeight - padding) / lineHeight, 2)
154+
const contentRows = mathMax((scrollHeight - padding) / lineHeight, 2)
154155
// Calculate number of rows to display (limited within min/max rows)
155-
const rows = Math.min(Math.max(contentRows, this.computedMinRows), this.computedMaxRows)
156+
const rows = mathMin(mathMax(contentRows, this.computedMinRows), this.computedMaxRows)
156157
// Calculate the required height of the textarea including border and padding (in pixels)
157-
const height = Math.max(Math.ceil(rows * lineHeight + offset), minHeight)
158+
const height = mathMax(mathCeil(rows * lineHeight + offset), minHeight)
158159

159160
// Computed height remains the larger of `oldHeight` and new `height`,
160161
// when height is in `sticky` mode (prop `no-auto-shrink` is true)

src/components/layout/row.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { mergeData } from 'vue-functional-data-merge'
22
import identity from '../../utils/identity'
33
import memoize from '../../utils/memoize'
44
import suffixPropName from '../../utils/suffix-prop-name'
5-
import { arrayIncludes } from '../../utils/array'
5+
import { arrayIncludes, concat } from '../../utils/array'
66
import { getBreakpointsUpCached } from '../../utils/config'
77
import { create, keys } from '../../utils/object'
88
import { lowerCase, toString, trim } from '../../utils/string'
@@ -58,18 +58,17 @@ const generateProps = () => {
5858
alignV: {
5959
type: String,
6060
default: null,
61-
validator: str => arrayIncludes(COMMON_ALIGNMENT.concat(['baseline', 'stretch']), str)
61+
validator: str => arrayIncludes(concat(COMMON_ALIGNMENT, 'baseline', 'stretch'), str)
6262
},
6363
alignH: {
6464
type: String,
6565
default: null,
66-
validator: str => arrayIncludes(COMMON_ALIGNMENT.concat(['between', 'around']), str)
66+
validator: str => arrayIncludes(concat(COMMON_ALIGNMENT, 'between', 'around'), str)
6767
},
6868
alignContent: {
6969
type: String,
7070
default: null,
71-
validator: str =>
72-
arrayIncludes(COMMON_ALIGNMENT.concat(['between', 'around', 'stretch']), str)
71+
validator: str => arrayIncludes(concat(COMMON_ALIGNMENT, 'between', 'around', 'stretch'), str)
7372
},
7473
...rowColsProps
7574
}

src/components/pagination-nav/pagination-nav.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getComponentConfig } from '../../utils/config'
44
import { requestAF } from '../../utils/dom'
55
import { isBrowser } from '../../utils/env'
66
import { isArray, isUndefined, isFunction, isObject } from '../../utils/inspect'
7+
import { mathMax } from '../../utils/math'
78
import { toInteger } from '../../utils/number'
89
import { computeHref, parseQuery } from '../../utils/router'
910
import { toString } from '../../utils/string'
@@ -13,7 +14,7 @@ import paginationMixin from '../../mixins/pagination'
1314
const NAME = 'BPaginationNav'
1415

1516
// Sanitize the provided number of pages (converting to a number)
16-
export const sanitizeNumberOfPages = value => Math.max(toInteger(value, 0), 1)
17+
export const sanitizeNumberOfPages = value => mathMax(toInteger(value, 0), 1)
1718

1819
const props = {
1920
size: {

src/components/pagination/pagination.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from '../../utils/vue'
22
import { getComponentConfig } from '../../utils/config'
33
import { isVisible } from '../../utils/dom'
44
import { isUndefinedOrNull } from '../../utils/inspect'
5+
import { mathCeil, mathMax } from '../../utils/math'
56
import { toInteger } from '../../utils/number'
67
import paginationMixin from '../../mixins/pagination'
78

@@ -34,10 +35,10 @@ const props = {
3435
// --- Helper functions ---
3536

3637
// Sanitize the provided per page number (converting to a number)
37-
const sanitizePerPage = val => Math.max(toInteger(val) || DEFAULT_PER_PAGE, 1)
38+
const sanitizePerPage = val => mathMax(toInteger(val) || DEFAULT_PER_PAGE, 1)
3839

3940
// Sanitize the provided total rows number (converting to a number)
40-
const sanitizeTotalRows = val => Math.max(toInteger(val) || DEFAULT_TOTAL_ROWS, 0)
41+
const sanitizeTotalRows = val => mathMax(toInteger(val) || DEFAULT_TOTAL_ROWS, 0)
4142

4243
// The render function is brought in via the `paginationMixin`
4344
// @vue/component
@@ -47,7 +48,7 @@ export const BPagination = /*#__PURE__*/ Vue.extend({
4748
props,
4849
computed: {
4950
numberOfPages() {
50-
const result = Math.ceil(sanitizeTotalRows(this.totalRows) / sanitizePerPage(this.perPage))
51+
const result = mathCeil(sanitizeTotalRows(this.totalRows) / sanitizePerPage(this.perPage))
5152
return result < 1 ? 1 : result
5253
},
5354
pageSizeNumberOfPages() {

src/components/progress/progress-bar.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Vue from '../../utils/vue'
22
import { getComponentConfig } from '../../utils/config'
33
import { htmlOrText } from '../../utils/html'
44
import { isBoolean } from '../../utils/inspect'
5+
import { mathMax, mathPow } from '../../utils/math'
56
import { toFixed, toFloat, toInteger } from '../../utils/number'
67
import { toString } from '../../utils/string'
78
import normalizeSlotMixin from '../../mixins/normalize-slot'
@@ -87,11 +88,11 @@ export const BProgressBar = /*#__PURE__*/ Vue.extend({
8788
computedPrecision() {
8889
// Prefer our precision over parent setting
8990
// Default to `0` for invalid values (`-x`, `NaN`)
90-
return Math.max(toInteger(this.precision, toInteger(this.bvProgress.precision, 0)), 0)
91+
return mathMax(toInteger(this.precision, toInteger(this.bvProgress.precision, 0)), 0)
9192
},
9293
computedProgress() {
9394
const precision = this.computedPrecision
94-
const p = Math.pow(10, precision)
95+
const p = mathPow(10, precision)
9596
return toFixed((100 * p * this.computedValue) / this.computedMax / p, precision)
9697
},
9798
computedVariant() {

src/components/table/helpers/mixin-items.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import looseEqual from '../../../utils/loose-equal'
22
import { isArray, isFunction, isString, isUndefinedOrNull } from '../../../utils/inspect'
3+
import { mathMax } from '../../../utils/math'
34
import { toInteger } from '../../../utils/number'
45
import { clone } from '../../../utils/object'
56
import normalizeFields from './normalize-fields'
@@ -86,8 +87,8 @@ export default {
8687
filter: this.localFilter,
8788
sortBy: this.localSortBy,
8889
sortDesc: this.localSortDesc,
89-
perPage: Math.max(toInteger(this.perPage, 0), 0),
90-
currentPage: Math.max(toInteger(this.currentPage, 0), 1),
90+
perPage: mathMax(toInteger(this.perPage, 0), 0),
91+
currentPage: mathMax(toInteger(this.currentPage, 0), 1),
9192
apiUrl: this.apiUrl
9293
}
9394
}

src/components/table/helpers/mixin-pagination.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { mathMax } from '../../../utils/math'
12
import { toInteger } from '../../../utils/number'
23

34
export default {
@@ -17,8 +18,8 @@ export default {
1718
},
1819
paginatedItems() {
1920
let items = this.sortedItems || this.filteredItems || this.localItems || []
20-
const currentPage = Math.max(toInteger(this.currentPage, 1), 1)
21-
const perPage = Math.max(toInteger(this.perPage, 0), 0)
21+
const currentPage = mathMax(toInteger(this.currentPage, 1), 1)
22+
const perPage = mathMax(toInteger(this.perPage, 0), 0)
2223
// Apply local pagination
2324
if (this.localPaging && !!perPage) {
2425
// Grab the current page of data (which may be past filtered items limit)

src/components/table/helpers/mixin-selectable.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import range from '../../../utils/range'
44
import { isArray, arrayIncludes } from '../../../utils/array'
55
import { getComponentConfig } from '../../../utils/config'
66
import { isNumber } from '../../../utils/inspect'
7+
import { mathMax, mathMin } from '../../../utils/math'
78
import sanitizeRow from './sanitize-row'
89

910
export default {
@@ -205,8 +206,8 @@ export default {
205206
if (this.selectedLastRow > -1 && evt.shiftKey) {
206207
// range
207208
for (
208-
let idx = Math.min(this.selectedLastRow, index);
209-
idx <= Math.max(this.selectedLastRow, index);
209+
let idx = mathMin(this.selectedLastRow, index);
210+
idx <= mathMax(this.selectedLastRow, index);
210211
idx++
211212
) {
212213
selectedRows[idx] = true

src/components/tabs/tabs.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { arrayIncludes, concat } from '../../utils/array'
88
import { BvEvent } from '../../utils/bv-event.class'
99
import { requestAF, selectAll } from '../../utils/dom'
1010
import { isEvent } from '../../utils/inspect'
11+
import { mathMax } from '../../utils/math'
1112
import { toInteger } from '../../utils/number'
1213
import { omit } from '../../utils/object'
1314
import idMixin from '../../mixins/id'
@@ -548,7 +549,7 @@ export const BTabs = /*#__PURE__*/ Vue.extend({
548549
},
549550
// Move to previous non-disabled tab
550551
previousTab(focus) {
551-
const currentIndex = Math.max(this.currentTab, 0)
552+
const currentIndex = mathMax(this.currentTab, 0)
552553
const tab = this.tabs
553554
.slice(0, currentIndex)
554555
.reverse()
@@ -560,7 +561,7 @@ export const BTabs = /*#__PURE__*/ Vue.extend({
560561
},
561562
// Move to next non-disabled tab
562563
nextTab(focus) {
563-
const currentIndex = Math.max(this.currentTab, -1)
564+
const currentIndex = mathMax(this.currentTab, -1)
564565
const tab = this.tabs.slice(currentIndex + 1).find(notDisabled)
565566
if (this.activateTab(tab) && focus) {
566567
this.focusButton(tab)

src/components/toast/toast.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BvEvent } from '../../utils/bv-event.class'
55
import { getComponentConfig } from '../../utils/config'
66
import { requestAF } from '../../utils/dom'
77
import { EVENT_OPTIONS_NO_CAPTURE, eventOnOff } from '../../utils/events'
8+
import { mathMax } from '../../utils/math'
89
import { toInteger } from '../../utils/number'
910
import idMixin from '../../mixins/id'
1011
import listenOnRootMixin from '../../mixins/listen-on-root'
@@ -144,7 +145,7 @@ export const BToast = /*#__PURE__*/ Vue.extend({
144145
},
145146
computedDuration() {
146147
// Minimum supported duration is 1 second
147-
return Math.max(toInteger(this.autoHideDelay, 0), MIN_DURATION)
148+
return mathMax(toInteger(this.autoHideDelay, 0), MIN_DURATION)
148149
},
149150
computedToaster() {
150151
return String(this.toaster)
@@ -303,7 +304,7 @@ export const BToast = /*#__PURE__*/ Vue.extend({
303304
const passed = Date.now() - this.dismissStarted
304305
if (passed > 0) {
305306
this.clearDismissTimer()
306-
this.resumeDismiss = Math.max(this.computedDuration - passed, MIN_DURATION)
307+
this.resumeDismiss = mathMax(this.computedDuration - passed, MIN_DURATION)
307308
}
308309
},
309310
onUnPause() {

0 commit comments

Comments
 (0)