mirror of
https://github.com/tenrok/vue-meta.git
synced 2026-06-24 03:50:34 +03:00
fix: prefer filter over slice
This commit is contained in:
@@ -7,27 +7,31 @@
|
|||||||
export default function updateAttribute({ attribute } = {}, attrs, tag) {
|
export default function updateAttribute({ attribute } = {}, attrs, tag) {
|
||||||
const vueMetaAttrString = tag.getAttribute(attribute)
|
const vueMetaAttrString = tag.getAttribute(attribute)
|
||||||
const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : []
|
const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : []
|
||||||
const toRemove = [].concat(vueMetaAttrs)
|
const toRemove = Array.from(vueMetaAttrs)
|
||||||
|
|
||||||
|
const keepIndexes = []
|
||||||
for (const attr in attrs) {
|
for (const attr in attrs) {
|
||||||
if (attrs.hasOwnProperty(attr)) {
|
if (attrs.hasOwnProperty(attr)) {
|
||||||
const val = attrs[attr] || ''
|
const value = attrs[attr] || ''
|
||||||
tag.setAttribute(attr, val)
|
tag.setAttribute(attr, value)
|
||||||
|
|
||||||
if (!vueMetaAttrs.includes(attr)) {
|
if (!vueMetaAttrs.includes(attr)) {
|
||||||
vueMetaAttrs.push(attr)
|
vueMetaAttrs.push(attr)
|
||||||
}
|
}
|
||||||
|
|
||||||
const keepIndex = toRemove.indexOf(attr)
|
// filter below wont ever check -1
|
||||||
if (keepIndex !== -1) {
|
keepIndexes.push(toRemove.indexOf(attr))
|
||||||
toRemove.splice(keepIndex, 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toRemove.forEach(attr => tag.removeAttribute(attr))
|
const removedAttributesCount = toRemove
|
||||||
|
.filter((el, index) => !keepIndexes.includes(index))
|
||||||
|
.reduce((acc, attr) => {
|
||||||
|
tag.removeAttribute(attr)
|
||||||
|
return acc + 1
|
||||||
|
}, 0)
|
||||||
|
|
||||||
if (vueMetaAttrs.length === toRemove.length) {
|
if (vueMetaAttrs.length === removedAttributesCount) {
|
||||||
tag.removeAttribute(attribute)
|
tag.removeAttribute(attribute)
|
||||||
} else {
|
} else {
|
||||||
tag.setAttribute(attribute, (vueMetaAttrs.sort()).join(','))
|
tag.setAttribute(attribute, (vueMetaAttrs.sort()).join(','))
|
||||||
|
|||||||
Reference in New Issue
Block a user