hughsk/flat

Does unflatten enter into infinite recursion on function or array members?

cmeeren opened this issue · 3 comments

I'm using this library in an F# project (transpiles to JS through using Fable). So unfortunately I have no JS repro (I'm not a JS dev).

unflatten is mostly working fine, but it seems to enter into infinite recursion on some objects. This happens even when there is seemingly nothing to unflatten.

I have an array of key-value pairs (React props) that I'm converting into an object, and then I'm using unflatten on this object.

This works fine for some prop arrays, such as this:

image

However, for others, such as this, it enters into an infinite recursion:

image

image

As you can see, in both cases, the original prop list (and thus the object) does not contain keys with the delimiter, and there are no sub-objects to recurse into (unless it does something with the function), so AFAIK unflatten shouldn't modify anything.

I have tried safe without any effect.

The weird thing is, the stack trace seems to come from the flatten function, which I do not call:

image

I think perhaps the problem might be the recursion into the children prop. I'll investigate some more.

I think I have a solution. For my use-case, I simply need unflatten to not recurse on this line:

flat/index.js

Lines 144 to 145 in 6e95c43

// unflatten again for 'messy objects'
recipient[key1] = unflatten(target[key], opts)

Edit: As well as only ever enter the first case here (i.e., not call addKeys):

flat/index.js

Lines 100 to 109 in 6e95c43

if (!isObject || isEmpty(target[key])) {
result[key] = target[key]
return result
} else {
return addKeys(
key,
result,
flatten(target[key], opts)
)
}

Would it be possible to add an option that controls this?

I decided to implement my own unflatten instead. Feel free to close if you want.