refactor link extension

This commit is contained in:
Philipp Kühn
2020-10-25 22:11:53 +01:00
parent 16d52d05a0
commit ac9fdf6481
3 changed files with 38 additions and 36 deletions

View File

@@ -18,3 +18,4 @@ export { default as getSchema } from './src/utils/getSchema'
export { default as generateHtml } from './src/utils/generateHtml'
export { default as getHtmlFromFragment } from './src/utils/getHtmlFromFragment'
export { default as getMarkAttrs } from './src/utils/getMarkAttrs'
export { default as mergeAttributes } from './src/utils/mergeAttributes'

View File

@@ -1,23 +1,25 @@
import { AnyObject } from '../types'
export default function mergeAttributes(attributes1: AnyObject, attributes2: AnyObject) {
const mergedAttributes = { ...attributes1 }
export default function mergeAttributes(...object: AnyObject[]) {
return object.reduce((items, item) => {
const mergedAttributes = { ...items }
Object.entries(attributes2).forEach(([key, value]) => {
if (!mergedAttributes[key]) {
mergedAttributes[key] = value
return
}
Object.entries(item).forEach(([key, value]) => {
if (!mergedAttributes[key]) {
mergedAttributes[key] = value
return
}
if (key === 'class') {
mergedAttributes[key] = [mergedAttributes[key], value].join(' ')
return
}
if (key === 'class') {
mergedAttributes[key] = [mergedAttributes[key], value].join(' ')
return
}
if (key === 'style') {
mergedAttributes[key] = [mergedAttributes[key], value].join('; ')
}
})
if (key === 'style') {
mergedAttributes[key] = [mergedAttributes[key], value].join('; ')
}
})
return mergedAttributes
return mergedAttributes
}, {})
}