Skip to content

Commit 1160994

Browse files
authored
feat: add isNumber function (#256)
1 parent d4ae688 commit 1160994

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/theme/calc/CSSCalculator.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import { isNumber } from '../../util';
12
import AbstractCalculator from './calculator';
23

34
const CALC_UNIT = 'CALC_UNIT';
45

56
const regexp = new RegExp(CALC_UNIT, 'g');
67

7-
function unit(value: string | number) {
8-
if (typeof value === 'number') {
8+
const unit = (value: string | number) => {
9+
if (isNumber(value)) {
910
return `${value}${CALC_UNIT}`;
1011
}
1112
return value;
12-
}
13+
};
1314

1415
export default class CSSCalculator extends AbstractCalculator {
1516
result: string = '';
@@ -30,8 +31,8 @@ export default class CSSCalculator extends AbstractCalculator {
3031

3132
if (num instanceof CSSCalculator) {
3233
this.result = `(${num.result})`;
33-
} else if (numType === 'number') {
34-
this.result = unit(num as number);
34+
} else if (isNumber(num)) {
35+
this.result = unit(num);
3536
} else if (numType === 'string') {
3637
this.result = num as string;
3738
}

src/util/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,16 @@ export function supportLogicProps(): boolean {
155155

156156
export const isClientSide = canUseDom();
157157

158-
export function unit(num: string | number) {
159-
if (typeof num === 'number') {
158+
export const isNumber = (val: any): val is number => {
159+
return typeof val === 'number' && !Number.isNaN(val);
160+
};
161+
162+
export const unit = (num: string | number) => {
163+
if (isNumber(num)) {
160164
return `${num}px`;
161165
}
162166
return num;
163-
}
167+
};
164168

165169
export function toStyleStr(
166170
style: string,

0 commit comments

Comments
 (0)