2
0
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:
pimlie
2019-02-10 23:34:26 +01:00
parent 3e010bb9ce
commit 82ba8c0582
+13 -9
View File
@@ -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(','))