Skip to content

Commit f684283

Browse files
committed
refactor: number validation functions for enhanced accuracy
1 parent b21a354 commit f684283

File tree

5 files changed

+21
-12
lines changed

5 files changed

+21
-12
lines changed

packages/components/common/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { prefix } from './config';
2-
import { isString, isNumber, isDef, isBoolean, isObject } from './validator';
2+
import { isString, isNumeric, isDef, isBoolean, isObject } from './validator';
33
import { getWindowInfo, getAppBaseInfo, getDeviceInfo } from './wechat';
44

55
interface WxWorkSystemInfo extends WechatMiniprogram.SystemInfo {
@@ -152,7 +152,7 @@ export const addUnit = function (value?: string | number): string | undefined {
152152
return undefined;
153153
}
154154
value = String(value);
155-
return isNumber(value) ? `${value}px` : value;
155+
return isNumeric(value) ? `${value}px` : value;
156156
};
157157

158158
/**

packages/components/common/validator.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ export function isDef(value: unknown): boolean {
1616
return !isUndefined(value) && !isNull(value);
1717
}
1818

19-
export function isNumber(value: string | number): boolean {
20-
return typeof value === 'number' || /^-?\d+(\.\d+)?$/.test(value);
19+
export function isInteger(value: unknown): boolean {
20+
return Number.isInteger(value);
21+
}
22+
23+
export function isNumeric(value: unknown): boolean {
24+
return !Number.isNaN(Number(value));
25+
}
26+
27+
export function isNumber(value: unknown): boolean {
28+
return typeof value === 'number';
2129
}
2230

2331
export function isBoolean(value: unknown): value is boolean {

packages/components/guide/guide.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SuperComponent, wxComponent } from '../common/src/index';
22
import props from './props';
33
import config from '../common/config';
4-
import { isFunction, isNumber } from '../common/validator';
4+
import { isFunction, isNumeric } from '../common/validator';
55
import { TdGuideProps, GuideStep } from './type';
66
import { debounce, getRect, rpx2px, styles, unitConvert, nextTick, systemInfo } from '../common/utils';
77

@@ -214,8 +214,8 @@ export default class Guide extends SuperComponent {
214214
},
215215
getPlacement() {
216216
const space = rpx2px(32);
217-
const offsetLeft = (offset) => unitConvert(isNumber(offset?.[0]) ? `${offset?.[0]}rpx` : offset?.[0] || 0);
218-
const offsetTop = (offset) => unitConvert(isNumber(offset?.[1]) ? `${offset?.[1]}rpx` : offset?.[1] || 0);
217+
const offsetLeft = (offset) => unitConvert(isNumeric(offset?.[0]) ? `${offset?.[0]}rpx` : offset?.[0] || 0);
218+
const offsetTop = (offset) => unitConvert(isNumeric(offset?.[1]) ? `${offset?.[1]}rpx` : offset?.[1] || 0);
219219
const left = (place) => parseFloat(place.left);
220220
const right = (place) => parseFloat(place.right);
221221
const top = (place) => parseFloat(place.top);

packages/components/skeleton/skeleton.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import props from './props';
44
import { SkeletonRowColObj } from './type';
55
import { ClassName, Styles } from '../common/common';
66
import { classNames } from '../common/utils';
7-
import { isNumber } from '../common/validator';
7+
import { isInteger, isNumeric } from '../common/validator';
88

99
const { prefix } = config;
1010
const name = `${prefix}-skeleton`;
@@ -68,12 +68,13 @@ export default class Skeleton extends SuperComponent {
6868
}
6969

7070
const parsedRowCols = rowCols.map((item) => {
71-
if (isNumber(item)) {
71+
if (isInteger(item) && item >= 0) {
7272
return new Array(item).fill({
7373
class: this.getColItemClass({ type: 'text' }),
7474
style: {},
7575
});
7676
}
77+
7778
if (Array.isArray(item)) {
7879
return item.map((col) => {
7980
return {
@@ -122,7 +123,7 @@ export default class Skeleton extends SuperComponent {
122123
const style: Styles = {};
123124
styleName.forEach((name) => {
124125
if (name in obj) {
125-
const px = isNumber(obj[name]) ? `${obj[name]}px` : obj[name];
126+
const px = isNumeric(obj[name]) ? `${obj[name]}px` : obj[name];
126127
if (name === 'size') {
127128
[style.width, style.height] = [px, px];
128129
} else {

packages/components/tag/tag.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { wxComponent, SuperComponent } from '../common/src/index';
22
import config from '../common/config';
33
import props from './props';
44
import { classNames, calcIcon } from '../common/utils';
5-
import { isNumber } from '../common/validator';
5+
import { isNumeric } from '../common/validator';
66

77
const { prefix } = config;
88
const name = `${prefix}-tag`;
@@ -77,7 +77,7 @@ export default class Tag extends SuperComponent {
7777
if (!maxWidth) {
7878
return '';
7979
}
80-
const width = isNumber(maxWidth) ? `${maxWidth}px` : maxWidth;
80+
const width = isNumeric(maxWidth) ? `${maxWidth}px` : maxWidth;
8181
this.setData({ tagStyle: `max-width:${width};` });
8282
},
8383

0 commit comments

Comments
 (0)