ramda / ramda

:ram: Practical functional Javascript

Home Page:https://ramdajs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

assocPath number path with undefined value expect array but got object

BoBoooooo opened this issue · comments

when input {a: undefined}

R.assocPath(['a', 0], 42, {a: undefined});
// output
{a: {0: 42}}
// expect
{a: [42]}

if input is "{a: []}", is ok , like

R.assocPath(['a', 0], 42, {a: []});
// output
{a: [42]}

is it designed so ? thanks !

This has more to do with javascript than ramda itself. When you have to create a new path inside an object, the implementation of assocPath starts off with an empty object literal and fills it with the value in the corresponding path. Literal objects have Object.prototype as their prototype and therefore will be printed as an object even if you add numerical indexes to them. On the other hand, array literals have Array.prototype as their prototype and if you start with an empty array, as you did in your second example, this empty array will be used to fill the path and the result will be printed as an usual array.