From 82ba8c05821ff0d2df293397048fe800bdaffcb6 Mon Sep 17 00:00:00 2001 From: pimlie Date: Sun, 10 Feb 2019 23:34:26 +0100 Subject: [PATCH] fix: prefer filter over slice --- src/client/updaters/attribute.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/client/updaters/attribute.js b/src/client/updaters/attribute.js index e3cf648..4808f74 100644 --- a/src/client/updaters/attribute.js +++ b/src/client/updaters/attribute.js @@ -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(','))