mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore: improve utils typing and naming
This commit is contained in:
parent
8345d6e126
commit
77756f30e6
@ -3,7 +3,7 @@ import Cell from '../../cell';
|
||||
import CellGroup from '../../cell-group';
|
||||
import Field from '../../field';
|
||||
import { isEmail } from '../../utils/validate/email';
|
||||
import { isNumber } from '../../utils/validate/number';
|
||||
import { isNumeric } from '../../utils/validate/number';
|
||||
import SkuImgUploader from './SkuImgUploader';
|
||||
|
||||
const [createComponent, bem, t] = createNamespace('sku-messages');
|
||||
@ -94,7 +94,7 @@ export default createComponent({
|
||||
return textType + message.name;
|
||||
}
|
||||
} else {
|
||||
if (message.type === 'tel' && !isNumber(value)) {
|
||||
if (message.type === 'tel' && !isNumeric(value)) {
|
||||
return t('invalid.tel');
|
||||
}
|
||||
if (message.type === 'mobile' && !/^\d{6,20}$/.test(value)) {
|
||||
|
@ -1,5 +1,9 @@
|
||||
type ScrollElement = HTMLElement | Window;
|
||||
|
||||
function isWindow(val: unknown): val is Window {
|
||||
return val === window;
|
||||
}
|
||||
|
||||
// get nearest scroll element
|
||||
// http://w3help.org/zh-cn/causes/SD9013
|
||||
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
|
||||
@ -63,24 +67,22 @@ export function setRootScrollTop(value: number) {
|
||||
|
||||
// get distance from element top to page top
|
||||
export function getElementTop(el: ScrollElement) {
|
||||
if (el === window) {
|
||||
if (isWindow(el)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (el as HTMLElement).getBoundingClientRect().top + getRootScrollTop();
|
||||
return el.getBoundingClientRect().top + getRootScrollTop();
|
||||
}
|
||||
|
||||
export function getVisibleHeight(el: ScrollElement) {
|
||||
if (el === window) {
|
||||
if (isWindow(el)) {
|
||||
return el.innerHeight;
|
||||
}
|
||||
|
||||
return (el as HTMLElement).getBoundingClientRect().height;
|
||||
return el.getBoundingClientRect().height;
|
||||
}
|
||||
|
||||
export function getVisibleTop(el: ScrollElement) {
|
||||
if (el === window) {
|
||||
if (isWindow(el)) {
|
||||
return 0;
|
||||
}
|
||||
return (el as HTMLElement).getBoundingClientRect().top;
|
||||
return el.getBoundingClientRect().top;
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { isDef } from '..';
|
||||
import { isNumber } from '../validate/number';
|
||||
import { isNumeric } from '../validate/number';
|
||||
|
||||
export function addUnit(value?: string | number): string | undefined {
|
||||
if (!isDef(value)) {
|
||||
@ -7,5 +7,5 @@ export function addUnit(value?: string | number): string | undefined {
|
||||
}
|
||||
|
||||
value = String(value);
|
||||
return isNumber(value) ? `${value}px` : value;
|
||||
return isNumeric(value) ? `${value}px` : value;
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import { raf, cancelRaf } from '../dom/raf';
|
||||
import { later } from '../../../test';
|
||||
import { isEmail } from '../validate/email';
|
||||
import { isMobile } from '../validate/mobile';
|
||||
import { isNumber } from '../validate/number';
|
||||
import { isNumeric } from '../validate/number';
|
||||
import { isAndroid } from '../validate/system';
|
||||
import { camelize } from '../format/string';
|
||||
|
||||
@ -92,9 +92,9 @@ test('is-mobile', () => {
|
||||
});
|
||||
|
||||
test('is-number', () => {
|
||||
expect(isNumber('1')).toBeTruthy();
|
||||
expect(isNumber('1.2')).toBeTruthy();
|
||||
expect(isNumber('1..2')).toBeFalsy();
|
||||
expect(isNumber('abc')).toBeFalsy();
|
||||
expect(isNumber('1b2')).toBeFalsy();
|
||||
expect(isNumeric('1')).toBeTruthy();
|
||||
expect(isNumeric('1.2')).toBeTruthy();
|
||||
expect(isNumeric('1..2')).toBeFalsy();
|
||||
expect(isNumeric('abc')).toBeFalsy();
|
||||
expect(isNumeric('1b2')).toBeFalsy();
|
||||
});
|
||||
|
@ -1,12 +1,12 @@
|
||||
export function isNumber(value: string): boolean {
|
||||
return /^\d+(\.\d+)?$/.test(value);
|
||||
export function isNumeric(val: string): boolean {
|
||||
return /^\d+(\.\d+)?$/.test(val);
|
||||
}
|
||||
|
||||
export function isNaN(value: any): boolean {
|
||||
export function isNaN(val: number): val is typeof NaN {
|
||||
if (Number.isNaN) {
|
||||
return Number.isNaN(value);
|
||||
return Number.isNaN(val);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-self-compare
|
||||
return value !== value;
|
||||
return val !== val;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user