| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- import { isArray, isString, isObject, hyphenate } from './'
- import { isNoUnitNumericStyleProp } from './domAttrConfig'
- export function normalizeStyle(
- value: unknown
- ): Record<string, string | number> | undefined {
- if (isArray(value)) {
- const res: Record<string, string | number> = {}
- for (let i = 0; i < value.length; i++) {
- const normalized = normalizeStyle(value[i])
- if (normalized) {
- for (const key in normalized) {
- res[key] = normalized[key]
- }
- }
- }
- return res
- } else if (isObject(value)) {
- return value
- }
- }
- export function stringifyStyle(
- styles: Record<string, string | number> | undefined
- ): string {
- let ret = ''
- if (!styles) {
- return ret
- }
- for (const key in styles) {
- const value = styles[key]
- const normalizedKey = key.indexOf(`--`) === 0 ? key : hyphenate(key)
- if (
- isString(value) ||
- (typeof value === 'number' && isNoUnitNumericStyleProp(normalizedKey))
- ) {
- // only render valid values
- ret += `${normalizedKey}:${value};`
- }
- }
- return ret
- }
- export function normalizeClass(value: unknown): string {
- let res = ''
- if (isString(value)) {
- res = value
- } else if (isArray(value)) {
- for (let i = 0; i < value.length; i++) {
- res += normalizeClass(value[i]) + ' '
- }
- } else if (isObject(value)) {
- for (const name in value) {
- if (value[name]) {
- res += name + ' '
- }
- }
- }
- return res.trim()
- }
|