diff --git a/packages/vant/src/utils/basic.ts b/packages/vant/src/utils/basic.ts index 88c5aa111ae..10747d55d39 100644 --- a/packages/vant/src/utils/basic.ts +++ b/packages/vant/src/utils/basic.ts @@ -1,4 +1,3 @@ -import { isObject } from './validate'; import type { ComponentPublicInstance } from 'vue'; export function noop() {} @@ -12,6 +11,38 @@ export type Numeric = number | string; // eslint-disable-next-line export type ComponentInstance = ComponentPublicInstance<{}, any>; +export const isObject = (val: unknown): val is Record => + val !== null && typeof val === 'object'; + +export const isDef = (val: T): val is NonNullable => + val !== undefined && val !== null; + +// eslint-disable-next-line @typescript-eslint/ban-types +export const isFunction = (val: unknown): val is Function => + typeof val === 'function'; + +export const isPromise = (val: unknown): val is Promise => + isObject(val) && isFunction(val.then) && isFunction(val.catch); + +export const isDate = (val: unknown): val is Date => + Object.prototype.toString.call(val) === '[object Date]' && + !Number.isNaN((val as Date).getTime()); + +export function isMobile(value: string): boolean { + value = value.replace(/[^-|\d]/g, ''); + return ( + /^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value) + ); +} + +export const isNumeric = (val: Numeric): val is string => + typeof val === 'number' || /^\d+(\.\d+)?$/.test(val); + +export const isIOS = (): boolean => + inBrowser + ? /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase()) + : false; + export function get(object: any, path: string): any { const keys = path.split('.'); let result = object; @@ -32,14 +63,17 @@ export type RequiredParams = T extends (...args: infer P) => infer R export function pick( obj: T, keys: ReadonlyArray, - ignoreUndefined?: boolean + ignoreUndefined?: boolean, ) { - return keys.reduce((ret, key) => { - if (!ignoreUndefined || obj[key] !== undefined) { - ret[key] = obj[key]; - } - return ret; - }, {} as Writeable>); + return keys.reduce( + (ret, key) => { + if (!ignoreUndefined || obj[key] !== undefined) { + ret[key] = obj[key]; + } + return ret; + }, + {} as Writeable>, + ); } export const isSameValue = (newValue: unknown, oldValue: unknown) => diff --git a/packages/vant/src/utils/create.ts b/packages/vant/src/utils/create.ts index d8ba8dff81f..3147420ae02 100644 --- a/packages/vant/src/utils/create.ts +++ b/packages/vant/src/utils/create.ts @@ -1,6 +1,5 @@ -import { get } from './basic'; +import { get, isFunction } from './basic'; import { camelize } from './format'; -import { isFunction } from './validate'; import locale from '../locale'; export function createTranslate(name: string) { @@ -31,13 +30,13 @@ function genBem(name: string, mods?: Mods): string { if (Array.isArray(mods)) { return (mods as Mod[]).reduce( (ret, item) => ret + genBem(name, item), - '' + '', ); } return Object.keys(mods).reduce( (ret, key) => ret + (mods[key] ? genBem(name, key) : ''), - '' + '', ); } diff --git a/packages/vant/src/utils/deep-assign.ts b/packages/vant/src/utils/deep-assign.ts index c1619b16bb9..48feb161a54 100644 --- a/packages/vant/src/utils/deep-assign.ts +++ b/packages/vant/src/utils/deep-assign.ts @@ -1,4 +1,4 @@ -import { isDef, isObject } from './validate'; +import { isDef, isObject } from './basic'; type ObjectIndex = Record; diff --git a/packages/vant/src/utils/deep-clone.ts b/packages/vant/src/utils/deep-clone.ts index 72cefb27cbd..6ad16cf1673 100644 --- a/packages/vant/src/utils/deep-clone.ts +++ b/packages/vant/src/utils/deep-clone.ts @@ -1,7 +1,7 @@ -import { isDef, isObject } from './validate'; +import { isDef, isObject } from './basic'; export function deepClone | null | undefined>( - obj: T + obj: T, ): T { if (!isDef(obj)) { return obj; diff --git a/packages/vant/src/utils/dom.ts b/packages/vant/src/utils/dom.ts index bc618321a0a..660b0e42b15 100644 --- a/packages/vant/src/utils/dom.ts +++ b/packages/vant/src/utils/dom.ts @@ -1,6 +1,6 @@ import { useRect, useWindowSize } from '@vant/use'; import { unref, Ref } from 'vue'; -import { isIOS as checkIsIOS } from './validate'; +import { isIOS as checkIsIOS } from './basic'; export type ScrollElement = Element | Window; @@ -67,7 +67,7 @@ export function preventDefault(event: Event, isStopPropagation?: boolean) { } export function isHidden( - elementRef: HTMLElement | Ref + elementRef: HTMLElement | Ref, ) { const el = unref(elementRef); if (!el) { diff --git a/packages/vant/src/utils/format.ts b/packages/vant/src/utils/format.ts index 11dbd37e7f6..c1ba778196b 100644 --- a/packages/vant/src/utils/format.ts +++ b/packages/vant/src/utils/format.ts @@ -1,7 +1,7 @@ import type { CSSProperties } from 'vue'; import { inBrowser, type Numeric } from './basic'; import { windowWidth, windowHeight } from './dom'; -import { isDef, isNumeric } from './validate'; +import { isDef, isNumeric } from './basic'; export function addUnit(value?: Numeric): string | undefined { if (isDef(value)) { @@ -11,7 +11,7 @@ export function addUnit(value?: Numeric): string | undefined { } export function getSizeStyle( - originSize?: Numeric | Numeric[] + originSize?: Numeric | Numeric[], ): CSSProperties | undefined { if (isDef(originSize)) { if (Array.isArray(originSize)) { @@ -128,7 +128,7 @@ function trimExtraChar(value: string, char: string, regExp: RegExp) { export function formatNumber( value: string, allowDot = true, - allowMinus = true + allowMinus = true, ) { if (allowDot) { value = trimExtraChar(value, '.', /\./g); diff --git a/packages/vant/src/utils/index.ts b/packages/vant/src/utils/index.ts index 8ba7d36b81e..7944fb18c4e 100644 --- a/packages/vant/src/utils/index.ts +++ b/packages/vant/src/utils/index.ts @@ -4,7 +4,6 @@ export * from './dom'; export * from './create'; export * from './format'; export * from './constant'; -export * from './validate'; export * from './interceptor'; export * from './with-install'; export * from './closest'; diff --git a/packages/vant/src/utils/interceptor.ts b/packages/vant/src/utils/interceptor.ts index 87b5c813fa3..e9f4d578ab6 100644 --- a/packages/vant/src/utils/interceptor.ts +++ b/packages/vant/src/utils/interceptor.ts @@ -1,5 +1,4 @@ -import { noop } from './basic'; -import { isPromise } from './validate'; +import { noop, isPromise } from './basic'; export type Interceptor = ( ...args: any[] @@ -15,7 +14,7 @@ export function callInterceptor( args?: unknown[]; done: () => void; canceled?: () => void; - } + }, ) { if (interceptor) { // eslint-disable-next-line prefer-spread diff --git a/packages/vant/src/utils/test/index.spec.ts b/packages/vant/src/utils/test/index.spec.ts index 428fa1d70d4..6b40da04030 100644 --- a/packages/vant/src/utils/test/index.spec.ts +++ b/packages/vant/src/utils/test/index.spec.ts @@ -1,7 +1,6 @@ -import { get, noop } from '../basic'; +import { get, noop, isDef, isMobile, isNumeric } from '../basic'; import { deepClone } from '../deep-clone'; import { deepAssign } from '../deep-assign'; -import { isDef, isMobile, isNumeric } from '../validate'; import { addUnit, unitToPx, camelize, formatNumber } from '../format'; import { trigger } from '../../../test'; @@ -23,7 +22,7 @@ test('deepAssign', () => { expect(deepAssign({ noop: null }, { noop })).toEqual({ noop }); expect(deepAssign({ foo: 0 }, { bar: 1 })).toEqual({ foo: 0, bar: 1 }); expect( - deepAssign({ foo: { bar: false } }, { foo: { bar: true, foo: false } }) + deepAssign({ foo: { bar: false } }, { foo: { bar: true, foo: false } }), ).toEqual({ foo: { bar: true, diff --git a/packages/vant/src/utils/validate.ts b/packages/vant/src/utils/validate.ts deleted file mode 100644 index c39809367df..00000000000 --- a/packages/vant/src/utils/validate.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { inBrowser, type Numeric } from './basic'; - -export const isDef = (val: T): val is NonNullable => - val !== undefined && val !== null; - -// eslint-disable-next-line @typescript-eslint/ban-types -export const isFunction = (val: unknown): val is Function => - typeof val === 'function'; - -export const isObject = (val: unknown): val is Record => - val !== null && typeof val === 'object'; - -export const isPromise = (val: unknown): val is Promise => - isObject(val) && isFunction(val.then) && isFunction(val.catch); - -export const isDate = (val: unknown): val is Date => - Object.prototype.toString.call(val) === '[object Date]' && - !Number.isNaN((val as Date).getTime()); - -export function isMobile(value: string): boolean { - value = value.replace(/[^-|\d]/g, ''); - return ( - /^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value) - ); -} - -export const isNumeric = (val: Numeric): val is string => - typeof val === 'number' || /^\d+(\.\d+)?$/.test(val); - -export const isIOS = (): boolean => - inBrowser - ? /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase()) - : false;