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++) {
// 递归调用进行处理
const normalized = normalizeClass(value[i])
if (normalized) {
res += normalized + ' '
// 如果是对象, 如{ active: isActive, 'text-danger': hasError },需要把key拼接
} else if (isObject(value)) {
for (const name in value) {
if (value[name]) {
res += name + ' '
return res.trim()
再看看normalizeStyle这个函数
export type NormalizedStyle = Record<string, string | number>
export function normalizeStyle(
value: unknown
): NormalizedStyle | string | undefined {
// 如果是数组的情况
if (isArray(value)) {
const res: NormalizedStyle = {}
for (let i = 0; i < value.length; i++) {
const item = value[i]
const normalized = isString(item)
? parseStringStyle(item)
: (normalizeStyle(item) as NormalizedStyle)
if (normalized) {
// 将序列化后的style保存到ret上
for (const key in normalized) {
res[key] = normalized[key]
return res
} else if (isString(value)) {
return value
} else if (isObject(value)) {
return value
export function stringifyStyle(
styles: NormalizedStyle | string | undefined
): string {
let ret = ''
if (!styles || isString(styles)) {
return ret
for (const key in styles) {
const value = styles[key]
const normalizedKey = key.startsWith(`--`) ? key : hyphenate(key)
isString(value) ||
(typeof value === 'number' && isNoUnitNumericStyleProp(normalizedKey))
// only render valid values
ret += `${normalizedKey}:${value};`
return ret