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