61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
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()
|
|
}
|