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 */ /* eslint-disable */
class Dayjs { class Dayjs {
utc: boolean utc: boolean
@ -28,7 +30,7 @@ class Dayjs {
parseConfig(dateStr?:string | number | Date) { parseConfig(dateStr?:string | number | Date) {
if (!dateStr) return new Date() if (!dateStr) return new Date()
if (dateStr instanceof Date) return dateStr if (isDate(dateStr)) return dateStr
if (/^(\d){8}$/.test(dateStr as string)) { if (/^(\d){8}$/.test(dateStr as string)) {
this.utc = true this.utc = true
return `${(dateStr as string).substr(0, 4)}-${(dateStr as string).substr(4, 2)}-${(dateStr as string).substr(6, 2)}` 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 return new Date(obj.getTime()) as any
} }
if (obj instanceof RegExp) { if (obj instanceof RegExp) {

View File

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

View File

@ -50,7 +50,7 @@ import {
getWeekRange getWeekRange
} from '../utils' } from '../utils'
import { useToast } from '../../wd-toast' 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 { useTranslate } from '../../composables/useTranslate'
import type { CalendarDayItem, CalendarDayType, CalendarType } from '../types' import type { CalendarDayItem, CalendarDayType, CalendarType } from '../types'
import { monthProps } 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 disabled: compareDate(date, props.minDate) === -1 || compareDate(date, props.maxDate) === 1
} }
if (props.formatter) { if (props.formatter) {
if (getType(props.formatter) === 'function') { if (isFunction(props.formatter)) {
dayObj = props.formatter(dayObj) dayObj = props.formatter(dayObj)
} else { } else {
console.error('[wot-design] error(wd-calendar-view): the formatter prop of wd-calendar-view should be a function') 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 { dayjs } from '../common/dayjs'
import { getType, isArray, padZero } from '../common/util' import { isArray, isFunction, padZero } from '../common/util'
import { useTranslate } from '../composables/useTranslate' import { useTranslate } from '../composables/useTranslate'
import type { CalendarDayType, CalendarItem, CalendarTimeFilter, CalendarType } from './types' import type { CalendarDayType, CalendarItem, CalendarTimeFilter, CalendarType } from './types'
const { translate } = useTranslate('calendar-view') const { translate } = useTranslate('calendar-view')
@ -380,7 +380,7 @@ export function getTimeData({
} }
}) })
let seconds: CalendarItem[] = [] let seconds: CalendarItem[] = []
if (filter && getType(filter) === 'function') { if (filter && isFunction(filter)) {
hours = filter({ hours = filter({
type: 'hour', type: 'hour',
values: hours values: hours
@ -399,7 +399,7 @@ export function getTimeData({
disabled: index < minSecond || index > maxSecond disabled: index < minSecond || index > maxSecond
} }
}) })
if (filter && getType(filter) === 'function') { if (filter && isFunction(filter)) {
seconds = filter({ seconds = filter({
type: 'second', type: 'second',
values: seconds values: seconds

View File

@ -29,7 +29,7 @@ export default {
<script lang="ts" setup> <script lang="ts" setup>
import { computed, ref, watch } from 'vue' 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 { compareMonth, formatYearTitle, getDateByDefaultTime, getItemClass, getMonthByOffset, getMonthOffset } from '../utils'
import { useToast } from '../../wd-toast' import { useToast } from '../../wd-toast'
import { useTranslate } from '../../composables/useTranslate' 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 disabled: compareMonth(date, props.minDate) === -1 || compareMonth(date, props.maxDate) === 1
} }
if (props.formatter) { if (props.formatter) {
if (getType(props.formatter) === 'function') { if (isFunction(props.formatter)) {
monthObj = props.formatter(monthObj) monthObj = props.formatter(monthObj)
} else { } else {
console.error('[wot-design] error(wd-calendar-view): the formatter prop of wd-calendar-view should be a function') 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> <script lang="ts" setup>
import { computed, onMounted, ref, nextTick } from 'vue' import { computed, onMounted, ref, nextTick } from 'vue'
import { compareYear, formatYearTitle, getYears } from '../utils' 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 Year from '../year/year.vue'
import { yearPanelProps, type YearInfo, type YearPanelExpose } from './types' import { yearPanelProps, type YearInfo, type YearPanelExpose } from './types'
@ -81,8 +81,7 @@ const requestAnimationFrame = (cb = () => {}) => {
function scrollIntoView() { function scrollIntoView() {
requestAnimationFrame().then(() => { requestAnimationFrame().then(() => {
let activeDate let activeDate: number | null = null
const type = getType(props.value)
if (isArray(props.value)) { if (isArray(props.value)) {
activeDate = props.value![0] activeDate = props.value![0]
} else if (isNumber(props.value)) { } else if (isNumber(props.value)) {

View File

@ -404,7 +404,7 @@ function setInnerLabel() {
}) })
} }
function setShowValue() { 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) showValue.value = (props.displayFormat || defaultDisplayFormat)(calendarValue.value, currentType.value)
} else { } else {
showValue.value = '' showValue.value = ''

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -52,7 +52,7 @@ export default {
<script lang="ts" setup> <script lang="ts" setup>
import { ref, watch } from 'vue' 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 { chooseFile } from './utils'
import { useTranslate } from '../composables/useTranslate' import { useTranslate } from '../composables/useTranslate'
import { uploadProps, type UploadFileItem } from './types' import { uploadProps, type UploadFileItem } from './types'
@ -99,7 +99,7 @@ watch(
watch( watch(
() => props.beforePreview, () => props.beforePreview,
(fn) => { (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') console.error('The type of beforePreview must be Function')
} }
}, },
@ -112,7 +112,7 @@ watch(
watch( watch(
() => props.onPreviewFail, () => props.onPreviewFail,
(fn) => { (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') console.error('The type of onPreviewFail must be Function')
} }
}, },
@ -125,7 +125,7 @@ watch(
watch( watch(
() => props.beforeRemove, () => props.beforeRemove,
(fn) => { (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') console.error('The type of beforeRemove must be Function')
} }
}, },
@ -138,7 +138,7 @@ watch(
watch( watch(
() => props.beforeUpload, () => props.beforeUpload,
(fn) => { (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') console.error('The type of beforeUpload must be Function')
} }
}, },
@ -151,7 +151,7 @@ watch(
watch( watch(
() => props.beforeChoose, () => props.beforeChoose,
(fn) => { (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') console.error('The type of beforeChoose must be Function')
} }
}, },
@ -164,7 +164,7 @@ watch(
watch( watch(
() => props.buildFormData, () => props.buildFormData,
(fn) => { (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') console.error('The type of buildFormData must be Function')
} }
}, },