From 42fb53aaa66412954f4f70f611598b4a6cd341df Mon Sep 17 00:00:00 2001 From: kooriookami <38392315+kooriookami@users.noreply.github.com> Date: Wed, 2 Nov 2022 16:48:55 +0800 Subject: [PATCH] perf(utils): addUnit support string number (#10362) --- packages/utils/dom/style.ts | 8 ++++---- packages/utils/types.ts | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/utils/dom/style.ts b/packages/utils/dom/style.ts index e2829c715a..4f8a1ef2e6 100644 --- a/packages/utils/dom/style.ts +++ b/packages/utils/dom/style.ts @@ -1,5 +1,5 @@ import { isClient } from '@vueuse/core' -import { isNumber, isObject, isString } from '../types' +import { isNumber, isObject, isString, isStringNumber } from '../types' import { camelize } from '../strings' import { entriesOf, keysOf } from '../objects' import { debugWarn } from '../error' @@ -76,10 +76,10 @@ export const removeStyle = ( export function addUnit(value?: string | number, defaultUnit = 'px') { if (!value) return '' - if (isString(value)) { - return value - } else if (isNumber(value)) { + if (isNumber(value) || isStringNumber(value)) { return `${value}${defaultUnit}` + } else if (isString(value)) { + return value } debugWarn(SCOPE, 'binding value must be a string or number') } diff --git a/packages/utils/types.ts b/packages/utils/types.ts index 5d7837353d..142d270b93 100644 --- a/packages/utils/types.ts +++ b/packages/utils/types.ts @@ -1,4 +1,4 @@ -import { isArray, isObject } from '@vue/shared' +import { isArray, isObject, isString } from '@vue/shared' import { isNil } from 'lodash-unified' export { @@ -28,3 +28,10 @@ export const isElement = (e: unknown): e is Element => { export const isPropAbsent = (prop: unknown): prop is null | undefined => { return isNil(prop) } + +export const isStringNumber = (val: string): boolean => { + if (!isString(val)) { + return false + } + return !Number.isNaN(Number(val)) +}