split helpers and utilities

This commit is contained in:
Philipp Kühn
2020-11-30 09:42:53 +01:00
parent 8d38459289
commit f486ddf80a
56 changed files with 57 additions and 60 deletions

View File

@@ -0,0 +1,28 @@
import { AnyObject } from '../types'
export default function mergeAttributes(...objects: AnyObject[]) {
return objects
.filter(item => !!item)
.reduce((items, item) => {
const mergedAttributes = { ...items }
Object.entries(item).forEach(([key, value]) => {
const exists = mergedAttributes[key]
if (!exists) {
mergedAttributes[key] = value
return
}
if (key === 'class') {
mergedAttributes[key] = [mergedAttributes[key], value].join(' ')
} else if (key === 'style') {
mergedAttributes[key] = [mergedAttributes[key], value].join('; ')
} else {
mergedAttributes[key] = value
}
})
return mergedAttributes
}, {})
}