Handle repeating elements w explicitArray=false

This commit is contained in:
T. R. Bernstein
2025-07-02 00:01:28 +02:00
parent 38f7230ce1
commit 60173e3e65

View File

@@ -214,15 +214,19 @@ function registerEvents() {
const tokens = path.split('.')
for (let i = 0; i < tokens.length; i++) {
if (tempObj[tokens[i]] && !(explicitArray === false && i === tokens.length - 1)) {
tempObj = tempObj[tokens[i]]
const token = tokens[i]
const isLastToken = i === tokens.length - 1
const doesTokenExist = token in tempObj
if (!doesTokenExist) {
tempObj[token] = explicitArray ? [] : obj
} else {
// if explicitArray is true then create each node as array
// irrespective of how many nodes are there with same name.
tempObj[tokens[i]] = explicitArray ? [] : obj
tempObj = tempObj[tokens[i]]
if (!Array.isArray(tempObj[token]) && isLastToken) tempObj[token] = [tempObj[token]]
}
if (Array.isArray(tempObj) && i !== tokens.length - 1) {
tempObj = tempObj[token]
if (Array.isArray(tempObj) && !isLastToken) {
tempObj = tempObj[tempObj.length - 1]
}
}