Skip to content

Commit 82ba8c0

Browse files
committed
fix: prefer filter over slice
1 parent 3e010bb commit 82ba8c0

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/client/updaters/attribute.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,31 @@
77
export default function updateAttribute({ attribute } = {}, attrs, tag) {
88
const vueMetaAttrString = tag.getAttribute(attribute)
99
const vueMetaAttrs = vueMetaAttrString ? vueMetaAttrString.split(',') : []
10-
const toRemove = [].concat(vueMetaAttrs)
10+
const toRemove = Array.from(vueMetaAttrs)
1111

12+
const keepIndexes = []
1213
for (const attr in attrs) {
1314
if (attrs.hasOwnProperty(attr)) {
14-
const val = attrs[attr] || ''
15-
tag.setAttribute(attr, val)
15+
const value = attrs[attr] || ''
16+
tag.setAttribute(attr, value)
1617

1718
if (!vueMetaAttrs.includes(attr)) {
1819
vueMetaAttrs.push(attr)
1920
}
2021

21-
const keepIndex = toRemove.indexOf(attr)
22-
if (keepIndex !== -1) {
23-
toRemove.splice(keepIndex, 1)
24-
}
22+
// filter below wont ever check -1
23+
keepIndexes.push(toRemove.indexOf(attr))
2524
}
2625
}
2726

28-
toRemove.forEach(attr => tag.removeAttribute(attr))
27+
const removedAttributesCount = toRemove
28+
.filter((el, index) => !keepIndexes.includes(index))
29+
.reduce((acc, attr) => {
30+
tag.removeAttribute(attr)
31+
return acc + 1
32+
}, 0)
2933

30-
if (vueMetaAttrs.length === toRemove.length) {
34+
if (vueMetaAttrs.length === removedAttributesCount) {
3135
tag.removeAttribute(attribute)
3236
} else {
3337
tag.setAttribute(attribute, (vueMetaAttrs.sort()).join(','))

0 commit comments

Comments
 (0)