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 CellGroup from '../../cell-group';
|
||||||
import Field from '../../field';
|
import Field from '../../field';
|
||||||
import { isEmail } from '../../utils/validate/email';
|
import { isEmail } from '../../utils/validate/email';
|
||||||
import { isNumber } from '../../utils/validate/number';
|
import { isNumeric } from '../../utils/validate/number';
|
||||||
import SkuImgUploader from './SkuImgUploader';
|
import SkuImgUploader from './SkuImgUploader';
|
||||||
|
|
||||||
const [createComponent, bem, t] = createNamespace('sku-messages');
|
const [createComponent, bem, t] = createNamespace('sku-messages');
|
||||||
@ -94,7 +94,7 @@ export default createComponent({
|
|||||||
return textType + message.name;
|
return textType + message.name;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (message.type === 'tel' && !isNumber(value)) {
|
if (message.type === 'tel' && !isNumeric(value)) {
|
||||||
return t('invalid.tel');
|
return t('invalid.tel');
|
||||||
}
|
}
|
||||||
if (message.type === 'mobile' && !/^\d{6,20}$/.test(value)) {
|
if (message.type === 'mobile' && !/^\d{6,20}$/.test(value)) {
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
type ScrollElement = HTMLElement | Window;
|
type ScrollElement = HTMLElement | Window;
|
||||||
|
|
||||||
|
function isWindow(val: unknown): val is Window {
|
||||||
|
return val === window;
|
||||||
|
}
|
||||||
|
|
||||||
// get nearest scroll element
|
// get nearest scroll element
|
||||||
// http://w3help.org/zh-cn/causes/SD9013
|
// http://w3help.org/zh-cn/causes/SD9013
|
||||||
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
|
// 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
|
// get distance from element top to page top
|
||||||
export function getElementTop(el: ScrollElement) {
|
export function getElementTop(el: ScrollElement) {
|
||||||
if (el === window) {
|
if (isWindow(el)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
return el.getBoundingClientRect().top + getRootScrollTop();
|
||||||
return (el as HTMLElement).getBoundingClientRect().top + getRootScrollTop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getVisibleHeight(el: ScrollElement) {
|
export function getVisibleHeight(el: ScrollElement) {
|
||||||
if (el === window) {
|
if (isWindow(el)) {
|
||||||
return el.innerHeight;
|
return el.innerHeight;
|
||||||
}
|
}
|
||||||
|
return el.getBoundingClientRect().height;
|
||||||
return (el as HTMLElement).getBoundingClientRect().height;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getVisibleTop(el: ScrollElement) {
|
export function getVisibleTop(el: ScrollElement) {
|
||||||
if (el === window) {
|
if (isWindow(el)) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return (el as HTMLElement).getBoundingClientRect().top;
|
return el.getBoundingClientRect().top;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { isDef } from '..';
|
import { isDef } from '..';
|
||||||
import { isNumber } from '../validate/number';
|
import { isNumeric } from '../validate/number';
|
||||||
|
|
||||||
export function addUnit(value?: string | number): string | undefined {
|
export function addUnit(value?: string | number): string | undefined {
|
||||||
if (!isDef(value)) {
|
if (!isDef(value)) {
|
||||||
@ -7,5 +7,5 @@ export function addUnit(value?: string | number): string | undefined {
|
|||||||
}
|
}
|
||||||
|
|
||||||
value = String(value);
|
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 { later } from '../../../test';
|
||||||
import { isEmail } from '../validate/email';
|
import { isEmail } from '../validate/email';
|
||||||
import { isMobile } from '../validate/mobile';
|
import { isMobile } from '../validate/mobile';
|
||||||
import { isNumber } from '../validate/number';
|
import { isNumeric } from '../validate/number';
|
||||||
import { isAndroid } from '../validate/system';
|
import { isAndroid } from '../validate/system';
|
||||||
import { camelize } from '../format/string';
|
import { camelize } from '../format/string';
|
||||||
|
|
||||||
@ -92,9 +92,9 @@ test('is-mobile', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test('is-number', () => {
|
test('is-number', () => {
|
||||||
expect(isNumber('1')).toBeTruthy();
|
expect(isNumeric('1')).toBeTruthy();
|
||||||
expect(isNumber('1.2')).toBeTruthy();
|
expect(isNumeric('1.2')).toBeTruthy();
|
||||||
expect(isNumber('1..2')).toBeFalsy();
|
expect(isNumeric('1..2')).toBeFalsy();
|
||||||
expect(isNumber('abc')).toBeFalsy();
|
expect(isNumeric('abc')).toBeFalsy();
|
||||||
expect(isNumber('1b2')).toBeFalsy();
|
expect(isNumeric('1b2')).toBeFalsy();
|
||||||
});
|
});
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
export function isNumber(value: string): boolean {
|
export function isNumeric(val: string): boolean {
|
||||||
return /^\d+(\.\d+)?$/.test(value);
|
return /^\d+(\.\d+)?$/.test(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isNaN(value: any): boolean {
|
export function isNaN(val: number): val is typeof NaN {
|
||||||
if (Number.isNaN) {
|
if (Number.isNaN) {
|
||||||
return Number.isNaN(value);
|
return Number.isNaN(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line no-self-compare
|
// eslint-disable-next-line no-self-compare
|
||||||
return value !== value;
|
return val !== val;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user