refactor: ♻️ 使用isFunction等工具方法替换getType

This commit is contained in:
xuqingkai 2024-03-29 13:09:25 +08:00
parent 1f8f8ae2bf
commit c8d9d263c3
15 changed files with 45 additions and 47 deletions

View File

@ -1,3 +1,5 @@
import { isDate } from './util'
/* eslint-disable */
class Dayjs {
utc: boolean
@ -28,7 +30,7 @@ class Dayjs {
parseConfig(dateStr?:string | number | Date) {
if (!dateStr) return new Date()
if (dateStr instanceof Date) return dateStr
if (isDate(dateStr)) return dateStr
if (/^(\d){8}$/.test(dateStr as string)) {
this.utc = true
return `${(dateStr as string).substr(0, 4)}-${(dateStr as string).substr(4, 2)}-${(dateStr as string).substr(6, 2)}`

View File

@ -439,7 +439,7 @@ export function deepClone<T>(obj: T, cache: Map<any, any> = new Map()): T {
}
// 处理特殊对象类型:日期、正则表达式、错误对象
if (obj instanceof Date) {
if (isDate(obj)) {
return new Date(obj.getTime()) as any
}
if (obj instanceof RegExp) {

View File

@ -73,6 +73,7 @@ export default {
<script lang="ts" setup>
import { watch, ref } from 'vue'
import { actionSheetProps, type Panel } from './types'
import { isArray } from '../common/util'
const props = defineProps(actionSheetProps)
const formatPanels = ref<Array<Panel> | Array<Panel[]>>([])
@ -91,11 +92,11 @@ watch(
const emit = defineEmits(['select', 'click-modal', 'cancel', 'closed', 'close', 'open', 'opened', 'update:modelValue'])
function isArray() {
return props.panels.length && !(props.panels[0] instanceof Array)
function isPanelArray() {
return props.panels.length && !isArray(props.panels[0])
}
function computedValue() {
formatPanels.value = isArray() ? [props.panels as Panel[]] : (props.panels as Panel[][])
formatPanels.value = isPanelArray() ? [props.panels as Panel[]] : (props.panels as Panel[][])
}
function select(rowIndex: number, type: 'action' | 'panels', colIndex?: number) {
@ -104,7 +105,7 @@ function select(rowIndex: number, type: 'action' | 'panels', colIndex?: number)
item: props.actions[rowIndex],
index: rowIndex
})
} else if (isArray()) {
} else if (isPanelArray()) {
emit('select', {
item: props.panels[Number(colIndex)],
index: colIndex

View File

@ -50,7 +50,7 @@ import {
getWeekRange
} from '../utils'
import { useToast } from '../../wd-toast'
import { deepClone, getType, isArray } from '../../common/util'
import { deepClone, isArray, isFunction } from '../../common/util'
import { useTranslate } from '../../composables/useTranslate'
import type { CalendarDayItem, CalendarDayType, CalendarType } from '../types'
import { monthProps } from './types'
@ -348,7 +348,7 @@ function getFormatterDate(date: number, day: string | number, type?: CalendarDay
disabled: compareDate(date, props.minDate) === -1 || compareDate(date, props.maxDate) === 1
}
if (props.formatter) {
if (getType(props.formatter) === 'function') {
if (isFunction(props.formatter)) {
dayObj = props.formatter(dayObj)
} else {
console.error('[wot-design] error(wd-calendar-view): the formatter prop of wd-calendar-view should be a function')

View File

@ -1,5 +1,5 @@
import { dayjs } from '../common/dayjs'
import { getType, isArray, padZero } from '../common/util'
import { isArray, isFunction, padZero } from '../common/util'
import { useTranslate } from '../composables/useTranslate'
import type { CalendarDayType, CalendarItem, CalendarTimeFilter, CalendarType } from './types'
const { translate } = useTranslate('calendar-view')
@ -380,7 +380,7 @@ export function getTimeData({
}
})
let seconds: CalendarItem[] = []
if (filter && getType(filter) === 'function') {
if (filter && isFunction(filter)) {
hours = filter({
type: 'hour',
values: hours
@ -399,7 +399,7 @@ export function getTimeData({
disabled: index < minSecond || index > maxSecond
}
})
if (filter && getType(filter) === 'function') {
if (filter && isFunction(filter)) {
seconds = filter({
type: 'second',
values: seconds

View File

@ -29,7 +29,7 @@ export default {
<script lang="ts" setup>
import { computed, ref, watch } from 'vue'
import { deepClone, getType, isArray } from '../../common/util'
import { deepClone, isArray, isFunction } from '../../common/util'
import { compareMonth, formatYearTitle, getDateByDefaultTime, getItemClass, getMonthByOffset, getMonthOffset } from '../utils'
import { useToast } from '../../wd-toast'
import { useTranslate } from '../../composables/useTranslate'
@ -181,7 +181,7 @@ function getFormatterDate(date: number, month: number, type?: CalendarDayType) {
disabled: compareMonth(date, props.minDate) === -1 || compareMonth(date, props.maxDate) === 1
}
if (props.formatter) {
if (getType(props.formatter) === 'function') {
if (isFunction(props.formatter)) {
monthObj = props.formatter(monthObj)
} else {
console.error('[wot-design] error(wd-calendar-view): the formatter prop of wd-calendar-view should be a function')

View File

@ -33,7 +33,7 @@ export default {
<script lang="ts" setup>
import { computed, onMounted, ref, nextTick } from 'vue'
import { compareYear, formatYearTitle, getYears } from '../utils'
import { getType, isArray, isNumber } from '../../common/util'
import { isArray, isNumber } from '../../common/util'
import Year from '../year/year.vue'
import { yearPanelProps, type YearInfo, type YearPanelExpose } from './types'
@ -81,8 +81,7 @@ const requestAnimationFrame = (cb = () => {}) => {
function scrollIntoView() {
requestAnimationFrame().then(() => {
let activeDate
const type = getType(props.value)
let activeDate: number | null = null
if (isArray(props.value)) {
activeDate = props.value![0]
} else if (isNumber(props.value)) {

View File

@ -404,7 +404,7 @@ function setInnerLabel() {
})
}
function setShowValue() {
if ((!(calendarValue.value instanceof Array) && calendarValue.value) || (calendarValue.value instanceof Array && calendarValue.value.length)) {
if ((!isArray(calendarValue.value) && calendarValue.value) || (isArray(calendarValue.value) && calendarValue.value.length)) {
showValue.value = (props.displayFormat || defaultDisplayFormat)(calendarValue.value, currentType.value)
} else {
showValue.value = ''

View File

@ -96,7 +96,7 @@ export default {
<script lang="ts" setup>
import { computed, getCurrentInstance, onMounted, ref, watch } from 'vue'
import { debounce, getRect, getType, isArray, isBoolean } from '../common/util'
import { debounce, getRect, isArray, isBoolean, isFunction } from '../common/util'
import { useCell } from '../composables/useCell'
import { FORM_KEY, type FormItemRule } from '../wd-form/types'
import { useParent } from '../composables/useParent'
@ -167,7 +167,7 @@ watch(
watch(
() => props.columns,
(newValue, oldValue) => {
if (newValue.length && !(newValue[0] instanceof Array)) {
if (newValue.length && !isArray(newValue[0])) {
console.error('[wot design] error(wd-col-picker): the columns props of wd-col-picker should be a two-dimensional array')
return
}
@ -195,7 +195,7 @@ watch(
watch(
() => props.columnChange,
(fn) => {
if (fn && getType(fn) !== 'function') {
if (fn && !isFunction(fn)) {
console.error('The type of columnChange must be Function')
}
},
@ -208,7 +208,7 @@ watch(
watch(
() => props.displayFormat,
(fn) => {
if (fn && getType(fn) !== 'function') {
if (fn && !isFunction(fn)) {
console.error('The type of displayFormat must be Function')
}
},
@ -221,7 +221,7 @@ watch(
watch(
() => props.beforeConfirm,
(fn) => {
if (fn && getType(fn) !== 'function') {
if (fn && !isFunction(fn)) {
console.error('The type of beforeConfirm must be Function')
}
},
@ -340,7 +340,7 @@ function handleColChange(colIndex: number, item: Record<string, any>, index: num
index: colIndex,
rowIndex: index,
resolve: (nextColumn: Record<string, any>[]) => {
if (!(nextColumn instanceof Array)) {
if (!isArray(nextColumn)) {
console.error('[wot design] error(wd-col-picker): the data of each column of wd-col-picker should be an array')
return
}

View File

@ -36,7 +36,7 @@ export default {
<script lang="ts" setup>
import { ref, watch } from 'vue'
import { debounce, getType } from '../common/util'
import { debounce, isDef } from '../common/util'
import { inputNumberProps } from './types'
const props = defineProps(inputNumberProps)
@ -109,9 +109,7 @@ function toStrictlyStep(value: number | string) {
}
function setValue(value: string | number, change: boolean = true) {
const type = getType(value)
if (props.allowNull && (type === 'null' || type === 'undefined' || value === '')) {
if (props.allowNull && (!isDef(value) || value === '')) {
dispatchChangeEvent(value, change)
return
}
@ -177,9 +175,7 @@ function dispatchChangeEvent(value: string | number, change: boolean = true) {
}
function formatValue(value: string | number) {
const type = getType(value)
if (props.allowNull && (type === 'null' || type === 'undefined' || value === '')) {
if (props.allowNull && (!isDef(value) || value === '')) {
return ''
}

View File

@ -16,7 +16,7 @@ export default {
<script lang="ts" setup>
import { computed, onBeforeMount } from 'vue'
import { addUnit, getType, objToStyle } from '../common/util'
import { addUnit, isFunction, objToStyle } from '../common/util'
import { switchProps } from './types'
const props = defineProps(switchProps)
@ -48,7 +48,7 @@ function switchValue() {
if (props.disabled) return
const newVal = props.modelValue === props.activeValue ? props.inactiveValue : props.activeValue
if (props.beforeChange && getType(props.beforeChange) === 'function') {
if (props.beforeChange && isFunction(props.beforeChange)) {
props.beforeChange({
value: newVal,
resolve: (pass: boolean) => {

View File

@ -1,12 +1,12 @@
import type { ExtractPropTypes } from 'vue'
import { baseProps, makeBooleanProp } from '../common/props'
import { baseProps, makeBooleanProp, numericProp } from '../common/props'
export const tabProps = {
...baseProps,
/**
*
*/
name: String,
name: numericProp,
/**
* tab的标题
*/

View File

@ -17,7 +17,7 @@ export default {
</script>
<script lang="ts" setup>
import { getCurrentInstance, ref, watch } from 'vue'
import { getType, isDef } from '../common/util'
import { isDef, isNumber, isString } from '../common/util'
import { useParent } from '../composables/useParent'
import { TABS_KEY } from '../wd-tabs/types'
import { computed } from 'vue'
@ -38,7 +38,7 @@ const activeIndex = computed(() => {
watch(
() => props.name,
(newValue) => {
if (newValue && getType(newValue) !== 'number' && getType(newValue) !== 'string') {
if (isDef(newValue) && !isNumber(newValue) && !isString(newValue)) {
console.error('[wot design] error(wd-tab): the type of name should be number or string')
return
}

View File

@ -136,7 +136,7 @@ export default {
</script>
<script lang="ts" setup>
import { computed, getCurrentInstance, onMounted, ref, watch, nextTick, reactive } from 'vue'
import { checkNumRange, debounce, getRect, getType, isDef, isNumber, isString, objToStyle } from '../common/util'
import { checkNumRange, debounce, getRect, isDef, isNumber, isString, objToStyle } from '../common/util'
import { useTouch } from '../composables/useTouch'
import { TABS_KEY, tabsProps } from './types'
import { useChildren } from '../composables/useChildren'
@ -217,13 +217,13 @@ const setActive = debounce(
watch(
() => props.modelValue,
(newValue) => {
if (getType(newValue) !== 'number' && getType(newValue) !== 'string') {
if (!isNumber(newValue) && !isString(newValue)) {
console.error('[wot design] error(wd-tabs): the type of value should be number or string')
}
// 0
if ((newValue as any) === '' || newValue === undefined) {
if (newValue === '' || !isDef(newValue)) {
// eslint-disable-next-line quotes
console.error("[wot design] error(wd-tabs): tabs's value cannot be null or undefined")
console.error("[wot design] error(wd-tabs): tabs's value cannot be '' null or undefined")
}
if (typeof newValue === 'number' && newValue < 0) {
// eslint-disable-next-line quotes

View File

@ -52,7 +52,7 @@ export default {
<script lang="ts" setup>
import { ref, watch } from 'vue'
import { context, getType, isDef, isEqual } from '../common/util'
import { context, getType, isDef, isEqual, isFunction } from '../common/util'
import { chooseFile } from './utils'
import { useTranslate } from '../composables/useTranslate'
import { uploadProps, type UploadFileItem } from './types'
@ -99,7 +99,7 @@ watch(
watch(
() => props.beforePreview,
(fn) => {
if (fn && getType(fn) !== 'function' && getType(fn) !== 'asyncfunction') {
if (fn && !isFunction(fn) && getType(fn) !== 'asyncfunction') {
console.error('The type of beforePreview must be Function')
}
},
@ -112,7 +112,7 @@ watch(
watch(
() => props.onPreviewFail,
(fn) => {
if (fn && getType(fn) !== 'function' && getType(fn) !== 'asyncfunction') {
if (fn && !isFunction(fn) && getType(fn) !== 'asyncfunction') {
console.error('The type of onPreviewFail must be Function')
}
},
@ -125,7 +125,7 @@ watch(
watch(
() => props.beforeRemove,
(fn) => {
if (fn && getType(fn) !== 'function' && getType(fn) !== 'asyncfunction') {
if (fn && !isFunction(fn) && getType(fn) !== 'asyncfunction') {
console.error('The type of beforeRemove must be Function')
}
},
@ -138,7 +138,7 @@ watch(
watch(
() => props.beforeUpload,
(fn) => {
if (fn && getType(fn) !== 'function' && getType(fn) !== 'asyncfunction') {
if (fn && !isFunction(fn) && getType(fn) !== 'asyncfunction') {
console.error('The type of beforeUpload must be Function')
}
},
@ -151,7 +151,7 @@ watch(
watch(
() => props.beforeChoose,
(fn) => {
if (fn && getType(fn) !== 'function' && getType(fn) !== 'asyncfunction') {
if (fn && !isFunction(fn) && getType(fn) !== 'asyncfunction') {
console.error('The type of beforeChoose must be Function')
}
},
@ -164,7 +164,7 @@ watch(
watch(
() => props.buildFormData,
(fn) => {
if (fn && getType(fn) !== 'function' && getType(fn) !== 'asyncfunction') {
if (fn && !isFunction(fn) && getType(fn) !== 'asyncfunction') {
console.error('The type of buildFormData must be Function')
}
},