Skip to content

Commit

Permalink
chore(utils): fix circular dependency (#12110)
Browse files Browse the repository at this point in the history
* chore: fix circular dependency

* chore: fix

* chore: fix test
  • Loading branch information
chenjiahan authored Jul 22, 2023
1 parent 78064c6 commit b601ca1
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 60 deletions.
50 changes: 42 additions & 8 deletions packages/vant/src/utils/basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isObject } from './validate';
import type { ComponentPublicInstance } from 'vue';

export function noop() {}
Expand All @@ -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<any, any> =>
val !== null && typeof val === 'object';

export const isDef = <T>(val: T): val is NonNullable<T> =>
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 = <T = any>(val: unknown): val is Promise<T> =>
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;
Expand All @@ -32,14 +63,17 @@ export type RequiredParams<T> = T extends (...args: infer P) => infer R
export function pick<T, U extends keyof T>(
obj: T,
keys: ReadonlyArray<U>,
ignoreUndefined?: boolean
ignoreUndefined?: boolean,
) {
return keys.reduce((ret, key) => {
if (!ignoreUndefined || obj[key] !== undefined) {
ret[key] = obj[key];
}
return ret;
}, {} as Writeable<Pick<T, U>>);
return keys.reduce(
(ret, key) => {
if (!ignoreUndefined || obj[key] !== undefined) {
ret[key] = obj[key];
}
return ret;
},
{} as Writeable<Pick<T, U>>,
);
}

export const isSameValue = (newValue: unknown, oldValue: unknown) =>
Expand Down
7 changes: 3 additions & 4 deletions packages/vant/src/utils/create.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -31,13 +30,13 @@ function genBem(name: string, mods?: Mods): string {
if (Array.isArray(mods)) {
return (mods as Mod[]).reduce<string>(
(ret, item) => ret + genBem(name, item),
''
'',
);
}

return Object.keys(mods).reduce(
(ret, key) => ret + (mods[key] ? genBem(name, key) : ''),
''
'',
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vant/src/utils/deep-assign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDef, isObject } from './validate';
import { isDef, isObject } from './basic';

type ObjectIndex = Record<string, unknown>;

Expand Down
4 changes: 2 additions & 2 deletions packages/vant/src/utils/deep-clone.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isDef, isObject } from './validate';
import { isDef, isObject } from './basic';

export function deepClone<T extends Record<string, any> | null | undefined>(
obj: T
obj: T,
): T {
if (!isDef(obj)) {
return obj;
Expand Down
4 changes: 2 additions & 2 deletions packages/vant/src/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -67,7 +67,7 @@ export function preventDefault(event: Event, isStopPropagation?: boolean) {
}

export function isHidden(
elementRef: HTMLElement | Ref<HTMLElement | undefined>
elementRef: HTMLElement | Ref<HTMLElement | undefined>,
) {
const el = unref(elementRef);
if (!el) {
Expand Down
6 changes: 3 additions & 3 deletions packages/vant/src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion packages/vant/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
5 changes: 2 additions & 3 deletions packages/vant/src/utils/interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { noop } from './basic';
import { isPromise } from './validate';
import { noop, isPromise } from './basic';

export type Interceptor = (
...args: any[]
Expand All @@ -15,7 +14,7 @@ export function callInterceptor(
args?: unknown[];
done: () => void;
canceled?: () => void;
}
},
) {
if (interceptor) {
// eslint-disable-next-line prefer-spread
Expand Down
5 changes: 2 additions & 3 deletions packages/vant/src/utils/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down
33 changes: 0 additions & 33 deletions packages/vant/src/utils/validate.ts

This file was deleted.

0 comments on commit b601ca1

Please sign in to comment.