Skip to content

Commit

Permalink
♻️ Uses Map
Browse files Browse the repository at this point in the history
  • Loading branch information
ekwoka committed Sep 30, 2024
1 parent 0f9e12e commit 985ce1c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/alpinejs/src/directives/x-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,36 @@ function loop(templateEl, iteratorNames, evaluateItems, evaluateKey) {
const oldLookup = templateEl._x_lookup
const lookup = new Map()
templateEl._x_lookup = lookup
const scopeEntries = []

const hasStringKeys = isObject(items)
Object.entries(items).forEach(([index, item]) => {
const scopeEntries = Object.entries(items).map(([index, item]) => {
if (!hasStringKeys) index = parseInt(index)
const scope = getIterationScopeVariables(
iteratorNames,
item,
index,
items
)

let key;
evaluateKey(
key => {
if (typeof key === 'object')
innerKey => {
if (typeof innerKey === 'object')
warn(
'x-for key cannot be an object, it must be a string or an integer',
templateEl
)

if (oldLookup.has(key)) {
lookup.set(key, oldLookup.get(key))
oldLookup.delete(key)
if (oldLookup.has(innerKey)) {
lookup.set(innerKey, oldLookup.get(innerKey))
oldLookup.delete(innerKey)
}
scopeEntries.push([key, scope])
key = innerKey
},
{
scope: { index, ...scope },
}
)
return [key, scope];
})

mutateDom(() => {
Expand Down

0 comments on commit 985ce1c

Please sign in to comment.