Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/components/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { prefix } from './config';
import { isString, isNumber, isDef, isBoolean, isObject } from './validator';
import { isString, isNumeric, isDef, isBoolean, isObject } from './validator';
import { getWindowInfo, getAppBaseInfo, getDeviceInfo } from './wechat';

interface WxWorkSystemInfo extends WechatMiniprogram.SystemInfo {
Expand Down Expand Up @@ -152,7 +152,7 @@ export const addUnit = function (value?: string | number): string | undefined {
return undefined;
}
value = String(value);
return isNumber(value) ? `${value}px` : value;
return isNumeric(value) ? `${value}px` : value;
};

/**
Expand Down
12 changes: 10 additions & 2 deletions packages/components/common/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ export function isDef(value: unknown): boolean {
return !isUndefined(value) && !isNull(value);
}

export function isNumber(value: string | number): boolean {
return typeof value === 'number' || /^-?\d+(\.\d+)?$/.test(value);
export function isInteger(value: unknown): boolean {
return Number.isInteger(value);
}

export function isNumeric(value: unknown): boolean {
return !Number.isNaN(Number(value));
}

export function isNumber(value: unknown): boolean {
return typeof value === 'number';
}

export function isBoolean(value: unknown): value is boolean {
Expand Down
6 changes: 3 additions & 3 deletions packages/components/guide/guide.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SuperComponent, wxComponent } from '../common/src/index';
import props from './props';
import config from '../common/config';
import { isFunction, isNumber } from '../common/validator';
import { isFunction, isNumeric } from '../common/validator';
import { TdGuideProps, GuideStep } from './type';
import { debounce, getRect, rpx2px, styles, unitConvert, nextTick, systemInfo } from '../common/utils';

Expand Down Expand Up @@ -214,8 +214,8 @@ export default class Guide extends SuperComponent {
},
getPlacement() {
const space = rpx2px(32);
const offsetLeft = (offset) => unitConvert(isNumber(offset?.[0]) ? `${offset?.[0]}rpx` : offset?.[0] || 0);
const offsetTop = (offset) => unitConvert(isNumber(offset?.[1]) ? `${offset?.[1]}rpx` : offset?.[1] || 0);
const offsetLeft = (offset) => unitConvert(isNumeric(offset?.[0]) ? `${offset?.[0]}rpx` : offset?.[0] || 0);
const offsetTop = (offset) => unitConvert(isNumeric(offset?.[1]) ? `${offset?.[1]}rpx` : offset?.[1] || 0);
const left = (place) => parseFloat(place.left);
const right = (place) => parseFloat(place.right);
const top = (place) => parseFloat(place.top);
Expand Down
7 changes: 4 additions & 3 deletions packages/components/skeleton/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import props from './props';
import { SkeletonRowColObj } from './type';
import { ClassName, Styles } from '../common/common';
import { classNames } from '../common/utils';
import { isNumber } from '../common/validator';
import { isInteger, isNumeric } from '../common/validator';

const { prefix } = config;
const name = `${prefix}-skeleton`;
Expand Down Expand Up @@ -68,12 +68,13 @@ export default class Skeleton extends SuperComponent {
}

const parsedRowCols = rowCols.map((item) => {
if (isNumber(item)) {
if (isInteger(item) && item >= 0) {
return new Array(item).fill({
class: this.getColItemClass({ type: 'text' }),
style: {},
});
}

if (Array.isArray(item)) {
return item.map((col) => {
return {
Expand Down Expand Up @@ -122,7 +123,7 @@ export default class Skeleton extends SuperComponent {
const style: Styles = {};
styleName.forEach((name) => {
if (name in obj) {
const px = isNumber(obj[name]) ? `${obj[name]}px` : obj[name];
const px = isNumeric(obj[name]) ? `${obj[name]}px` : obj[name];
if (name === 'size') {
[style.width, style.height] = [px, px];
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/components/tag/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { wxComponent, SuperComponent } from '../common/src/index';
import config from '../common/config';
import props from './props';
import { classNames, calcIcon } from '../common/utils';
import { isNumber } from '../common/validator';
import { isNumeric } from '../common/validator';

const { prefix } = config;
const name = `${prefix}-tag`;
Expand Down Expand Up @@ -77,7 +77,7 @@ export default class Tag extends SuperComponent {
if (!maxWidth) {
return '';
}
const width = isNumber(maxWidth) ? `${maxWidth}px` : maxWidth;
const width = isNumeric(maxWidth) ? `${maxWidth}px` : maxWidth;
this.setData({ tagStyle: `max-width:${width};` });
},

Expand Down
Loading