perf(utils): addUnit support string number (#10362)

This commit is contained in:
kooriookami 2022-11-02 16:48:55 +08:00 committed by GitHub
parent 364d1a24fc
commit 42fb53aaa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -1,5 +1,5 @@
import { isClient } from '@vueuse/core' import { isClient } from '@vueuse/core'
import { isNumber, isObject, isString } from '../types' import { isNumber, isObject, isString, isStringNumber } from '../types'
import { camelize } from '../strings' import { camelize } from '../strings'
import { entriesOf, keysOf } from '../objects' import { entriesOf, keysOf } from '../objects'
import { debugWarn } from '../error' import { debugWarn } from '../error'
@ -76,10 +76,10 @@ export const removeStyle = (
export function addUnit(value?: string | number, defaultUnit = 'px') { export function addUnit(value?: string | number, defaultUnit = 'px') {
if (!value) return '' if (!value) return ''
if (isString(value)) { if (isNumber(value) || isStringNumber(value)) {
return value
} else if (isNumber(value)) {
return `${value}${defaultUnit}` return `${value}${defaultUnit}`
} else if (isString(value)) {
return value
} }
debugWarn(SCOPE, 'binding value must be a string or number') debugWarn(SCOPE, 'binding value must be a string or number')
} }

View File

@ -1,4 +1,4 @@
import { isArray, isObject } from '@vue/shared' import { isArray, isObject, isString } from '@vue/shared'
import { isNil } from 'lodash-unified' import { isNil } from 'lodash-unified'
export { export {
@ -28,3 +28,10 @@ export const isElement = (e: unknown): e is Element => {
export const isPropAbsent = (prop: unknown): prop is null | undefined => { export const isPropAbsent = (prop: unknown): prop is null | undefined => {
return isNil(prop) return isNil(prop)
} }
export const isStringNumber = (val: string): boolean => {
if (!isString(val)) {
return false
}
return !Number.isNaN(Number(val))
}