From eb899f070200476b8fa3dd96481e4af755afb819 Mon Sep 17 00:00:00 2001 From: jiaxiang Date: Wed, 30 Oct 2024 14:54:49 +0800 Subject: [PATCH] refactor: replace typeof number with the built-in isNumber (#18706) * refactor: replace typeof number with the built-in isNumber * style: format --- .../components/slider/src/composables/use-lifecycle.ts | 7 ++----- packages/components/slider/src/composables/use-watch.ts | 4 ++-- packages/components/table/src/config.ts | 4 ++-- packages/components/table/src/table-layout.ts | 9 ++++----- packages/components/table/src/util.ts | 3 ++- .../src/time-picker-com/basic-time-spinner.vue | 4 ++-- 6 files changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/components/slider/src/composables/use-lifecycle.ts b/packages/components/slider/src/composables/use-lifecycle.ts index 6b3e770d04..60c44f5203 100644 --- a/packages/components/slider/src/composables/use-lifecycle.ts +++ b/packages/components/slider/src/composables/use-lifecycle.ts @@ -1,6 +1,6 @@ import { nextTick, onMounted, ref } from 'vue' import { useEventListener } from '@vueuse/core' -import { isArray } from '@element-plus/utils' +import { isArray, isNumber } from '@element-plus/utils' import type { SliderInitData, SliderProps } from '../slider' export const useLifecycle = ( @@ -21,10 +21,7 @@ export const useLifecycle = ( } initData.oldValue = [initData.firstValue, initData.secondValue] } else { - if ( - typeof props.modelValue !== 'number' || - Number.isNaN(props.modelValue) - ) { + if (!isNumber(props.modelValue) || Number.isNaN(props.modelValue)) { initData.firstValue = props.min } else { initData.firstValue = Math.min( diff --git a/packages/components/slider/src/composables/use-watch.ts b/packages/components/slider/src/composables/use-watch.ts index ce9690090e..622c5c62df 100644 --- a/packages/components/slider/src/composables/use-watch.ts +++ b/packages/components/slider/src/composables/use-watch.ts @@ -1,6 +1,6 @@ import { watch } from 'vue' import { INPUT_EVENT, UPDATE_MODEL_EVENT } from '@element-plus/constants' -import { debugWarn, isArray, throwError } from '@element-plus/utils' +import { debugWarn, isArray, isNumber, throwError } from '@element-plus/utils' import type { ComputedRef, SetupContext } from 'vue' import type { Arrayable } from '@element-plus/utils' import type { FormItemContext } from '@element-plus/components/form' @@ -53,7 +53,7 @@ export const useWatch = ( initData.oldValue = val.slice() } } - } else if (!props.range && typeof val === 'number' && !Number.isNaN(val)) { + } else if (!props.range && isNumber(val) && !Number.isNaN(val)) { if (val < props.min) { _emit(props.min) } else if (val > props.max) { diff --git a/packages/components/table/src/config.ts b/packages/components/table/src/config.ts index fbeed05aea..13fa9d8a2f 100644 --- a/packages/components/table/src/config.ts +++ b/packages/components/table/src/config.ts @@ -3,7 +3,7 @@ import { h } from 'vue' import ElCheckbox from '@element-plus/components/checkbox' import { ElIcon } from '@element-plus/components/icon' import { ArrowRight, Loading } from '@element-plus/icons-vue' -import { getProp, isFunction } from '@element-plus/utils' +import { getProp, isFunction, isNumber } from '@element-plus/utils' import type { VNode } from 'vue' import type { TableColumnCtx } from './table-column/defaults' @@ -102,7 +102,7 @@ export const cellForced = { let i = $index + 1 const index = column.index - if (typeof index === 'number') { + if (isNumber(index)) { i = $index + index } else if (isFunction(index)) { i = index($index) diff --git a/packages/components/table/src/table-layout.ts b/packages/components/table/src/table-layout.ts index 2b44d7abdd..8addae5e76 100644 --- a/packages/components/table/src/table-layout.ts +++ b/packages/components/table/src/table-layout.ts @@ -1,6 +1,6 @@ // @ts-nocheck import { isRef, nextTick, ref } from 'vue' -import { hasOwn, isClient } from '@element-plus/utils' +import { hasOwn, isClient, isNumber } from '@element-plus/utils' import { parseHeight } from './util' import type { Ref } from 'vue' @@ -86,7 +86,7 @@ class TableLayout { if (!el && (value || value === 0)) return nextTick(() => this.setHeight(value, prop)) - if (typeof value === 'number') { + if (isNumber(value)) { el.style[prop] = `${value}px` this.updateElsHeight() } else if (typeof value === 'string') { @@ -139,12 +139,11 @@ class TableLayout { const flattenColumns = this.getFlattenColumns() const flexColumns = flattenColumns.filter( - (column) => typeof column.width !== 'number' + (column) => !isNumber(column.width) ) flattenColumns.forEach((column) => { // Clean those columns whose width changed from flex to unflex - if (typeof column.width === 'number' && column.realWidth) - column.realWidth = null + if (isNumber(column.width) && column.realWidth) column.realWidth = null }) if (flexColumns.length > 0 && fit) { flattenColumns.forEach((column) => { diff --git a/packages/components/table/src/util.ts b/packages/components/table/src/util.ts index 75dfdb1d8f..4f0817d27a 100644 --- a/packages/components/table/src/util.ts +++ b/packages/components/table/src/util.ts @@ -6,6 +6,7 @@ import { isArray, isBoolean, isFunction, + isNumber, isObject, throwError, } from '@element-plus/utils' @@ -233,7 +234,7 @@ export function parseMinWidth(minWidth: number | string): number | string { } export function parseHeight(height: number | string) { - if (typeof height === 'number') { + if (isNumber(height)) { return height } if (typeof height === 'string') { diff --git a/packages/components/time-picker/src/time-picker-com/basic-time-spinner.vue b/packages/components/time-picker/src/time-picker-com/basic-time-spinner.vue index 010a485c78..4f8aefd03a 100644 --- a/packages/components/time-picker/src/time-picker-com/basic-time-spinner.vue +++ b/packages/components/time-picker/src/time-picker-com/basic-time-spinner.vue @@ -62,7 +62,7 @@ ns.is('disabled', timeList[item][time!]), ]" > -