Open jazzypants1989 opened 1 year ago
This will be the death of me. I'm still not even sure what I want to happen when trying to insert make a NodeList become another NodeList. This is my most recent attempt:
export function become(
elements,
replacements,
options = { mode: "clone", match: "cycle" }
) {
const children = Array.isArray(elements) ? elements : [elements].flat()
const replacementsArray = Array.isArray(replacements)
? replacements
: [replacements].flat()
let i = 0
const matchModes = {
cycle: () => {
return () => replacementsArray[i++ % replacementsArray.length]
},
repeat: () => {
return () => {
if (i < replacementsArray.length - 1) i++
return replacementsArray[i]
}
},
default: () => {
return () => {
if (i < replacementsArray.length) {
return replacementsArray[i++]
}
}
},
}
const matchMode = matchModes[options.match] || matchModes.default
const getReplacement = matchMode()
children.forEach((child) => {
const replacement = getReplacement()
if (replacement) {
const parent = child.parentElement
modifyDOM(parent, replacement, options)
child.remove()
}
})
}
I can't reasonably spend any more time on this issue... Someone save me!
To clarify, cloneTo works perfectly for this situation, but I really want this library to emphasize flexibility.
I know this is probably an easy fix, but I have spent an absolutely insane amount of time debugging this compared to the rest of this library. I tried implementing some behavior where the replacement elements were either cycled or original elements were removed if there were not enough replacements... I had it working for a moment, but I re-factored stuff and I have NO idea when I broke it.
Honestly, the logic of taking every element in a collection and replacing it with every element in another collection gets REALLY confusing once you start to think about it. There's so many different ways that could really work. Do you want each element to contain EVERY element in the other collection, or are you trying to match elements one by one? I could see why you would want either behavior depending on the situation, but this is very hard to make happen.
If you want to see the unwanted behavior, just look at the test: become3.html. Here it is in full:
If anyone wants me to love them forever, just help me figure this out: PLEASE!