chore: remove isNaN util, re-organize validate utils (#8473)

This commit is contained in:
neverland
2021-04-08 10:08:03 +08:00
committed by GitHub
parent 9f93dd9d61
commit 3214b1dd80
21 changed files with 70 additions and 80 deletions
+1 -18
View File
@@ -10,23 +10,6 @@ export const UnknownProp = (null as unknown) as PropType<unknown>;
// eslint-disable-next-line
export type ComponentInstance = ComponentPublicInstance<{}, any>;
export function isDef<T>(val: T): val is NonNullable<T> {
return val !== undefined && val !== null;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function isFunction(val: unknown): val is Function {
return typeof val === 'function';
}
export function isObject(val: unknown): val is Record<any, any> {
return val !== null && typeof val === 'object';
}
export function isPromise<T = any>(val: unknown): val is Promise<T> {
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
}
export function get(object: any, path: string): any {
const keys = path.split('.');
let result = object;
@@ -38,7 +21,7 @@ export function get(object: any, path: string): any {
return result;
}
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
export type Writeable<T> = { -readonly [P in keyof T]: T[P] };
export function pick<T, U extends keyof T>(
obj: T,
+2 -1
View File
@@ -1,5 +1,6 @@
import { get, isFunction } from '../base';
import { get } from '../base';
import { camelize } from '../format/string';
import { isFunction } from '../validate';
import locale from '../../locale';
export function createTranslate(name: string) {
+1 -1
View File
@@ -1,4 +1,4 @@
import { isDef, isObject } from './base';
import { isDef, isObject } from './validate';
type ObjectIndex = Record<string, any>;
+1 -1
View File
@@ -1,4 +1,4 @@
import { isDef } from './base';
import { isDef } from './validate';
export function deepClone<T extends Record<string, any> | null | undefined>(
obj: T
+1 -1
View File
@@ -1,4 +1,4 @@
import { isIOS as checkIsIOS } from '../validate/system';
import { isIOS as checkIsIOS } from '../validate';
export type ScrollElement = Element | Window;
+2 -2
View File
@@ -1,6 +1,6 @@
import { CSSProperties } from 'vue';
import { isDef, inBrowser } from '../base';
import { isNumeric } from '../validate/number';
import { inBrowser } from '../base';
import { isDef, isNumeric } from '../validate';
export function addUnit(value?: string | number): string | undefined {
if (!isDef(value)) {
+1
View File
@@ -1,5 +1,6 @@
export * from './base';
export * from './create';
export * from './validate';
export * from './with-install';
export * from './format/unit';
export * from './format/number';
+2 -4
View File
@@ -1,9 +1,7 @@
import { deepClone } from '../deep-clone';
import { deepAssign } from '../deep-assign';
import { isDef, get, noop } from '..';
import { isMobile } from '../validate/mobile';
import { isNumeric } from '../validate/number';
import { isAndroid } from '../validate/system';
import { get, noop } from '..';
import { isDef, isMobile, isNumeric, isAndroid } from '../validate';
import { camelize } from '../format/string';
import { formatNumber } from '../format/number';
import { addUnit, unitToPx } from '../format/unit';
+46
View File
@@ -0,0 +1,46 @@
import { inBrowser } from './base';
export function isDef<T>(val: T): val is NonNullable<T> {
return val !== undefined && val !== null;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export function isFunction(val: unknown): val is Function {
return typeof val === 'function';
}
export function isObject(val: unknown): val is Record<any, any> {
return val !== null && typeof val === 'object';
}
export function isPromise<T = any>(val: unknown): val is Promise<T> {
return isObject(val) && isFunction(val.then) && isFunction(val.catch);
}
export function isDate(val: unknown): val is Date {
return (
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 function isNumeric(val: string | number): val is string {
return typeof val === 'number' || /^\d+(\.\d+)?$/.test(val);
}
export function isAndroid(): boolean {
return inBrowser ? /android/.test(navigator.userAgent.toLowerCase()) : false;
}
export function isIOS(): boolean {
return inBrowser
? /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase())
: false;
}
-8
View File
@@ -1,8 +0,0 @@
import { isNaN } from './number';
export function isDate(val: unknown): val is Date {
return (
Object.prototype.toString.call(val) === '[object Date]' &&
!isNaN((val as Date).getTime())
);
}
-6
View File
@@ -1,6 +0,0 @@
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)
);
}
-12
View File
@@ -1,12 +0,0 @@
export function isNumeric(val: string | number): val is string {
return typeof val === 'number' || /^\d+(\.\d+)?$/.test(val);
}
export function isNaN(val: number): val is typeof NaN {
if (Number.isNaN) {
return Number.isNaN(val);
}
// eslint-disable-next-line no-self-compare
return val !== val;
}
-11
View File
@@ -1,11 +0,0 @@
import { inBrowser } from '../base';
export function isAndroid(): boolean {
return inBrowser ? /android/.test(navigator.userAgent.toLowerCase()) : false;
}
export function isIOS(): boolean {
return inBrowser
? /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase())
: false;
}